Ver Fonte

fix lin loop bug

Nicolas Winkler há 4 anos atrás
pai
commit
14374e1029
3 ficheiros alterados com 16 adições e 16 exclusões
  1. 8 8
      src/compile.rs
  2. 1 1
      src/interpret.rs
  3. 7 7
      src/trans/python.rs

+ 8 - 8
src/compile.rs

@@ -112,7 +112,7 @@ impl CodeGenerator {
 impl ir::ConstVisitor for CodeGenerator {
     type Ret = ();
 
-    fn visit_nop(&mut self, nop: &Instruction) {
+    fn visit_nop(&mut self, _nop: &Instruction) {
     }
 
     fn visit_add(&mut self, add: &'_ Instruction) {
@@ -133,10 +133,9 @@ impl ir::ConstVisitor for CodeGenerator {
 
     fn visit_linear_loop(&mut self, l: &Instruction) {
         if let Instruction::LinearLoop{ offset: glob_offset, factors } = l {
-            if factors.len() > 1 ||
-                factors.len() >= 1 && !factors.contains_key(&0) {
+            if factors.len() > 0 {
                 dynasm!(self.buffer
-                    ; movzx ecx, BYTE [rdi + *glob_offset as i32]
+                    ; movzx rcx, BYTE [rdi + *glob_offset as i32]
                 );
             }
             for (&offset, &factor) in factors {
@@ -161,19 +160,19 @@ impl ir::ConstVisitor for CodeGenerator {
                 else if factor == 3 {
                     dynasm!(self.buffer
                         ; lea ebx, [rcx + rcx * 2]
-                        ; mov [rdi + offset as i32], bl
+                        ; add BYTE [rdi + absoff as i32], bl
                     );
                 }
                 else if factor == 5 {
                     dynasm!(self.buffer
                         ; lea ebx, [rcx + rcx * 4]
-                        ; mov [rdi + offset as i32], bl
+                        ; add BYTE [rdi + absoff as i32], bl
                     );
                 }
                 else if factor == 9 {
                     dynasm!(self.buffer
                         ; lea ebx, [rcx + rcx * 8]
-                        ; mov [rdi + offset as i32], bl
+                        ; add BYTE [rdi + absoff as i32], bl
                     );
                 }
                 else if factor.count_ones() == 1 {
@@ -265,7 +264,8 @@ impl ir::ConstVisitor for CodeGenerator {
 
 extern "C" fn putbyte(chr: u8) {
     //print!("{:?}", chr as char);
-    std::io::stdout().write(&[chr]);
+    std::io::stdout().write(&[chr]).unwrap();
+    std::io::stdout().flush().unwrap();
 }
 
 extern "C" fn readbyte() -> u8 {

+ 1 - 1
src/interpret.rs

@@ -45,7 +45,7 @@ impl FromNum for Wrapping<u16> {
 }
 impl CellWrite for Wrapping<u16> {
     fn write<S: Write>(&self, s: &mut S) {
-        write!(s, "{}", self.0);
+        s.write(&[self.0 as _]).unwrap();
         s.flush().unwrap();
     }
 }

+ 7 - 7
src/trans/python.rs

@@ -22,23 +22,23 @@ fn generate(formatter: &mut Formatter, instrs: &Vec<Instruction>, opts: &Options
 
     let cell_mask = match opts.cell_size {
         CellSize::Bits(n) => {
-            "0x".to_owned() + &hex_bitmask(n)
+            " & 0x".to_owned() + &hex_bitmask(n)
         },
-        CellSize::Modular(n) => n.to_string(),
-        CellSize::Int => "-1".to_owned()
+        CellSize::Modular(n) => " % ".to_owned() + &n.to_string(),
+        CellSize::Int => "".to_owned()
     };
     for instr in instrs {
         match instr {
             Instruction::Nop => {},
             Instruction::Add{ offset, value } => {
-                formatter.add_line(&format!("mem[(ptr + {}) & 0xFFFF] = (mem[(ptr + {}) & 0xFFFF] + {}) & {}", offset, offset, value, cell_mask));
+                formatter.add_line(&format!("mem[(ptr + {}) & 0xFFFF] = (mem[(ptr + {}) & 0xFFFF] + {}){}", offset, offset, value, cell_mask));
             },
             Instruction::Set{ offset, value } => {
-                formatter.add_line(&format!("mem[(ptr + {}) & 0xFFFF] = {} & {}", offset, value, cell_mask));
+                formatter.add_line(&format!("mem[(ptr + {}) & 0xFFFF] = {}{}", offset, value, cell_mask));
             },
             Instruction::LinearLoop{ offset, factors } => {
                 for (off, factor) in factors {
-                    formatter.add_line(&format!("mem[(ptr + {}) & 0xFFFF] = (mem[(ptr + {}) & 0xFFFF] + {} * mem[(ptr + {}) & 0xFFFF]) & {}",
+                    formatter.add_line(&format!("mem[(ptr + {}) & 0xFFFF] = (mem[(ptr + {}) & 0xFFFF] + {} * mem[(ptr + {}) & 0xFFFF]){}",
                                                 offset + off, offset + off, factor, offset, cell_mask));
                 }
                 formatter.add_line(&format!("mem[(ptr + {}) & 0xFFFF] = 0", offset));
@@ -53,7 +53,7 @@ fn generate(formatter: &mut Formatter, instrs: &Vec<Instruction>, opts: &Options
                 formatter.unindent();
             },
             Instruction::Read(offset) => {
-                formatter.add_line(&format!("mem[(ptr + {}) & 0xFFFF] = sys.stdin.buffer.read(1)", offset));
+                formatter.add_line(&format!("mem[(ptr + {}) & 0xFFFF] = sys.stdin.buffer.read(1){}", offset, cell_mask));
             },
             Instruction::Write(offset) => {
                 formatter.add_line(&format!("sys.stdout.buffer.write(mem[(ptr + {}) & 0xFFFF].to_bytes(1, 'little'))", offset));