fixed sign-extend bug
authoradam <adam@megacz.com>
Fri, 16 Feb 2007 14:58:07 +0000 (15:58 +0100)
committeradam <adam@megacz.com>
Fri, 16 Feb 2007 14:58:07 +0000 (15:58 +0100)
src/edu/berkeley/fleet/ies44/InstructionEncoder.java

index 72e7839..e8f38a3 100644 (file)
@@ -64,6 +64,7 @@ public abstract class InstructionEncoder {
             case 2: {
                 BenkoBox name = getBoxByInstAddr((inst >> 24) & 0x7ff);
                 int val   = ((int)(instr     )) & ~(0xffffffff << 24);
+                val = signExtend24(val);
                 return new Instruction.Literal.Absolute(name, val);
             }
 
@@ -123,7 +124,7 @@ public abstract class InstructionEncoder {
             instr |= (getBoxAddr(ld.dest)) << 24;
             if (ld.value >= (1<<25))
                 throw new RuntimeException("literals must be less than 2^24");
-            instr |= ((long)ld.value);
+            instr |= ((long)ld.value) & ~(0xffffffff << 24);
 
         } else if (d instanceof Instruction.Literal.Relative) {
             Instruction.Literal.Relative lr = (Instruction.Literal.Relative)d;
@@ -147,4 +148,10 @@ public abstract class InstructionEncoder {
         os.write((int)((instr >> (0*8)) & 0xff));
    }
 
+    public static int signExtend24(int val) {
+        if ((val & (1 << 23)) != 0)
+            val = val | (0xffffffff << 24);
+        return val;
+    }
+
 }
\ No newline at end of file