Skip to content

Commit

Permalink
Ch- ch- ch- CHANGES
Browse files Browse the repository at this point in the history
  • Loading branch information
jake-derry committed Apr 28, 2020
1 parent 7253f9d commit 9e51c0a
Show file tree
Hide file tree
Showing 28 changed files with 6,174 additions and 37,654 deletions.
3 changes: 1 addition & 2 deletions .cm/SKEL/main.sml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
Skeleton 5
d2f7d"List"d"Temp"Cd"Assem"d"Canon"d"Parse"d"TextIO"d"MIPSGen"Nad"Main"h4aä¢F"gp1d"MIPSFrame"aä¢Tr"jçð±ÿ
gp1e"Translate"ad"S"jgp1ÿgp1e"Semant"ad"R"jÿ$gp1e"RegAlloc"
d2f8d"OS"d"List"d"Temp"Cd"Assem"d"Canon"d"Parse"d"TextIO"d"MIPSGen"Nad"Main"h4aä¢F"gp1d"MIPSFrame"aä¢Tr"jçð±ÿgp1e"Translate"ad"S"jgp1ÿ#gp1e"Semant"ad"R"jÿ)gp1e"RegAlloc"
Binary file modified .cm/amd64-unix/main.sml
Binary file not shown.
Binary file modified IR/.cm/amd64-unix/translate.sig
Binary file not shown.
Binary file modified IR/.cm/amd64-unix/translate.sml
Binary file not shown.
1 change: 1 addition & 0 deletions IR/translate.sig
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ sig
val stringIR : string -> exp
val letIR : exp list * exp -> exp
val subscriptIR : exp * exp -> exp
val fieldIR : exp * int -> exp


val procedureEntryExit : level * exp * Temp.label -> unit
Expand Down
12 changes: 6 additions & 6 deletions IR/translate.sml
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,12 @@ struct
| opIR (left, A.GtOp, right) = Ex (T.RELOP (T.GT, unEx left, unEx right))
| opIR (left, A.GeOp, right) = Ex (T.RELOP (T.GE, unEx left, unEx right))

(* fun stringCompare (left, right) = Ex () *)

fun stringOpIR (left, A.EqOp, right) = Ex (F.externalCall ("tig_stringEqual", [unEx left, unEx right]))
| stringOpIR (left, A.NeqOp, right) = Ex (T.RELOP (T.EQ, F.externalCall ("tig_stringEqual", [unEx left, unEx right]), T.CONST (0)))
| stringOpIR (left, A.LtOp, right) = Ex (T.RELOP (T.LT, F.externalCall ("stringCompare", [unEx left, unEx right]), T.CONST (0)))
| stringOpIR (left, A.LeOp, right) = Ex (T.RELOP (T.LE, F.externalCall ("stringCompare", [unEx left, unEx right]), T.CONST (0)))
| stringOpIR (left, A.GtOp, right) = Ex (T.RELOP (T.GT, F.externalCall ("stringCompare", [unEx left, unEx right]), T.CONST (0)))
| stringOpIR (left, A.GeOp, right) = Ex (T.RELOP (T.GE, F.externalCall ("stringCompare", [unEx left, unEx right]), T.CONST (0)))
| stringOpIR (left, A.LtOp, right) = Ex (T.RELOP (T.LT, F.externalCall ("tig_stringCompare", [unEx left, unEx right]), T.CONST (0)))
| stringOpIR (left, A.LeOp, right) = Ex (T.RELOP (T.LE, F.externalCall ("tig_stringCompare", [unEx left, unEx right]), T.CONST (0)))
| stringOpIR (left, A.GtOp, right) = Ex (T.RELOP (T.GT, F.externalCall ("tig_stringCompare", [unEx left, unEx right]), T.CONST (0)))
| stringOpIR (left, A.GeOp, right) = Ex (T.RELOP (T.GE, F.externalCall ("tig_stringCompare", [unEx left, unEx right]), T.CONST (0)))
| stringOpIR (left, _, right) = raise TranslationFailure ("Invalid string operation")

fun callIR (TOPLEVEL, calllevel, label, args) = Ex (T.CALL (T.NAME label, List.map unEx args))
Expand Down Expand Up @@ -231,6 +229,8 @@ struct
in Ex (T.ESEQ (seq (setUpRecord ()), T.TEMP record))
end

