Skip to content

Commit c0d7d4f

Browse files
Merge pull request #60 from ramonasuncion/end-to-end-comp
Add default compile mode to ripec
2 parents 82c4441 + 24db267 commit c0d7d4f

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

compiler/bin/main.ml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@ let parse_file filename =
4141
else
4242
let il = Ripe.Codegen.emit_qbe tdecls in
4343
if !emit_qbe then print_string il
44+
else
45+
let dir = Filename.dirname !file in
46+
let base =
47+
Filename.concat dir
48+
(Filename.remove_extension (Filename.basename !file))
49+
in
50+
let tmp_qbe = Filename.temp_file "ripe" ".ssa" in
51+
let tmp_asm = Filename.temp_file "ripe" ".s" in
52+
let oc = open_out tmp_qbe in
53+
output_string oc il;
54+
close_out oc;
55+
let run cmd =
56+
if Sys.command cmd <> 0 then (
57+
Printf.eprintf "ripec: command failed: %s\n" cmd;
58+
exit 1)
59+
in
60+
run (Printf.sprintf "qbe -o %s %s" tmp_asm tmp_qbe);
61+
run (Printf.sprintf "cc -o %s %s" base tmp_asm);
62+
Sys.remove tmp_qbe;
63+
Sys.remove tmp_asm
4464
| exception Ripe.Typechecker.TypeError msg ->
4565
Printf.eprintf "typecheck error: %s\n" msg;
4666
exit 1

compiler/lib/codegen.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,10 @@ let emit_func (ctx : ctx) (tfd : T.tfunc_def) =
314314
(* FIXME: main(): with a trailing colon and no return type syntax error *)
315315
(* TODO: Create a custom _start. *)
316316
let is_main = tfd.name = "main" && tfd.ret_ty = TInt I32 in
317+
let export_part = if is_main then "export " else "" in
317318
let ret_part = match tfd.ret_ty with TVoid -> "" | t -> qbe_ty t ^ " " in
318319
(* TODO: Add export for pub(?) *)
319-
emit ctx "function %s$%s(%s) {\n" ret_part tfd.name
320+
emit ctx "%sfunction %s$%s(%s) {\n" export_part ret_part tfd.name
320321
(String.concat ", " params_strs);
321322
emit ctx "@start\n";
322323

0 commit comments

Comments
 (0)