Skip to content

Commit

Permalink
One test is failing
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanuppal committed May 14, 2024
1 parent 781cb5e commit 904f38f
Show file tree
Hide file tree
Showing 18 changed files with 75 additions and 94 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
![CI Status](https://github.com/ethanuppal/cs3110_compiler/actions/workflows/ci.yaml/badge.svg)

> "x86 is simple trust me bro"
> Last updated: 2024-05-14 16:21:01.991746
> Last updated: 2024-05-14 17:09:39.630538
```
$ ./main -h
Expand Down
2 changes: 1 addition & 1 deletion lib/ir/basic_block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ let to_string bb =
^ BatDynArray.fold_left
(fun acc (ir, _) -> acc ^ "\n " ^ Ir.to_string ir)
"" bb.contents
^ "\n br "
^ "\n branch if "
^ Branch_condition.to_string bb.condition
5 changes: 3 additions & 2 deletions lib/ir/branch_condition.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type t =

(* TODO: pretty print *)
let to_string = function
| Always -> "always"
| Never -> "never"
| Always -> "true"
| Conditional (Constant cond) when cond <> 0 -> "true"
| Never | Conditional (Constant 0) -> "false"
| Conditional op -> "if " ^ Operand.to_string op
13 changes: 12 additions & 1 deletion lib/ir/cfg/cfg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,15 @@ let to_string cfg =
"_"
^ (cfg.name |> String.concat "::")
^ ":\n"
^ (blocks_of cfg |> List.map Basic_block.to_string |> String.concat "\n")
^ (blocks_of cfg
|> List.map (fun bb ->
let bb_string = Basic_block.to_string bb in
let dest_strings =
out_edges cfg bb
|> List.map (fun (dest, cond) ->
"\n "
^ (if cond then "true" else "false")
^ " -> " ^ Basic_block.label_for dest)
in
bb_string ^ String.concat "" dest_strings)
|> String.concat "\n")
7 changes: 6 additions & 1 deletion lib/ir/passes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module ConstFold : Pass.PASS = struct
(Ir.Assign (var, Operand.make_const (lhs - rhs)))
| _ -> ()
done
(* ; match Basic_block.condition_of bb with | Conditional (Constant cond) ->
Basic_block.set_condition bb (if cond = 0 then Never else Always) | _ ->
() *)

let pass = Pass.make const_fold
end
Expand All @@ -26,7 +29,9 @@ module CopyProp : Pass.PASS = struct
in
for i = 0 to Basic_block.length_of bb - 1 do
match Basic_block.get_ir bb i with
| Assign (var, oper) -> Ir.VariableMap.replace vals var oper
| Assign (var, oper) ->
Ir.VariableMap.replace vals var oper;
Basic_block.set_ir bb i (Assign (var, subs oper))
| Add (var, oper1, oper2) ->
Basic_block.set_ir bb i (Add (var, subs oper1, subs oper2))
| Sub (var, oper1, oper2) ->
Expand Down
76 changes: 52 additions & 24 deletions lib/user/driver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@ let print_version () =
printf "\n";
printf "Written by: %s\n" (String.concat ", " Meta.get.authors)

let compile paths _flags build_dir_loc =
Printf.printf "[DEBUG] assumes [paths] has one file, ignores flags\n";
let source_path = List.hd paths in
let source = Util.read_file source_path in

try
(* Compile to NASM *)
let compile paths flags build_dir_loc =
let do_opts = List.mem Cli.Optimize flags in
let compile_one source_path =
let source = Util.read_file source_path in
let statements = Parse_lex.lex_and_parse ~filename:source_path source in
Analysis.infer statements;
let cfgs = Ir_gen.generate statements in
Expand All @@ -35,6 +32,14 @@ let compile paths _flags build_dir_loc =
List.iter
(fun cfg ->
let liveliness_analysis = Liveliness.analysis_of cfg in
if do_opts then
Passes.apply
[
Passes.DeadCode.pass;
Pass.sequence Passes.CopyProp.pass Passes.ConstFold.pass
|> Pass.repeat 10;
]
cfg liveliness_analysis;
let instr_ordering = InstrOrdering.make cfg in
let regalloc =
Regalloc.allocate_for cfg liveliness_analysis instr_ordering
Expand All @@ -43,6 +48,18 @@ let compile paths _flags build_dir_loc =
cfgs;
let asm_file = Asm.AssemblyFile.make () in
Asm.AssemblyFile.add asm_file text_section;
let file_name_root =
BatFilename.(source_path |> basename |> chop_extension)
in
let ir_file_name = file_name_root ^ ".x86istmb_ir" in
let asm_file_name = file_name_root ^ ".nasm" in
(ir_file_name, cfgs, asm_file_name, asm_file)
in

Printf.printf "[DEBUG] ignores some flags but -O works\n";

try
let compiled_files = List.map compile_one paths in

(* Set up build directory *)
let build_dir =
Expand All @@ -60,24 +77,30 @@ let compile paths _flags build_dir_loc =
failwith "Could not remove old build_dir/ contents.";
Sys.chdir build_dir;

(* Write NASM *)
let asm_file_name =
BatFilename.(source_path |> basename |> chop_extension) ^ ".nasm"
in
Util.write_file asm_file_name (Asm.AssemblyFile.to_nasm asm_file);

let platform = Platform.get_platform () in

(* Write NASM *)
List.iter
(fun (ir_file_name, cfgs, asm_file_name, asm_file) ->
Util.write_file ir_file_name
(cfgs |> List.map Cfg.to_string |> String.concat "\n");
Util.write_file asm_file_name (Asm.AssemblyFile.to_nasm asm_file))
compiled_files;

(* Run NASM *)
let object_format =
match Platform.object_format platform with
| Some format -> format
| None -> failwith "Could not determine object file format."
in
let nasm_command =
Printf.sprintf "nasm -f %s %s -o build.o" object_format asm_file_name
in
if Sys.command nasm_command <> 0 then failwith "Failed to run NASM.";
List.iter
(fun (_, _, asm_file_name, _) ->
let nasm_command =
Printf.sprintf "nasm -f %s %s -o %s.o" object_format asm_file_name
(BatFilename.chop_extension asm_file_name)
in
if Sys.command nasm_command <> 0 then failwith "Failed to run NASM.")
compiled_files;

(* Run clang *)
let runtime_folder_name =
Expand All @@ -95,7 +118,7 @@ let compile paths _flags build_dir_loc =
Util.merge_paths [ Project_root.path; "lib/runtime"; runtime_folder_name ]
in
let clang_command =
Printf.sprintf "clang -target %s build.o %s/* -o a.out" clang_target
Printf.sprintf "clang -target %s *.o %s/* -o a.out" clang_target
runtime_lib_loc
in
if Sys.command clang_command <> 0 then failwith "Failed to run clang.";
Expand All @@ -107,11 +130,16 @@ let compile paths _flags build_dir_loc =
(Util.merge_paths [ build_dir; "a.out" ])
with Parse_lex.ParserError msg -> print_error (msg ^ "\n")

let rec dispatch action prog =
match action with
| Cli.Help -> print_help prog
| Version -> print_version ()
| Compile { paths; flags } ->
if List.is_empty paths then
dispatch (Error { msg = "expected at least one file name" }) prog
else compile paths flags None
| Error { msg } -> Printf.sprintf "%s\nuse %s -h\n" msg prog |> print_error

let main args =
let parse = Cli.parse args in
match parse.action with
| Help -> print_help parse.prog
| Version -> print_version ()
| Compile { paths; flags } -> compile paths flags None
| Error { msg } ->
Printf.sprintf "%s\nuse %s -h\n" msg parse.prog |> print_error
dispatch parse.action parse.prog
1 change: 0 additions & 1 deletion source/README.txt

This file was deleted.

4 changes: 0 additions & 4 deletions source/basic.x86istmb

This file was deleted.

7 changes: 0 additions & 7 deletions source/comment.x86istmb

This file was deleted.

18 changes: 0 additions & 18 deletions source/func.x86istmb

This file was deleted.

10 changes: 0 additions & 10 deletions source/scope.x86istmb

This file was deleted.

19 changes: 0 additions & 19 deletions source/shadow1.x86istmb

This file was deleted.

5 changes: 0 additions & 5 deletions source/start.x86istmb

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 904f38f

Please sign in to comment.