-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
132 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,71 @@ | ||
let emit_ir section = | ||
ignore section; | ||
failwith "i hate reg alloc" | ||
|
||
let emit_bb section cfg bb = | ||
let ir = Basic_block.to_list bb in | ||
let jumps = Cfg.out_edges cfg bb in | ||
ignore jumps; | ||
List.iter (emit_ir section) ir; | ||
failwith "emit jumps" | ||
|
||
let emit section cfg = Cfg.blocks_of cfg |> List.iter (emit_bb section cfg) | ||
let emit_var regalloc var = | ||
Asm.Operand.Register (Ir.VariableMap.find regalloc var) | ||
|
||
let emit_oper regalloc = function | ||
| Operand.Variable var -> emit_var regalloc var | ||
| Constant int -> Asm.Operand.Intermediate int | ||
|
||
let emit_call text regalloc name args = | ||
Asm.Section.add_all text | ||
[ | ||
Push (Register RDI); | ||
Push (Register RDI); | ||
(* double push for 16 byte alignment *) | ||
Mov (Register RDI, List.hd args |> emit_oper regalloc); | ||
Call (Label name); | ||
Pop (Register RDI); | ||
Pop (Register RDI); | ||
] | ||
|
||
(** *) | ||
let emit_ir text regalloc = function | ||
| Ir.Assign (var, op) -> | ||
Asm.Section.add text (Mov (emit_var regalloc var, emit_oper regalloc op)) | ||
| Add (var, op, op2) -> | ||
Asm.Section.add_all text | ||
[ | ||
Mov (emit_var regalloc var, emit_oper regalloc op); | ||
Add (emit_var regalloc var, emit_oper regalloc op2); | ||
] | ||
| Sub (var, op, op2) | TestEqual (var, op, op2) -> | ||
Asm.Section.add_all text | ||
[ | ||
Mov (emit_var regalloc var, emit_oper regalloc op); | ||
Sub (emit_var regalloc var, emit_oper regalloc op2); | ||
] | ||
| Ref _ -> failwith "ref not impl" | ||
| Deref _ -> failwith "deref not impl" | ||
| DebugPrint op -> emit_call text regalloc "_x86istimb_debug_print" [ op ] | ||
| Call _ -> failwith "TODO" | ||
| Return op -> | ||
Asm.Section.add text (Mov (Register RAX, emit_oper regalloc op)) | ||
|
||
let emit_bb text cfg regalloc bb = | ||
Asm.Section.add text | ||
(Label | ||
(Asm.Label.make ~is_global:false ~is_external:false | ||
(Basic_block.label_for bb))); | ||
bb |> Basic_block.to_list |> List.iter (emit_ir text regalloc); | ||
match Basic_block.condition_of bb with | ||
| Never | Conditional (Constant 0) -> () | ||
| Always | Conditional (Constant _) -> | ||
let dest_bb = Cfg.take_branch cfg bb true |> Option.get in | ||
Asm.Section.add text (Jmp (Label (Basic_block.label_for dest_bb))) | ||
| Conditional op -> ( | ||
let true_bb = Cfg.take_branch cfg bb true |> Option.get in | ||
let false_bb = Cfg.take_branch cfg bb false |> Option.get in | ||
match op with | ||
| Variable var -> | ||
Asm.Section.add text (Cmp (emit_var regalloc var, Intermediate 0)); | ||
Asm.Section.add text (Je (Label (Basic_block.label_for false_bb))); | ||
Asm.Section.add text (Jmp (Label (Basic_block.label_for true_bb))) | ||
| Constant _ -> failwith "failure") | ||
|
||
let emit_preamble ~text = | ||
Asm.Section.add text | ||
(Label | ||
(Asm.Label.make ~is_global:false ~is_external:true | ||
"_x86istimb_debug_print")) | ||
|
||
let emit_cfg ~text cfg regalloc = | ||
Cfg.blocks_of cfg |> List.iter (emit_bb text cfg regalloc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
(** [emit section cfg] emits the function [cfg] into the assembly section | ||
[section]. *) | ||
val emit : Asm.Section.t -> Cfg.t -> unit | ||
(** [emit_preamble ~text:text] emits the x86istmb runtime preamble into the | ||
assembly code section [text]. *) | ||
val emit_preamble : text:Asm.Section.t -> unit | ||
|
||
(** [emit ~text:text cfg regalloc] emits the function [cfg] with register | ||
allocation [regalloc] into the assembly code section [text]. *) | ||
val emit_cfg : | ||
text:Asm.Section.t -> Cfg.t -> Regalloc.allocation Ir.VariableMap.t -> unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#include <stdio.h> | ||
#include <stdint.h> | ||
|
||
void x86istimb_debug_print(int64_t value) { | ||
printf("%d\n", value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters