mirror of
https://github.com/Mercantec-GHC/h4-projekt-gruppe-0-sm.git
synced 2025-04-27 00:14:06 +02:00
compiler: use cqo, short ty
This commit is contained in:
parent
43740eb9ee
commit
ba8da1fccb
@ -33,16 +33,16 @@ export class AsmGen {
|
||||
}
|
||||
|
||||
this.writeln(`sbc__div:`);
|
||||
this.writeIns(`mov rdx, 0`);
|
||||
this.writeIns(`mov rax, [rsp+16]`);
|
||||
this.writeIns(`mov rdi, [rsp+8]`);
|
||||
this.writeIns(`div rdi`);
|
||||
this.writeIns(`cqo`);
|
||||
this.writeIns(`idiv rdi`);
|
||||
this.writeIns(`ret`);
|
||||
this.writeln(`sbc__mod:`);
|
||||
this.writeIns(`mov rdx, 0`);
|
||||
this.writeIns(`mov rax, [rsp+16]`);
|
||||
this.writeIns(`mov rdi, [rsp+8]`);
|
||||
this.writeIns(`div rdi`);
|
||||
this.writeIns(`cqo`);
|
||||
this.writeIns(`idiv rdi`);
|
||||
this.writeIns(`mov rax, rdx`);
|
||||
this.writeIns(`ret`);
|
||||
|
||||
|
@ -111,7 +111,9 @@ export class FnStringifyer {
|
||||
case "error":
|
||||
return "<error>";
|
||||
case "push":
|
||||
return `push (${tyToString(k.ty)}) ${this.val(k.val)}`;
|
||||
return `push (${tyToString(k.ty, { short: true })}) ${
|
||||
this.val(k.val)
|
||||
}`;
|
||||
case "pop":
|
||||
return "pop";
|
||||
case "load":
|
||||
|
29
sbc/ty.ts
29
sbc/ty.ts
@ -9,7 +9,11 @@ export type Ty =
|
||||
| { tag: "ptr"; ty: Ty }
|
||||
| { tag: "fn"; stmt: ast.Stmt; params: Ty[]; returnTy: Ty };
|
||||
|
||||
export function tyToString(ty: Ty): string {
|
||||
type TyToStringOpts = {
|
||||
short?: boolean;
|
||||
};
|
||||
|
||||
export function tyToString(ty: Ty, opts: TyToStringOpts = {}): string {
|
||||
switch (ty.tag) {
|
||||
case "error":
|
||||
return `<error>`;
|
||||
@ -24,12 +28,23 @@ export function tyToString(ty: Ty): string {
|
||||
case "ptr":
|
||||
return `*${tyToString(ty.ty)}`;
|
||||
case "fn": {
|
||||
const k = ty.stmt.kind as ast.StmtKind & { tag: "fn" };
|
||||
const params = ty.params
|
||||
.map((param, i) => `${k.params[i].ident}: ${tyToString(param)}`)
|
||||
.join(", ");
|
||||
const returnTy = tyToString(ty.returnTy);
|
||||
return `fn ${k.ident}(${params}) -> ${returnTy}`;
|
||||
if (!opts.short) {
|
||||
const k = ty.stmt.kind as ast.StmtKind & { tag: "fn" };
|
||||
const params = ty.params
|
||||
.map((param, i) =>
|
||||
`${k.params[i].ident}: ${tyToString(param)}`
|
||||
)
|
||||
.join(", ");
|
||||
const returnTy = tyToString(ty.returnTy);
|
||||
return `fn ${k.ident}(${params}) -> ${returnTy}`;
|
||||
} else {
|
||||
const k = ty.stmt.kind as ast.StmtKind & { tag: "fn" };
|
||||
const params = ty.params
|
||||
.map((param) => tyToString(param))
|
||||
.join(", ");
|
||||
const returnTy = tyToString(ty.returnTy);
|
||||
return `fn(${params}) -> ${returnTy}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user