Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 42 additions & 8 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { rmSync } from "fs"
import { Lexer } from "./src/lexer"
import { Parser } from "./src/parser"
import { SemanticAnalyzer } from "./src/semantic/analysis"
import { TypeChecker } from "./src/semantic/typing"
import { TypeChecker } from "./src/semantic/checker"
import { Codegen } from "./src/gen"
import { TypeInferrer } from "./src/semantic/inferrer"
rmSync("./out", { recursive: true, force: true })

// error driven development right here
Expand Down Expand Up @@ -39,8 +41,12 @@ rmSync("./out", { recursive: true, force: true })

const program = `
fn main() {
let x := true
let y := 3 + x
return 0
}

fn hello() {
let b: int= 3
return 2
}
`.trim()

Expand All @@ -51,7 +57,7 @@ const t = l.lex()
const lexerTime = performance.now()
await Bun.write("out/tok.json", JSON.stringify(t, null, 2))

console.log(`lexed ${t.length} tokens in ${(lexerTime - start).toFixed(3)}ms`)
console.log(`lexed ${program.length} characters in ${(lexerTime - start).toFixed(3)}ms into ${t.length} tokens`)

const p = new Parser(t)
const ast = p.parse()
Expand All @@ -63,12 +69,40 @@ console.log(`parsed ${t.length} tokens in ${(astTime - lexerTime).toFixed(3)}ms,
const sa = new SemanticAnalyzer(ast)
const analyzed = sa.analyze()

const tc = new TypeChecker(ast)
tc.check()
const ti = new TypeInferrer(analyzed)
const inferred = ti.infer()

const tc = new TypeChecker(inferred)
const checked = tc.check()

await Bun.write("out/semantic.json", JSON.stringify(analyzed, null, 2))
await Bun.write("out/semantic.json", JSON.stringify(checked, null, 2))
const semanticTime = performance.now()

console.log(`semantic analysis done in ${(semanticTime - astTime).toFixed(3)}ms`)

console.log(`total time: ${(semanticTime - start).toFixed(3)}ms`)
const cg = new Codegen("my_module", ast)
const llvmIr = cg.generate()
await Bun.write("out/module.ll", llvmIr)

console.log("")

Bun.write("out/module.ll", llvmIr)
const clang = Bun.spawn({
cmd: ["clang", "-o", "out/program.exe", "out/module.ll"],
})
await clang.exited

const cgTime = performance.now()


console.log(`codegen done in ${(cgTime - semanticTime).toFixed(3)}ms`)
console.log(`total time: ${(cgTime - start).toFixed(3)}ms`)
console.log("")

const exe = Bun.spawn({
cmd: ["out/program.exe"],
})

await exe.exited

console.log(`program exited with code ${exe.exitCode}`)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"devDependencies": {
"@types/bun": "^1.2.19"
"@types/bun": "^1.3.13"
},
"dependencies": {
"cmake-js": "^7.3.1"
Expand Down
16 changes: 8 additions & 8 deletions src/bindings/ffi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { platform } from "os";
const location = platform() === "win32" ? "LLVM-C.dll" : "libLLVM-C.so";

const lib = dlopen(location, {
LLVMConstStringInContext: { args: ["ptr", "cstring", "uint32_t", "bool"], returns: "ptr" },
LLVMArrayType: { args: ["ptr", "uint32_t"], returns: "ptr" },
LLVMConstStringInContext2: { args: ["ptr", "cstring", "uint64_t", "bool"], returns: "ptr" },
LLVMArrayType2: { args: ["ptr", "uint64_t"], returns: "ptr" },
LLVMAddGlobal: { args: ["ptr", "ptr", "cstring"], returns: "ptr" },
LLVMSetInitializer: { args: ["ptr", "ptr"], returns: "void" },
LLVMSetGlobalConstant: { args: ["ptr", "bool"], returns: "void" },
Expand All @@ -31,7 +31,7 @@ const lib = dlopen(location, {
LLVMDoubleTypeInContext: { args: ["ptr"], returns: "ptr" },

LLVMVoidTypeInContext: { args: ["ptr"], returns: "ptr" },
LLVMPointerType: { args: ["ptr", "uint32_t"], returns: "ptr" },
LLVMPointerTypeInContext: { args: ["ptr", "uint32_t"], returns: "ptr" },

// functions & blocks
LLVMFunctionType: { args: ["ptr", "ptr", "uint32_t", "bool"], returns: "ptr" },
Expand Down Expand Up @@ -59,7 +59,7 @@ const lib = dlopen(location, {
// memory
LLVMBuildAlloca: { args: ["ptr", "ptr", "cstring"], returns: "ptr" },
LLVMBuildStore: { args: ["ptr", "ptr", "ptr"], returns: "ptr" },
LLVMBuildLoad: { args: ["ptr", "ptr", "cstring"], returns: "ptr" },
LLVMBuildLoad2: { args: ["ptr", "ptr", "ptr", "cstring"], returns: "ptr" },

// constants
LLVMConstInt: { args: ["ptr", "uint64_t", "bool"], returns: "ptr" },
Expand Down Expand Up @@ -90,7 +90,7 @@ export const {
LLVMFloatTypeInContext,
LLVMDoubleTypeInContext,
LLVMVoidTypeInContext,
LLVMPointerType,
LLVMPointerTypeInContext,

LLVMFunctionType,
LLVMAddFunction,
Expand Down Expand Up @@ -123,13 +123,13 @@ export const {
LLVMGetIntTypeWidth,
LLVMBuildAlloca,
LLVMBuildStore,
LLVMBuildLoad,
LLVMBuildLoad2,
LLVMGetNamedFunction,
LLVMBuildCall2,
LLVMBuildICmp,
LLVMGetInsertBlock,
LLVMConstStringInContext,
LLVMArrayType,
LLVMConstStringInContext2,
LLVMArrayType2,
LLVMAddGlobal,
LLVMSetInitializer,
LLVMSetGlobalConstant,
Expand Down
27 changes: 14 additions & 13 deletions src/bindings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
LLVMAddFunction,
LLVMAddGlobal,
LLVMAppendBasicBlockInContext,
LLVMArrayType,
LLVMArrayType2,
LLVMBuildAdd,
LLVMBuildAlloca,
LLVMBuildBitCast,
Expand All @@ -14,7 +14,7 @@ import {
LLVMBuildFMul,
LLVMBuildFSub,
LLVMBuildICmp,
LLVMBuildLoad,
LLVMBuildLoad2,
LLVMBuildMul,
LLVMBuildRet,
LLVMBuildSDiv,
Expand All @@ -23,7 +23,7 @@ import {
LLVMBuildUDiv,
LLVMConstInt,
LLVMConstReal,
LLVMConstStringInContext,
LLVMConstStringInContext2,
LLVMContextCreate,
LLVMCreateBuilderInContext,
LLVMDeleteBasicBlock,
Expand All @@ -40,7 +40,7 @@ import {
LLVMInt64TypeInContext,
LLVMInt8TypeInContext,
LLVMModuleCreateWithNameInContext,
LLVMPointerType,
LLVMPointerTypeInContext,
LLVMPositionBuilderAtEnd,
LLVMPrintModuleToString,
LLVMSetGlobalConstant,
Expand Down Expand Up @@ -96,12 +96,12 @@ private _funcs: Map<string, Func> = new Map();
const ctx = this.getContext();
const i8 = Type.int8(ctx);
const strBytes = Buffer.from(value + "\0");
const strConst = LLVMConstStringInContext(ctx.handle, strBytes, strBytes.length, 1);
const arrType = LLVMArrayType(i8.handle, strBytes.length);
const strConst = LLVMConstStringInContext2(ctx.handle, strBytes, BigInt(strBytes.length), 1);
const arrType = LLVMArrayType2(i8.handle, BigInt(strBytes.length));
const global = LLVMAddGlobal(this.ptr, arrType, Buffer.from(name + "\0"));
LLVMSetInitializer(global, strConst);
LLVMSetGlobalConstant(global, true);
return new Value(global, Type.pointer(i8));
return new Value(global, Type.pointer(ctx));
}

/**
Expand Down Expand Up @@ -437,12 +437,13 @@ export class IRBuilder {

/**
load a value from memory
@param type the type of the value to load
@param ptr the pointer to load from
@param name variable name
@returns the loaded value
*/
load(ptr: Value, name = "load"): Value {
return new Value(LLVMBuildLoad(this.ptr, ptr.handle, Buffer.from(name + "\0")));
load(type: Type, ptr: Value, name = "load"): Value {
return new Value(LLVMBuildLoad2(this.ptr, type.handle, ptr.handle, Buffer.from(name + "\0")), type);
}

/**
Expand Down Expand Up @@ -691,12 +692,12 @@ export class Type {
}

/**
get pointer type
@param elementType type to point to
get pointer type (opaque pointer in LLVM 21)
@param context LLVM context
@param addressSpace address space
*/
static pointer(elementType: Type, addressSpace = 0): Type {
return new Type(LLVMPointerType(elementType.handle, addressSpace), "pointer", undefined, elementType);
static pointer(context: Context, addressSpace = 0): Type {
return new Type(LLVMPointerTypeInContext(context.handle, addressSpace), "pointer");
}

/**
Expand Down
77 changes: 77 additions & 0 deletions src/gen/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import VM, { Context, Func, FunctionType, IRBuilder, Linkage, Module, Type, Value } from "../bindings";

export class LLVMHelper {
public name: string;

public ctx: Context;
public mod: Module;
public builder: IRBuilder;

// currentFunction: Func | null = null;

constructor(name: string) {
this.name = name

this.ctx = new VM.Context();
this.mod = new VM.Module(name, this.ctx);
this.builder = new VM.IRBuilder(this.ctx);
}

fn(name: string, fnType: FunctionType, opts?: {
linkage?: Linkage;
extern?: boolean;
}): Func {
let func = this.mod.getFunction(name);
if (func) return func;
func = this.mod.createFunction(name, fnType, opts ?? { linkage: Linkage.External, extern: false });
if (!opts?.extern) this.builder.insertInto(func.addBlock("entry"));

// this.currentFunction = func;
return func;
}

ret(value?: Value) {
// if (!this.currentFunction) {
// throw new Error("No current function to return from");
// }

this.builder.ret(value);
}

public add(left: Value, right: Value): Value {
return this.builder.add(left, right);
}

public sub(left: Value, right: Value): Value {
return this.builder.sub(left, right);
}

public mul(left: Value, right: Value): Value {
return this.builder.mul(left, right);
}

public div(left: Value, right: Value): Value {
if (left.getType().isFloat() || right.getType().isFloat()) {
return this.builder.fdiv(left, right);
}

return this.builder.sdiv(left, right);
}

public alloca(type: Type, name?: string): Value {
return this.builder.alloca(type, name);
}

public store(value: Value, ptr: Value): void {
this.builder.store(value, ptr);
}

public load(type: Type, ptr: Value, name?: string): Value {
return this.builder.load(type, ptr, name);
}

toString() {
this.mod.verify()
return this.mod.toString();
}
}
Loading