fun fieldIR (r, index) = Ex (T.MEM (T.BINOP (T.PLUS, unEx r, T.CONST (index * F.wordSize))))

fun arrayIR (sizeExp, initExp) = Ex (F.externalCall ("tig_initArray", [unEx sizeExp, unEx initExp]))

fun subscriptIR (arrayExp, subscriptExp) = Ex (T.MEM (T.BINOP (T.PLUS, unEx arrayExp, T.BINOP (T.MUL, unEx subscriptExp, T.CONST F.wordSize))))
Expand Down
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ARCH=amd64-darwin

ambush: ambush.$(ARCH)
echo "sml @SMLload ambush.$(ARCH) \$$1" > ./ambush && chmod +x ./ambush

ambush.$(ARCH): sources.cm
ml-build sources.cm Main.main ambush

clean:
rm testcases/*.s && rm ./ambush.* && rm ./ambush
72 changes: 53 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,57 @@ Diego. Archived from the original on July 4, 2013.</sub>

### Usage Instructions

To test the compiler, open a terminal in the main folder and run:
#### Using `make` and packaged executable

To build:

```bash
sml run.sml
make
```

In the resulting SML REPL, execute
To use the compiler:

```sml
runTest n;
```
./ambush [tiger file]
```

To run the test case with the number `n`. To compile an arbitrary Tiger
program, instead execute
This will place the output file in the same directory as the source file, with
the extension `.s` appended (for example `test1.tig` becomes `test1.tig.s`).

```sml
Main.compile "testcases/myProgram";
To run the resulting assembly file using SPIM:

```
spim -file [assembly file]
```

(**NOTE:** Without the `.tig` extension)
To perform this whole process at once, use the `test.sh` script. For
example, to build the compiler, compile test 51, and run the result using SPIM,
just execute:

#### Old Instructions:
```bash
tc=test51.tig ./test.sh
```

To use the lexer, first open an `sml` REPL in the root folder of this
repository. Then, run the following command to compile the project:
For more information, check the `Makefile`.

```sml
CM.make "sources.cm";
**Note: This has only been tested on MacOS. If using Linux, Windows, or other OS,
please build the project manually using `sml` then `CM.make "sources.cm"`. **

#### Manually building and testing using REPL

To test the compiler, open a terminal in the main folder and run:

```bash
sml run.sml
```

In order to test the parser, use the following command, where `myFile.tig` is
a relative path to the Tiger input source file:
In the resulting SML REPL, execute

```sml
Parse.parse "myFile.tig";
Main.compile "testcases/myProgram.tig";
```


### Project Structure and Files

#### General
Expand Down Expand Up @@ -85,6 +99,10 @@ Parse.parse "myFile.tig";
- `tiger.grm.sig` contains auto-generated code from ML-Yacc
- `tiger.grm.sml` contains the auto-generated parser by ML-Yacc

```
TODO: Finish this description/list
```


## Lexer

Expand Down Expand Up @@ -403,6 +421,22 @@ L4:
# END L0
```

## Putting it All Together

To finish, we added some miscellaneous fixes throughout the project.

Functions now get their arguments/formals from the right `access`es, and follow
proper calling conventions for saving and restoring registers.

We also (fixed and then) added the Tiger runtime library as provided by Professor
Hilton, and augmented it with a few of our own functions, such as `print_int`,
which use MIPS syscalls to easily accomplish what would otherwise have been complicated
features to implement in plain Tiger.

We also revamped the project's build and testing system by switching to `make`
and `ml-build` to package a heap snapshot of the SMLofNJ environment for ease
of use.


## Extra Credit Features

Expand All @@ -418,7 +452,7 @@ No guarantees are made that the `musical` branch will be maintained or updated
past the state that it was as of 2020-02-27 at 12:15 PM. As of now, it should
not be considered part of our official submission.

## Future Compiler Features (Extra Credit)
## Future Possible Compiler Features (Extra Credit)

- [ ] SML Formatter
- [ ] Tiger formatter
Expand Down
1 change: 1 addition & 0 deletions ambush
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sml @SMLload ambush.amd64-darwin $1
Binary file added ambush.amd64-darwin
Binary file not shown.
74 changes: 48 additions & 26 deletions main.sml
Original file line number Diff line number Diff line change
Expand Up @@ -60,32 +60,54 @@ struct
end
| emitproc out (F.STRING(lab,s)) = TextIO.output(out, (F.string (lab,s)) ^ "\n")

fun withOpenFile fname f =
let val out = TextIO.openOut fname
in ((*(f TextIO.stdOut);*) (f out before TextIO.closeOut out))
(* handle e => (TextIO.closeOut out; raise e) *)
end
fun withOpenFile fname f =
let val out = TextIO.openOut fname
in ((*(f TextIO.stdOut);*) (f out before TextIO.closeOut out))
(* handle e => (TextIO.closeOut out; raise e) *)
end

fun compile filename =
let val _ = Temp.start ()
(* Read the libraries from its file *)
val runtimeLibLines = readFileLines "runtimele.s"
val spimSyscallLib = readFileLines "sysspim.s"

fun compile filename =
let val _ = Temp.start ()
(* Read the libraries from its file *)
val runtimeLibLines = readFileLines "runtimele.s"
val spimSyscallLib = readFileLines "sysspim.s"
val absyn = Parse.parse (filename)
(* val _ = print "\nAbstract Syntax Tree: \n";
val _ = PrintAbsyn.print (TextIO.stdOut, absyn) *)

val frags = ((*FindEscape.prog absyn;*) S.transProg absyn)
handle e => raise e

val absyn = Parse.parse (filename ^ ".tig")
(* val _ = print "\nAbstract Syntax Tree: \n";
val _ = PrintAbsyn.print (TextIO.stdOut, absyn) *)
val fail = ref false
val frags = ((*FindEscape.prog absyn;*) S.transProg absyn)
handle S.TypeCheckError (frags)=> (fail := true; frags)
val openFile = if (!fail) then ()
else withOpenFile (filename ^ ".s")
(fn out => (TextIO.output (out, F.jumpStart);
(app (emitproc out) ((*List.rev*) frags));

(* Add the runtime library and SPIM syscall library to the end of the file *)
(app (fn l => TextIO.output (out, l)) (runtimeLibLines @ ["\n\n\n"] @ spimSyscallLib))))
val _ = Temp.reset ()
in ()
end
val openFile = withOpenFile (filename ^ ".s")
(fn out => (TextIO.output (out, F.jumpStart);
(app (emitproc out) ((*List.rev*) frags));

(* Add the runtime library and SPIM syscall library to the end of the file *)
(app (fn l => TextIO.output (out, l)) (runtimeLibLines @ ["\n\n\n"] @ spimSyscallLib))))
val _ = Temp.reset ()
in ()
end

(* Entry point of the application *)
fun main (sml_path_name : string, args : string list) : OS.Process.status =
let val STATUS_SUCCESS = 0
val STATUS_FAIL = 1
val prog_name = List.hd args
val args = List.tl args
val exitstatus = case args of
[] => ((print "Error: Unspecified input file.\nUsage: ambush [inputfile] or ambush --help\n");
STATUS_FAIL)
| [arg1] => if arg1 = "--help"
then ((print "Usage: ambush [inputfile] or ambush --help\n");
STATUS_SUCCESS)
else (* We were given a file name-- compile it *)
(compile arg1;
STATUS_SUCCESS)
| _ => ((print "Usage: ambush [inputfile] or ambush --help\n");
STATUS_FAIL)
handle _ => STATUS_FAIL
in exitstatus : OS.Process.status
end

end
Binary file modified mips/.cm/amd64-unix/mipsframe.sml
Binary file not shown.
4 changes: 2 additions & 2 deletions mips/mipsframe.sml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ struct
val callersaves = (* argregs @ *) [t0, t1, t2, t3, t4, t5, t6, t7, t8, t9]
val calleesaves = [s0, s1, s2, s3, s4, s5, s6, s7]

val jumpStart = ".text\n j L0\n"
val jumpStart = ".text\n j tig_main\n"

(* Categories of registers *)

Expand Down Expand Up @@ -198,7 +198,7 @@ struct


fun string (label, str) =
(".data\n" ^ Symbol.name label ^ ": .asciiz \"" ^ str ^ "\"\n")
(".data\n .align 4\n" ^ Symbol.name label ^ ": .asciiz \"" ^ str ^ "\"\n")



Expand Down
Loading

0 comments on commit 9e51c0a

Please sign in to comment.