diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/codegen.zig | 44 |
1 files changed, 26 insertions, 18 deletions
diff --git a/src/codegen.zig b/src/codegen.zig index d463122..f734018 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -1,11 +1,19 @@ const std = @import("std"); -const llvm = @import("llvm"); -const target_m = llvm.target_machine; -const target = llvm.target; -const types = llvm.types; -const core = llvm.core; -const analysis = llvm.analysis; +const llvm = @cImport({ + @cInclude("llvm-c/Core.h"); + @cInclude("llvm-c/TargetMachine.h"); + @cInclude("llvm-c/Types.h"); + @cInclude("llvm-c/Analysis.h"); + @cInclude("llvm-c/Target.h"); +}); + +// Create aliases for clarity +const core = llvm; +const target_m = llvm; +const types = llvm; +const analysis = llvm; +const target = llvm; const parser = @import("parser.zig"); @@ -74,9 +82,9 @@ pub const CodeGen = struct { triple, "", "", - types.LLVMCodeGenOptLevel.LLVMCodeGenLevelDefault, - types.LLVMRelocMode.LLVMRelocDefault, - types.LLVMCodeModel.LLVMCodeModelDefault, + types.LLVMCodeGenLevelDefault, + types.LLVMRelocDefault, + types.LLVMCodeModelDefault, ); // Generate the object file @@ -85,12 +93,12 @@ pub const CodeGen = struct { target_machine, self.llvm_module, filename, - types.LLVMCodeGenFileType.LLVMObjectFile, + types.LLVMObjectFile, null, ); std.debug.print("Object file generated: {s}\n", .{filename}); - _ = analysis.LLVMVerifyModule(self.llvm_module, types.LLVMVerifierFailureAction.LLVMAbortProcessAction, &message.?); + _ = analysis.LLVMVerifyModule(self.llvm_module, types.LLVMAbortProcessAction, &message.?); // std.debug.print("Verification output: {any}.\n", .{message}); // core.LLVMDisposeMessage(message); @@ -158,7 +166,7 @@ pub const CodeGen = struct { .PRIMARY_EXPRESSION => |primary_expression| { std.debug.assert(primary_expression == .IDENTIFIER); function = self.environment.get_variable(primary_expression.IDENTIFIER.name) orelse return CodeGenError.CompilationError; - if (core.LLVMGetValueKind(function.value) != types.LLVMValueKind.LLVMFunctionValueKind) { + if (core.LLVMGetValueKind(function.value) != types.LLVMFunctionValueKind) { function.value = core.LLVMBuildLoad2(self.builder, core.LLVMPointerType(function.type, 0), function.value, ""); } }, @@ -353,7 +361,7 @@ pub const CodeGen = struct { .IDENTIFIER => |i| { const variable = self.environment.get_variable(i.name).?; var param_type = variable.type; - if (core.LLVMGetTypeKind(param_type.?) == types.LLVMTypeKind.LLVMFunctionTypeKind) { + if (core.LLVMGetTypeKind(param_type.?) == types.LLVMFunctionTypeKind) { param_type = core.LLVMPointerType(param_type.?, 0); } const loaded = core.LLVMBuildLoad2(self.builder, param_type, variable.value, ""); @@ -401,7 +409,7 @@ pub const CodeGen = struct { switch (exp.negation) { true => { std.debug.assert(k.type == core.LLVMInt1Type()); - r = core.LLVMBuildICmp(self.builder, types.LLVMIntPredicate.LLVMIntEQ, k.value, core.LLVMConstInt(core.LLVMInt1Type(), 0, 0), ""); + r = core.LLVMBuildICmp(self.builder, types.LLVMIntEQ, k.value, core.LLVMConstInt(core.LLVMInt1Type(), 0, 0), ""); t = core.LLVMInt1Type(); }, false => { @@ -416,10 +424,10 @@ pub const CodeGen = struct { const lhs_value = try self.generate_expression_value(exp.lhs, null); const rhs_value = try self.generate_expression_value(exp.rhs, null); - const op = switch (exp.typ) { - .EQ => types.LLVMIntPredicate.LLVMIntEQ, - .LT => types.LLVMIntPredicate.LLVMIntSLT, - .GT => types.LLVMIntPredicate.LLVMIntSGT, + const op: c_uint = switch (exp.typ) { + .EQ => types.LLVMIntEQ, + .LT => types.LLVMIntSLT, + .GT => types.LLVMIntSGT, }; const cmp = core.LLVMBuildICmp(self.builder, op, lhs_value.value, rhs_value.value, ""); |