-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen.zig
More file actions
165 lines (150 loc) · 5.77 KB
/
codegen.zig
File metadata and controls
165 lines (150 loc) · 5.77 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const std = @import("std");
const eql = std.mem.eql;
const CodeError = error{
InvalidDestCode,
InvalidCompCode,
InvalidJumpCode,
InvalidAInstruction,
InvalidCInstruction,
};
pub const Codegen = struct {
dest_code: [3]u8 = undefined,
comp_code: [7]u8 = undefined,
jump_code: [3]u8 = undefined,
a_code: [16]u8 = undefined,
c_code: [16]u8 = undefined,
pub fn init() Codegen {
return Codegen{};
}
pub fn dest(self: *Codegen, str_optional: ?[]const u8) !void {
if (str_optional == null) {
self.dest_code = "000".*;
return;
}
const str = str_optional.?;
if (eql(u8, str, "")) {
self.dest_code = "000".*;
} else if (eql(u8, str, "M")) {
self.dest_code = "001".*;
} else if (eql(u8, str, "D")) {
self.dest_code = "010".*;
} else if (eql(u8, str, "DM") or eql(u8, str, "MD")) { // WTF? "MD" is not specified in books.. but it is needed
self.dest_code = "011".*;
} else if (eql(u8, str, "A")) {
self.dest_code = "100".*;
} else if (eql(u8, str, "AM")) {
self.dest_code = "101".*;
} else if (eql(u8, str, "AD")) {
self.dest_code = "110".*;
} else if (eql(u8, str, "ADM")) {
self.dest_code = "111".*;
} else {
std.debug.print("Invalid Destcode : {s} : {s}\n", .{ str, self.dest_code });
return CodeError.InvalidDestCode;
}
}
pub fn comp(self: *Codegen, str_optional: ?[]const u8) !void {
// 7-bit output
if (str_optional == null) return CodeError.InvalidCompCode;
const str = str_optional.?;
if (eql(u8, str, "0")) {
self.comp_code = "0101010".*;
} else if (eql(u8, str, "1")) {
self.comp_code = "0111111".*;
} else if (eql(u8, str, "-1")) {
self.comp_code = "0111010".*;
} else if (eql(u8, str, "D")) {
self.comp_code = "0001100".*;
} else if (eql(u8, str, "A")) {
self.comp_code = "0110000".*;
} else if (eql(u8, str, "M")) {
self.comp_code = "1110000".*;
} else if (eql(u8, str, "!D")) {
self.comp_code = "0001101".*;
} else if (eql(u8, str, "!A")) {
self.comp_code = "0110001".*;
} else if (eql(u8, str, "!M")) {
self.comp_code = "1110001".*;
} else if (eql(u8, str, "-D")) {
self.comp_code = "0001111".*;
} else if (eql(u8, str, "-A")) {
self.comp_code = "0110011".*;
} else if (eql(u8, str, "-M")) {
self.comp_code = "1110011".*;
} else if (eql(u8, str, "D+1")) {
self.comp_code = "0011111".*;
} else if (eql(u8, str, "A+1")) {
self.comp_code = "0110111".*;
} else if (eql(u8, str, "M+1")) {
self.comp_code = "1110111".*;
} else if (eql(u8, str, "D-1")) {
self.comp_code = "0001110".*;
} else if (eql(u8, str, "A-1")) {
self.comp_code = "0110010".*;
} else if (eql(u8, str, "M-1")) {
self.comp_code = "1110010".*;
} else if (eql(u8, str, "D+A")) {
self.comp_code = "0000010".*;
} else if (eql(u8, str, "D+M")) {
self.comp_code = "1000010".*;
} else if (eql(u8, str, "D-A")) {
self.comp_code = "0010011".*;
} else if (eql(u8, str, "D-M")) {
self.comp_code = "1010011".*;
} else if (eql(u8, str, "A-D")) {
self.comp_code = "0000111".*;
} else if (eql(u8, str, "M-D")) {
self.comp_code = "1000111".*;
} else if (eql(u8, str, "D&A")) {
self.comp_code = "0000000".*;
} else if (eql(u8, str, "D&M")) {
self.comp_code = "1000000".*;
} else if (eql(u8, str, "D|A")) {
self.comp_code = "0010101".*;
} else if (eql(u8, str, "D|M")) {
self.comp_code = "1010101".*;
} else return CodeError.InvalidCompCode;
}
pub fn jump(self: *Codegen, str_optional: ?[]const u8) !void {
// 3-bit output
if (str_optional == null) {
self.jump_code = "000".*;
return;
}
const str = str_optional.?;
if (eql(u8, str, "")) {
self.jump_code = "000".*;
} else if (eql(u8, str, "JGT")) {
self.jump_code = "001".*;
} else if (eql(u8, str, "JEQ")) {
self.jump_code = "010".*;
} else if (eql(u8, str, "JGE")) {
self.jump_code = "011".*;
} else if (eql(u8, str, "JLT")) {
self.jump_code = "100".*;
} else if (eql(u8, str, "JNE")) {
self.jump_code = "101".*;
} else if (eql(u8, str, "JLE")) {
self.jump_code = "110".*;
} else if (eql(u8, str, "JMP")) {
self.jump_code = "111".*;
} else return CodeError.InvalidJumpCode;
}
pub fn gen_a_inst(self: *Codegen, symbol: anytype) !void {
// version 1 ==> only numeric symbol passsed
const value = switch (@TypeOf(symbol)) {
// since we read symbol in []u8, we should parse it.
[]const u8 => try std.fmt.parseInt(u32, symbol, 10),
u32 => symbol,
else => @compileError("invalid symbol format"),
};
// for this
const written = try std.fmt.bufPrint(&self.a_code, "{b:0>16}", .{value});
if (written.len != 16) return CodeError.InvalidAInstruction;
}
pub fn gen_c_inst(self: *Codegen) !void {
const instruction = ("111" ++ self.comp_code ++ self.dest_code ++ self.jump_code).*;
const written = try std.fmt.bufPrint(&self.c_code, "{s}", .{instruction});
if (written.len != 16) return CodeError.InvalidCInstruction;
}
};