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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
const std = @import("std");
const parse = @import("parser.zig");
const symb = @import("symtable.zig");
const llvm = @import("llvm");
const analysis = llvm.analysis;
const core = llvm.core;
const target = llvm.target;
const types = llvm.types;
const CodegenError = error{
Immutable,
OutOfMemory,
IncorrectType,
};
fn toLLVMtype(typ: parse.TypeIdent, sym: *symb.SymbolTable, expr: ?parse.NodeExpr) types.LLVMTypeRef {
_ = expr;
if (sym.getType(typ)) |t| {
return switch (t) {
.Integer => core.LLVMInt32Type(),
.String => core.LLVMPointerType(core.LLVMInt8Type(), 0),
.Void => core.LLVMVoidType(),
else => core.LLVMVoidType(),
};
}
return core.LLVMVoidType();
}
pub const Generator = struct {
root: parse.NodeStmt,
allocator: std.mem.Allocator,
builder: types.LLVMBuilderRef,
context: types.LLVMContextRef,
module: types.LLVMModuleRef,
currentFunc: ?types.LLVMValueRef,
currentFuncIsVoid: bool,
references: std.AutoHashMap(u32, types.LLVMValueRef),
stringId: u32,
pub fn init(allocator: std.mem.Allocator, root: parse.NodeStmt, filename: [*:0]const u8) Generator {
_ = target.LLVMInitializeNativeTarget();
_ = target.LLVMInitializeNativeAsmPrinter();
_ = target.LLVMInitializeNativeAsmParser();
const context = core.LLVMContextCreate();
const builder = core.LLVMCreateBuilderInContext(context);
const module = core.LLVMModuleCreateWithNameInContext(filename, context);
return .{
.root = root,
.allocator = allocator,
.builder = builder,
.context = context,
.module = module,
.currentFunc = null,
.currentFuncIsVoid = false,
.references = std.AutoHashMap(u32, types.LLVMValueRef).init(allocator),
.stringId = 0,
};
}
pub fn deinit(self: *Generator) void {
// Shutdown LLVM
defer core.LLVMShutdown();
defer core.LLVMDisposeModule(self.module);
defer core.LLVMDisposeBuilder(self.builder);
//self.code.deinit();
}
fn genExit(self: *Generator, exit: parse.NodeExit) !void {
const expr = exit;
const val = try self.genExpr(expr);
_ = core.LLVMBuildRet(self.builder, val);
}
fn genVar(self: *Generator, stmt: parse.NodeStmt) !void {
const nodeVar = stmt.kind.defVar;
const table = stmt.symtable;
const symbol = table.getValue(nodeVar.ident.ident).?;
const value = try self.genExpr(nodeVar.expr);
const ptr = try self.genAlloc(toLLVMtype(nodeVar.expr.typ orelse try nodeVar.expr.symtable.getValue(nodeVar.ident.ident).?.typ.toTypeIdent(self.allocator), table, nodeVar.expr).?, nodeVar.ident.ident);
_ = core.LLVMBuildStore(self.builder, value, ptr);
try self.references.put(symbol.id, ptr);
}
fn genValue(self: *Generator, stmt: parse.NodeStmt) !void {
const nodeVar = stmt.kind.defValue;
const table = stmt.symtable;
const symbol = table.getValue(nodeVar.ident.ident).?;
const ptr = try self.genAlloc(toLLVMtype(nodeVar.expr.typ.?, table, nodeVar.expr), nodeVar.ident.ident);
const value = try self.genExpr(nodeVar.expr);
_ = core.LLVMBuildStore(self.builder, value, ptr);
try self.references.put(symbol.id, ptr);
}
fn genAlloc(self: *Generator, typ: types.LLVMTypeRef, ident: []const u8) !types.LLVMValueRef {
const builder = core.LLVMCreateBuilderInContext(self.context);
const entryFunc = self.currentFunc.?;
const entry = core.LLVMGetFirstBasicBlock(entryFunc).?;
if (core.LLVMGetFirstInstruction(entry)) |first| {
core.LLVMPositionBuilderBefore(builder, first);
} else {
core.LLVMPositionBuilderAtEnd(builder, entry);
}
const str: [*:0]const u8 = try self.allocator.dupeZ(u8, ident);
return core.LLVMBuildAlloca(builder, typ, str);
}
fn asBasicType(typ: symb.SymbType) ?types.LLVMTypeKind {
return switch (typ) {
.Integer => types.LLVMTypeKind.LLVMIntegerTypeKind,
else => null,
};
}
fn genAssign(self: *Generator, stmt: parse.NodeStmt) !void {
// std.debug.print("assign\n", .{});
const table = stmt.symtable;
const symbol = table.get(stmt.kind.assignVar.ident.ident).?;
if (!symbol.Value.mut) return CodegenError.Immutable;
const ptr = self.references.get(symbol.Value.id).?;
const value = try self.genExpr(stmt.kind.assignVar.expr);
_ = core.LLVMBuildStore(self.builder, value, ptr);
}
fn genBlock(self: *Generator, block: []const parse.NodeStmt) CodegenError!void {
for (block) |stmt| try self.genStmt(stmt);
}
fn genFunc(self: *Generator, stmt: parse.NodeStmt) !void {
self.references.clearAndFree();
const fun = stmt.kind.function;
var table: *symb.SymbolTable = stmt.symtable;
var block: *parse.NodeStmt = undefined;
var codeSlice: []const parse.NodeStmt = undefined;
if (fun.block != null) {
table = fun.block.?.symtable;
block = fun.block.?;
codeSlice = block.kind.block;
}
const funcName: [*:0]const u8 = try self.allocator.dupeZ(u8, fun.ident.ident);
const retType = toLLVMtype(fun.retType.?, table, null);
var params = std.ArrayList(types.LLVMTypeRef).init(self.allocator);
for (fun.args) |arg| {
try params.append(toLLVMtype(arg.typ, table, null));
}
const funcType = core.LLVMFunctionType(retType, @ptrCast(params.items), @intCast(params.items.len), 0);
const func = core.LLVMAddFunction(self.module, funcName, funcType);
for (fun.args, 0..) |arg, i| {
const symbol = table.get(arg.ident).?;
const ptr: types.LLVMValueRef = core.LLVMGetParam(func, @intCast(i));
try self.references.put(symbol.Value.id, ptr);
}
if (fun.block != null) {
self.currentFunc = func;
self.currentFuncIsVoid = switch (table.getType(fun.retType.?).?) {
.Void => true,
else => false,
};
const function: types.LLVMValueRef = func;
const codeBlock = core.LLVMAppendBasicBlockInContext(self.context, function, "entry");
core.LLVMPositionBuilderAtEnd(self.builder, codeBlock);
const bodyTable = block.symtable;
_ = bodyTable;
try self.genBlock(codeSlice);
_ = if (self.currentFuncIsVoid) core.LLVMBuildRetVoid(self.builder);
}
}
fn genStmt(self: *Generator, stmt: parse.NodeStmt) !void {
try switch (stmt.kind) {
.exit => |expr| self.genExit(expr),
.function => self.genFunc(stmt),
.defValue => self.genValue(stmt),
.defVar => self.genVar(stmt),
.assignVar => self.genAssign(stmt),
.expr => |expression| {
_ = try self.genExpr(expression);
},
else => {},
};
}
fn genExpr(self: *Generator, expr: parse.NodeExpr) !types.LLVMValueRef {
return switch (expr.kind) {
.ident => |id| blk: {
// std.debug.print("getValue({s})\n", .{id.ident});
const table = expr.symtable;
// std.debug.print("\n\nEXPERTABLE\n\n", .{});
// var iterTable = table.scope.?.symbs.iterator();
// while (iterTable.next()) |entry| {
// // std.debug.print("{s} -> {any}\n", .{ entry.key_ptr.*, entry.value_ptr.* });
// }
// std.debug.print("\n\nEXPERTABLE\n\n", .{});
const symbol = table.getValue(id.ident).?;
const ptr = self.references.get(symbol.id).?;
if (core.LLVMIsAArgument(ptr)) |_|
break :blk ptr;
break :blk core.LLVMBuildLoad2(
self.builder,
toLLVMtype(
expr.typ orelse try table.getValue(id.ident).?.typ.toTypeIdent(self.allocator),
table,
expr,
),
ptr,
"",
);
},
.intLit => |int| core.LLVMConstInt(core.LLVMInt32TypeInContext(self.context), @intCast(int.intLit), 1),
.stringLit => |str| blk: {
const vref = core.LLVMAddGlobal(
self.module,
core.LLVMArrayType(core.LLVMInt8Type(), @intCast(str.stringLit.len + 1)),
try self.allocator.dupeZ(u8, try std.fmt.allocPrint(
self.allocator,
".str.{d}",
.{self.stringId},
)),
);
self.stringId += 1;
const sref = core.LLVMConstString(try self.allocator.dupeZ(u8, str.stringLit), @intCast(str.stringLit.len), 0);
core.LLVMSetInitializer(vref, sref);
core.LLVMSetGlobalConstant(vref, 1);
core.LLVMSetLinkage(vref, .LLVMPrivateLinkage);
core.LLVMSetUnnamedAddr(vref, 1);
break :blk vref;
},
.call => |call| blk: {
std.debug.print("Function {s} requested\n", .{call.ident.ident});
const functype = expr.symtable.getValue(call.ident.ident).?.typ.Function;
const ident = try self.allocator.dupeZ(u8, call.ident.ident);
const function = core.LLVMGetNamedFunction(self.module, ident);
var args = std.ArrayList(types.LLVMValueRef).init(self.allocator);
for (call.args, functype.input) |arg, intype| {
if (!std.meta.eql(expr.symtable.getType(arg.typ.?).?, intype)) return CodegenError.IncorrectType;
try args.append(try self.genExpr(arg));
}
const funcType = core.LLVMGlobalGetValueType(function);
// std.debug.print("FUNCTYPE: {s}\n", .{call.ident.ident});
const llvmCall = core.LLVMBuildCall2(
self.builder,
funcType,
function,
@ptrCast(args.items),
@intCast(call.args.len),
ident,
);
// std.debug.print("CALL\n", .{});
break :blk llvmCall;
},
};
}
pub fn generate(self: *Generator) ![]const u8 {
try switch (self.root.kind) {
.block => |b| {
for (b) |stmt|
try self.genStmt(stmt);
},
else => error.InvalidTop,
};
const string: []const u8 = std.mem.span(core.LLVMPrintModuleToString(self.module));
return string;
}
};
test "Codegen exit" {
const tok = @import("tokenize.zig");
const expect = std.testing.expect;
const main = @import("main.zig");
const src =
\\fn main() -> i32 {
\\ return 7;
\\}
;
const expected =
\\; ModuleID = '_calico_start'
\\source_filename = "_calico_start"
\\
\\define i32 @main() {
\\entry:
\\ ret i32 7
\\}
\\
;
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
var tokenizer = tok.Tokenizer.init(allocator, src);
defer tokenizer.deinit();
const tokens = try tokenizer.tokenize();
const symbTable = try main.initSymbolTable(arena.allocator());
var parser = parse.Parser.init(arena.allocator(), tokens, symbTable);
const tree = try parser.parse();
var treeNode = tree.asNode();
var pop = symb.Populator.init(arena.allocator());
try pop.populateSymtable(&treeNode);
var generator = Generator.init(arena.allocator(), tree, "_calico_start");
defer generator.deinit();
const code = try generator.generate();
try expect(std.mem.eql(u8, code, expected));
}
test "Codegen assign" {
const tok = @import("tokenize.zig");
const expect = std.testing.expect;
const main = @import("main.zig");
const src =
\\fn main() -> i32 {
\\ const testval = 6;
\\ varbl testvar = testval;
\\ testvar = 5;
\\ return testvar;
\\}
;
const expected =
\\; ModuleID = '_calico_start'
\\source_filename = "_calico_start"
\\
\\define i32 @main() {
\\entry:
\\ %testvar = alloca i32, align 4
\\ %testval = alloca i32, align 4
\\ store i32 6, ptr %testval, align 4
\\ %0 = load i32, ptr %testval, align 4
\\ store i32 %0, ptr %testvar, align 4
\\ store i32 5, ptr %testvar, align 4
\\ %1 = load i32, ptr %testvar, align 4
\\ ret i32 %1
\\}
\\
;
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
var tokenizer = tok.Tokenizer.init(allocator, src);
defer tokenizer.deinit();
const tokens = try tokenizer.tokenize();
const symbTable = try main.initSymbolTable(arena.allocator());
var parser = parse.Parser.init(arena.allocator(), tokens, symbTable);
const tree = try parser.parse();
var treeNode = tree.asNode();
var pop = symb.Populator.init(arena.allocator());
try pop.populateSymtable(&treeNode);
var generator = Generator.init(arena.allocator(), tree, "_calico_start");
defer generator.deinit();
const code = try generator.generate();
try expect(std.mem.eql(u8, code, expected));
}
test "Codegen assign constant" {
const tok = @import("tokenize.zig");
const main = @import("main.zig");
const src =
\\fn main() -> i32 {
\\ const testval = 6;
\\ const testvar = testval;
\\ testvar = 5;
\\ return testvar;
\\}
;
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
var tokenizer = tok.Tokenizer.init(allocator, src);
defer tokenizer.deinit();
const tokens = try tokenizer.tokenize();
const symbTable = try main.initSymbolTable(arena.allocator());
var parser = parse.Parser.init(arena.allocator(), tokens, symbTable);
const tree = try parser.parse();
var treeNode = tree.asNode();
var pop = symb.Populator.init(arena.allocator());
try pop.populateSymtable(&treeNode);
var generator = Generator.init(arena.allocator(), tree, "_calico_start");
defer generator.deinit();
const code = generator.generate();
try std.testing.expectError(CodegenError.Immutable, code);
}
test "Codegen extern fn string" {
const tok = @import("tokenize.zig");
const expect = std.testing.expect;
const main = @import("main.zig");
const src =
\\import fn puts(str: [u8]) -> i32;
\\fn main() -> i32 {
\\ puts("Hello World!");
\\}
;
const expected =
\\; ModuleID = '_calico_start'
\\source_filename = "_calico_start"
\\
\\@.str.0 = private unnamed_addr constant [13 x i8] c"Hello World!\00"
\\
\\declare i32 @puts(ptr)
\\
\\define i32 @main() {
\\entry:
\\ %puts = call i32 @puts(ptr @.str.0)
\\}
\\
;
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
var tokenizer = tok.Tokenizer.init(allocator, src);
defer tokenizer.deinit();
const tokens = try tokenizer.tokenize();
const symbTable = try main.initSymbolTable(arena.allocator());
var parser = parse.Parser.init(arena.allocator(), tokens, symbTable);
const tree = try parser.parse();
var treeNode = tree.asNode();
var pop = symb.Populator.init(arena.allocator());
try pop.populateSymtable(&treeNode);
var generator = Generator.init(arena.allocator(), tree, "_calico_start");
defer generator.deinit();
const code = try generator.generate();
try expect(std.mem.eql(u8, code, expected));
}
|