mirror of
https://github.com/Mercantec-GHC/h4-projekt-gruppe-0-sm.git
synced 2025-04-27 08:24:05 +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.writeln(`sbc__div:`);
|
||||||
this.writeIns(`mov rdx, 0`);
|
|
||||||
this.writeIns(`mov rax, [rsp+16]`);
|
this.writeIns(`mov rax, [rsp+16]`);
|
||||||
this.writeIns(`mov rdi, [rsp+8]`);
|
this.writeIns(`mov rdi, [rsp+8]`);
|
||||||
this.writeIns(`div rdi`);
|
this.writeIns(`cqo`);
|
||||||
|
this.writeIns(`idiv rdi`);
|
||||||
this.writeIns(`ret`);
|
this.writeIns(`ret`);
|
||||||
this.writeln(`sbc__mod:`);
|
this.writeln(`sbc__mod:`);
|
||||||
this.writeIns(`mov rdx, 0`);
|
|
||||||
this.writeIns(`mov rax, [rsp+16]`);
|
this.writeIns(`mov rax, [rsp+16]`);
|
||||||
this.writeIns(`mov rdi, [rsp+8]`);
|
this.writeIns(`mov rdi, [rsp+8]`);
|
||||||
this.writeIns(`div rdi`);
|
this.writeIns(`cqo`);
|
||||||
|
this.writeIns(`idiv rdi`);
|
||||||
this.writeIns(`mov rax, rdx`);
|
this.writeIns(`mov rax, rdx`);
|
||||||
this.writeIns(`ret`);
|
this.writeIns(`ret`);
|
||||||
|
|
||||||
|
@ -111,7 +111,9 @@ export class FnStringifyer {
|
|||||||
case "error":
|
case "error":
|
||||||
return "<error>";
|
return "<error>";
|
||||||
case "push":
|
case "push":
|
||||||
return `push (${tyToString(k.ty)}) ${this.val(k.val)}`;
|
return `push (${tyToString(k.ty, { short: true })}) ${
|
||||||
|
this.val(k.val)
|
||||||
|
}`;
|
||||||
case "pop":
|
case "pop":
|
||||||
return "pop";
|
return "pop";
|
||||||
case "load":
|
case "load":
|
||||||
|
29
sbc/ty.ts
29
sbc/ty.ts
@ -9,7 +9,11 @@ export type Ty =
|
|||||||
| { tag: "ptr"; ty: Ty }
|
| { tag: "ptr"; ty: Ty }
|
||||||
| { tag: "fn"; stmt: ast.Stmt; params: Ty[]; returnTy: 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) {
|
switch (ty.tag) {
|
||||||
case "error":
|
case "error":
|
||||||
return `<error>`;
|
return `<error>`;
|
||||||
@ -24,12 +28,23 @@ export function tyToString(ty: Ty): string {
|
|||||||
case "ptr":
|
case "ptr":
|
||||||
return `*${tyToString(ty.ty)}`;
|
return `*${tyToString(ty.ty)}`;
|
||||||
case "fn": {
|
case "fn": {
|
||||||
const k = ty.stmt.kind as ast.StmtKind & { tag: "fn" };
|
if (!opts.short) {
|
||||||
const params = ty.params
|
const k = ty.stmt.kind as ast.StmtKind & { tag: "fn" };
|
||||||
.map((param, i) => `${k.params[i].ident}: ${tyToString(param)}`)
|
const params = ty.params
|
||||||
.join(", ");
|
.map((param, i) =>
|
||||||
const returnTy = tyToString(ty.returnTy);
|
`${k.params[i].ident}: ${tyToString(param)}`
|
||||||
return `fn ${k.ident}(${params}) -> ${returnTy}`;
|
)
|
||||||
|
.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