Skip to content
Merged
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
42 changes: 42 additions & 0 deletions src/codegen/infrastructure/base-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,48 @@ export class BaseGenerator {
this.emit(`${name}:`);
}

emitCall(retType: string, func: string, args: string): string {
const temp = this.nextTemp();
this.emit(`${temp} = call ${retType} ${func}(${args})`);
this.setVariableType(temp, retType);
return temp;
}

emitCallVoid(func: string, args: string): void {
this.emit(`call void ${func}(${args})`);
}

emitLoad(type: string, ptr: string): string {
const temp = this.nextTemp();
this.emit(`${temp} = load ${type}, ${type}* ${ptr}`);
this.setVariableType(temp, type);
return temp;
}

emitStore(type: string, value: string, ptr: string): void {
this.emit(`store ${type} ${value}, ${type}* ${ptr}`);
}

emitGep(baseType: string, ptr: string, indices: string): string {
const temp = this.nextTemp();
this.emit(`${temp} = getelementptr ${baseType}, ${baseType}* ${ptr}, ${indices}`);
return temp;
}

emitIcmp(pred: string, type: string, lhs: string, rhs: string): string {
const temp = this.nextTemp();
this.emit(`${temp} = icmp ${pred} ${type} ${lhs}, ${rhs}`);
this.setVariableType(temp, 'i1');
return temp;
}

emitBitcast(value: string, fromType: string, toType: string): string {
const temp = this.nextTemp();
this.emit(`${temp} = bitcast ${fromType} ${value} to ${toType}`);
this.setVariableType(temp, toType);
return temp;
}

// ============================================
// Symbol table convenience methods
// ============================================
Expand Down
50 changes: 50 additions & 0 deletions src/codegen/infrastructure/generator-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,14 @@ export interface IGeneratorContext {
emitUnreachable(): void;
emitLabel(name: string): void;

emitCall(retType: string, func: string, args: string): string;
emitCallVoid(func: string, args: string): void;
emitLoad(type: string, ptr: string): string;
emitStore(type: string, value: string, ptr: string): void;
emitGep(baseType: string, ptr: string, indices: string): string;
emitIcmp(pred: string, type: string, lhs: string, rhs: string): string;
emitBitcast(value: string, fromType: string, toType: string): string;

getOutput(): string[];
clearOutput(): void;
pushOutput(line: string): void;
Expand Down Expand Up @@ -1200,6 +1208,48 @@ export class MockGeneratorContext implements IGeneratorContext {
this.emit(`${name}:`);
}

emitCall(retType: string, func: string, args: string): string {
const temp = this.nextTemp();
this.emit(`${temp} = call ${retType} ${func}(${args})`);
this.setVariableType(temp, retType);
return temp;
}

emitCallVoid(func: string, args: string): void {
this.emit(`call void ${func}(${args})`);
}

emitLoad(type: string, ptr: string): string {
const temp = this.nextTemp();
this.emit(`${temp} = load ${type}, ${type}* ${ptr}`);
this.setVariableType(temp, type);
return temp;
}

emitStore(type: string, value: string, ptr: string): void {
this.emit(`store ${type} ${value}, ${type}* ${ptr}`);
}

emitGep(baseType: string, ptr: string, indices: string): string {
const temp = this.nextTemp();
this.emit(`${temp} = getelementptr ${baseType}, ${baseType}* ${ptr}, ${indices}`);
return temp;
}

emitIcmp(pred: string, type: string, lhs: string, rhs: string): string {
const temp = this.nextTemp();
this.emit(`${temp} = icmp ${pred} ${type} ${lhs}, ${rhs}`);
this.setVariableType(temp, 'i1');
return temp;
}

emitBitcast(value: string, fromType: string, toType: string): string {
const temp = this.nextTemp();
this.emit(`${temp} = bitcast ${fromType} ${value} to ${toType}`);
this.setVariableType(temp, toType);
return temp;
}

getOutput(): string[] {
return this.output;
}
Expand Down
19 changes: 7 additions & 12 deletions src/codegen/stdlib/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,13 @@ export class DateGenerator {
const tvAlloca = this.ctx.nextTemp();
this.ctx.emit(`${tvAlloca} = alloca %struct.timeval`);

const callResult = this.ctx.nextTemp();
this.ctx.emit(`${callResult} = call i32 @gettimeofday(%struct.timeval* ${tvAlloca}, i8* null)`);

const secPtr = this.ctx.nextTemp();
this.ctx.emit(`${secPtr} = getelementptr %struct.timeval, %struct.timeval* ${tvAlloca}, i32 0, i32 0`);
const secVal = this.ctx.nextTemp();
this.ctx.emit(`${secVal} = load i64, i64* ${secPtr}`);

const usecPtr = this.ctx.nextTemp();
this.ctx.emit(`${usecPtr} = getelementptr %struct.timeval, %struct.timeval* ${tvAlloca}, i32 0, i32 1`);
const usecVal = this.ctx.nextTemp();
this.ctx.emit(`${usecVal} = load i64, i64* ${usecPtr}`);
this.ctx.emitCall('i32', '@gettimeofday', `%struct.timeval* ${tvAlloca}, i8* null`);

const secPtr = this.ctx.emitGep('%struct.timeval', tvAlloca, 'i32 0, i32 0');
const secVal = this.ctx.emitLoad('i64', secPtr);

const usecPtr = this.ctx.emitGep('%struct.timeval', tvAlloca, 'i32 0, i32 1');
const usecVal = this.ctx.emitLoad('i64', usecPtr);

const secDouble = this.ctx.nextTemp();
this.ctx.emit(`${secDouble} = sitofp i64 ${secVal} to double`);
Expand Down
Loading