-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathOperand.java
More file actions
150 lines (130 loc) · 4.53 KB
/
Operand.java
File metadata and controls
150 lines (130 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.compilerprogramming.ezlang.compiler;
import com.compilerprogramming.ezlang.types.Symbol;
import com.compilerprogramming.ezlang.types.EZType;
import java.util.Objects;
public class Operand {
EZType type;
public static class ConstantOperand extends Operand {
public final long value;
public ConstantOperand(long value, EZType type) {
this.value = value;
this.type = type;
}
@Override
public String toString() {
return String.valueOf(value);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ConstantOperand that = (ConstantOperand) o;
return value == that.value;
}
@Override
public int hashCode() {
return Objects.hashCode(value);
}
}
public static class NullConstantOperand extends Operand {
public NullConstantOperand(EZType type) {
this.type = type;
}
@Override
public String toString() {
return "null";
}
}
public static class RegisterOperand extends Operand {
final Register reg;
protected RegisterOperand(Register reg) {
this.reg = reg;
if (reg == null)
throw new NullPointerException();
}
public int frameSlot() { return reg.frameSlot(); }
public RegisterOperand copy(Register register) {
return new RegisterOperand(register);
}
@Override
public String toString() {
return reg.name();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RegisterOperand operand = (RegisterOperand) o;
return Objects.equals(reg, operand.reg);
}
@Override
public int hashCode() {
return Objects.hashCode(reg);
}
}
public static class LocalRegisterOperand extends RegisterOperand {
Symbol.VarSymbol variable;
public LocalRegisterOperand(Register reg, Symbol.VarSymbol variable) {
super(reg);
this.variable = variable;
}
@Override
public RegisterOperand copy(Register register) {
return new LocalRegisterOperand(register, variable);
}
}
public static class LocalFunctionOperand extends Operand {
public final EZType.EZTypeFunction functionType;
public LocalFunctionOperand(EZType.EZTypeFunction functionType) {
this.functionType = functionType;
}
@Override
public String toString() {
return functionType.toString();
}
}
/**
* Represents a temp register, maps to a location on the
* virtual stack. Temps start at offset 0, but this is a relative
* register number from start of temp area.
*/
public static class TempRegisterOperand extends RegisterOperand {
public TempRegisterOperand(Register reg) {
super(reg);
}
@Override
public RegisterOperand copy(Register register) {
return new TempRegisterOperand(register);
}
}
public static class IndexedOperand extends Operand {}
public static class LoadIndexedOperand extends IndexedOperand {
public final Operand arrayOperand;
public final Operand indexOperand;
public LoadIndexedOperand(Operand arrayOperand, Operand indexOperand) {
this.arrayOperand = arrayOperand;
this.indexOperand = indexOperand;
assert !(indexOperand instanceof IndexedOperand) &&
!(arrayOperand instanceof IndexedOperand);
}
@Override
public String toString() {
return arrayOperand + "[" + indexOperand + "]";
}
}
public static class LoadFieldOperand extends IndexedOperand {
public final Operand structOperand;
public final int fieldIndex;
public final String fieldName;
public LoadFieldOperand(Operand structOperand, String fieldName, int field) {
this.structOperand = structOperand;
this.fieldName = fieldName;
this.fieldIndex = field;
assert !(structOperand instanceof IndexedOperand);
}
@Override
public String toString() {
return structOperand + "." + fieldName;
}
}
}