Skip to content

Commit

Permalink
A09: x86 compiler works
Browse files Browse the repository at this point in the history
  • Loading branch information
khaser committed May 8, 2024
1 parent 8811031 commit 6cdef55
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
4 changes: 2 additions & 2 deletions regression/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ check: $(TESTS) expr_tests

$(TESTS): %: %.lama
@echo $@
@ulimit -s -S 32768; cat $@.input | $(LAMAC) $@.lama -i > $@.log && diff $@.log orig/$@.log
@ulimit -s -S 32768; cat $@.input | $(LAMAC) $@.lama -i > $@.log && diff $@.log orig/$@.log
@ulimit -s -S 32768; cat $@.input | $(LAMAC) $@.lama -s > $@.log && diff $@.log orig/$@.log
# $(LAMAC) [email protected] && cat [email protected] | ./$@ > [email protected] && diff [email protected] orig/[email protected]
$(LAMAC) $@.lama && cat $@.input | ./$@ > $@.log && diff $@.log orig/$@.log

expr_tests:
make -C expressions
Expand Down
5 changes: 5 additions & 0 deletions src/Lexer.lama
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public fun located (p) {
syntax(l=pos x=p {srcTab := addHashTab (srcTab, x, l); x})
}

-- A custom combinator to additionally add location info
public fun addLoc (x, loc) {
srcTab := addHashTab (srcTab, x, loc); skip
}

-- Retrieves location info; fail if no info found
public fun getLoc (s) {
case findHashTab (srcTab, s) of Some (loc) -> loc | None -> failure ("no location found for item %s\n", s.string) esac
Expand Down
1 change: 1 addition & 0 deletions src/Parser.lama
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ var
fun (a) {
scrut (Val) =>> fun (scrut) { chainST (map (fun (br) { br(a) }, brs)) =>> fun (brs) {
freshName => fun (scrut_name) {
addLoc (scrut_name, loc);
expandScope ({Var ({[scrut_name, Some (scrut)]})}, Case (scrut_name, reifyPatternBindings(scrut_name, brs)))
}
}}
Expand Down
5 changes: 3 additions & 2 deletions src/SM.lama
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public fun showSMInsn (i) {
| BUILTIN (f, n) -> sprintf ("BUILTIN %s, %d", f, n)
| META (m) -> sprintf ("META %s", m.string)
| PATT (p) -> sprintf ("PATT %s", p.string)
| UNMATCH (v, loc) -> sprintf ("UNMATCH %s, %d:%d", v.string, loc.fst, loc.snd)
esac
}

Expand Down Expand Up @@ -206,7 +207,7 @@ fun eval (env, w, insns) {
esac;
[[isMatched:ss, cf_stack, state, w], None]
esac
| META (errmsg) -> failure ("Runtime error: " ++ errmsg ++ "\n")
| UNMATCH (v, loc) -> error (sprintf ("pattern matching failed for variable ""%s""", v), loc)
| LABEL (_) -> [c, None]
| DROP ->
case stack of
Expand Down Expand Up @@ -707,7 +708,7 @@ public fun compileSM (stmt) {
esac
| Case (scrut_name, brs) ->
case foldl (genCaseBranchCode, [env, singletonBuffer (LD (lookupVal (env, scrut_name)))], brs) of
[env, code] -> [true, env, code <+ META ("suitable pattern not found") ]
[env, code] -> [true, env, code <+ UNMATCH (scrut_name, getLoc (scrut_name)) ]
esac
| While (pred, body) ->
case genLabels (env, 3) of
Expand Down
28 changes: 16 additions & 12 deletions src/X86.lama
Original file line number Diff line number Diff line change
Expand Up @@ -572,14 +572,6 @@ fun compile (env, code) {
[s, env] -> [env, code <+ Lea (env.loc (x), eax) <+> move (eax, s)]
esac
| ST (x) -> [env, code <+> move (env.peek, env.loc (x))]
-- Not emitted now
-- | STI ->
-- case pop (env, 2) of
-- [{v, rx}, env] ->
-- case allocate (env) of
-- [s, env] -> [env, code <+> move (v, if memOpnd (rx) then rx else I (0, rx) fi) <+> move (v, s)]
-- esac
-- esac
| STA ->
case pop (env, 3) of
[{v, i, a}, env] -> saveCallRestore (env, code, "Bsta", 0, {}, {v, i, a})
Expand All @@ -594,7 +586,7 @@ fun compile (env, code) {
[env, sname] -> saveCallRestore (env, code, "Bstring", 0, {}, {M ("$" ++ sname)})
esac
-- tagHash defined in standard library
| SEXP (stag, n) -> saveCallRestore (env, code, "Bsexp", n, {L (tagHash (stag))}, {L (makeBox (n + 1))})
| SEXP (stag, n) -> saveCallRestore (env, code, "Bsexp", n, {L (makeBox (tagHash (stag)))}, {L (makeBox (n + 1))})
| CONST (n) ->
case allocate (env) of
[s, env] -> [env, code <+> move (L (makeBox (n)), s)]
Expand All @@ -615,7 +607,7 @@ fun compile (env, code) {
esac
esac
esac
| LABEL (label) -> [if isBarrier(env) then retrieveStack (env, label) else env fi, code <+ Label (label)]
| LABEL (label) -> [if isBarrier (env) then retrieveStack (env, label) else env fi, code <+ Label (label)]
| JMP (label) -> [setBarrier (setStack (env, label)), code <+ Jmp (label)]
| CJMP (t, label) ->
case pop (env, 1) of
Expand All @@ -625,6 +617,10 @@ fun compile (env, code) {
case pop (env, 1) of
[_, env] -> [env, code]
esac
| DUP ->
case allocate (env) of
[s, new_env] -> [new_env, code <+> move (peek (env), s)]
esac
| GLOBAL (x) -> [addGlobal (env, x), code]
| BEGIN (fLabel, nargs, nlocs) -> [enterFunction (env, fLabel, nlocs), code <+ Label (fLabel) <+> prologue (fLabel) ]
| END ->
Expand All @@ -633,7 +629,14 @@ fun compile (env, code) {
esac
| CALL (fLabel, nargs) -> saveCallRestore (env, code, fLabel, nargs, {}, {})
| BUILTIN (fName, nargs) -> saveCallRestore (env, code, "L" ++ fName, nargs, {}, {})
esac
| PATT (pat) ->
case pat of
PArray (n) -> saveCallRestore (env, code, "Barray_patt", 1, {L (makeBox (n))}, {})
| PTag (tag, n) -> saveCallRestore (env, code, "Btag", 1, {L (makeBox (n)), L (makeBox (tagHash (tag)))}, {})
esac
| UNMATCH (v, loc) -> saveCallRestore (env, code, "Bmatch_failure", 1,
{ L (makeBox (snd (loc))), L (makeBox (fst (loc))), M ("$filename") }, {})
esac
}, [env, emptyBuffer ()], code)
}

Expand All @@ -653,7 +656,8 @@ public fun compileX86 (args, code) {
getBuffer $
singletonBuffer (Meta ("\t.global\tmain\n")) <+>
dataSection (listBuffer (map (intDef , getGlobals (env))) <+>
listBuffer (map (stringDef, getStrings (env)))) <+>
listBuffer (map (stringDef, getStrings (env))) <+>
singletonBuffer (stringDef (["filename", getBaseName (args)]))) <+>
codeSection (code)
).stringcat);

Expand Down

0 comments on commit 6cdef55

Please sign in to comment.