Skip to content

Commit a40b99a

Browse files
committed
Fixed division. Chapter 12 is done!
1 parent cc9645d commit a40b99a

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

src/com/plasstech/lang/c/codegen/tacky/TackyInstructionToInstructionsVisitor.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ public List<Instruction> visit(TackyBinary op) {
122122
}
123123
} else {
124124
// page 288
125-
// mov (left, register(ax))
125+
// mov (left, register(ax)) (above)
126+
// mov 0, rdx
127+
instructions.add(new Mov(leftType, new Imm(0), RegisterOperand.RDX));
126128
// div(right)
127129
instructions.add(new Div(leftType, right));
128130
if (operator == TokenType.SLASH) {

src/com/plasstech/lang/c/typecheck/Initializer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
public record Initializer(StaticInit staticInit) implements InitialValue {
77
public static Initializer of(long value, Type type) {
88
if (type.equals(Type.INT)) {
9-
return new Initializer(new IntInit((int) value));
9+
return new Initializer(new IntInit(value));
1010
}
1111
if (type.equals(Type.LONG)) {
1212
return new Initializer(new LongInit(value));
1313
}
1414
// Not sure if this is right. Page 280
1515
if (type.equals(Type.UNSIGNED_INT)) {
16-
return new Initializer(new UIntInit((int) value));
16+
return new Initializer(new UIntInit(value));
1717
}
1818
if (type.equals(Type.UNSIGNED_LONG)) {
1919
return new Initializer(new ULongInit(value));

src/com/plasstech/lang/c/typecheck/IntInit.java

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.plasstech.lang.c.typecheck;
22

33
public record IntInit(int value) implements StaticInit {
4+
public IntInit(long lvalue) {
5+
this((int) (lvalue % 4294967296L));
6+
}
7+
48
@Override
59
public long valueAsLong() {
610
return value;

src/com/plasstech/lang/c/typecheck/UIntInit.java

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.plasstech.lang.c.typecheck;
22

33
public record UIntInit(int value) implements StaticInit {
4+
public UIntInit(long lvalue) {
5+
this((int) (lvalue - 4294967296L));
6+
}
7+
48
@Override
59
public long valueAsLong() {
610
return value;

0 commit comments

Comments
 (0)