Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CUB] Dmitrii Artiukhov A02 #1223

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/Expr.lama
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,28 @@ import State;
-- Binop (string, expr, expr)

public fun evalExpr (st, expr) {
failure ("evalExpr not implemented\n")
case expr of
Var (x) -> st(x)
| Const (n) -> n
| Binop (op, e1, e2) -> evalBinop(op, evalExpr(st, e1), evalExpr(st, e2))
esac
-- failure ("evalExpr not implemented\n")
}

public fun evalBinop(op, x, y) {
case op of
"+" -> x + y
| "-" -> x - y
| "*" -> x * y
| "/" -> x / y
| "%" -> x % y
| "<" -> x < y
| ">" -> x > y
| "<=" -> x <= y
| ">=" -> x >= y
| "==" -> x == y
| "!=" -> x != y
| "&&" -> x && y
| "||" -> x !! y
esac
}
35 changes: 32 additions & 3 deletions src/SM.lama
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,24 @@ public fun showSM (prg) {
-- Stack machine interpreter. Takes an SM-configuration and a program,
-- returns a final configuration
fun eval (c, insns) {
failure ("SM eval not implemented\n")
foldl(fun (conf@[stack, st, w], ins) {
case ins of
READ ->
case readWorld(w) of
[v, wld] -> [v : stack, st, wld]
esac
| WRITE -> [tl(stack), st, writeWorld(hd(stack), w)]
| BINOP (op) ->
case stack of
y : x : rest -> [evalBinop(op, x, y) : rest, st, w]
| _ -> failure("Stack does not have enough elements. Required >=2 for BINOP.")
esac
| LD (x) -> [st(x) : stack, st, w]
| ST (x) -> [tl(stack), st <- [x, hd(stack)], w]
| CONST (n) -> [n : stack, st, w]
esac
}, c, insns)
-- failure ("SM eval not implemented\n")
}

-- Runs a stack machine for a given input and a given program, returns an output
Expand All @@ -38,12 +55,24 @@ public fun evalSM (input, insns) {
-- Compiles an expression into a stack machine code.
-- Takes an expression, returns a list of stack machine instructions
fun compileExpr (expr) {
failure ("compileExpr not implemented\n")
case expr of
Var (x) -> {LD (x)}
| Const (n) -> {CONST (n)}
| Binop (op, e1, e2) -> compileExpr(e1) +++ compileExpr(e2) +++ { BINOP (op) }
esac
-- failure ("compileExpr not implemented\n")
}

-- Compiles a statement into a stack machine code.
-- Takes a statement, returns a list of stack machine
-- instructions.
public fun compileSM (stmt) {
failure ("compileSM not implemented\n")
case stmt of
Assn (x, expr) -> compileExpr(expr) +++ {ST (x)}
| Seq (stmt1, stmt2) -> compileSM(stmt1) +++ compileSM(stmt2)
| Skip -> {}
| Read (x) -> {READ, ST (x)}
| Write (expr) -> compileExpr(expr) +++ {WRITE}
esac
-- failure ("compileSM not implemented\n")
}
15 changes: 14 additions & 1 deletion src/Stmt.lama
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,22 @@ import World;
-- Write (expr) |

fun eval (c, stmt) {
failure ("Stmt eval not implemented\n")
-- c = [ state, world ]
case stmt of
Assn (x, expr) -> [c.fst <- [x, evalExpr(c.fst, expr)], c.snd]
| Seq (stmt1, stmt2) -> eval(eval(c, stmt1), stmt2)
| Skip -> c
| Read (x) ->
case readWorld(c.snd) of
[v, w] -> [c.fst <- [x, v], w]
esac
| Write (expr) -> [c.fst, writeWorld(evalExpr(c.fst, expr), c.snd)]
esac

-- failure ("Stmt eval not implemented\n")
}


-- Evaluates a program with a given input and returns an output
public fun evalStmt (input, stmt) {
eval ([emptyState, createWorld (input)], stmt).snd.getOutput
Expand Down
84 changes: 80 additions & 4 deletions src/X86.lama
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,77 @@ fun suffix (op) {
esac
}

fun compileAssociativeOp (op, x, y, env, code) {
if stackOpnd(x) && stackOpnd(y) then [env.push(y), code <+ Mov (x, eax) <+ Binop (op, eax, y)]
else [env.push(y), code <+ Binop (op, x, y)]
fi
}

fun compileDivisionOp (op, x, y, env, code) {
-- we are actually doing (y / x) or (y % x) here
case op of
"/" -> [env.push(y), code <+ Mov (y, eax) <+ Cltd <+ IDiv (x) <+ Mov (eax, y)]
| "%" -> [env.push(y), code <+ Mov (y, eax) <+ Cltd <+ IDiv (x) <+ Mov (edx, y)]
esac
}

fun compileLogicalOp (op, x, y, env, code) {
-- if y == 0: env.push(0) else: env.push(1)
-- if x == 0: env.push(0) else: env.push(1)
-- return env.pop2 -> [binx, biny, env]: compileBinop(op, binx, biny, env, code)

fun compileConvertionIntToBoolean (pos, env, code) {
var dl = "%dl";

[env.push(pos), code <+ Mov(L (0), edx) <+ Binop ("cmp", L (0), pos) <+ Set (suffix("!="), dl) <+ Mov (edx, pos)]
}

case compileConvertionIntToBoolean(y, env, code) of
[env, code] ->
case compileConvertionIntToBoolean(x, env, code) of
[env, code] ->
case env.pop2 of
[binx, biny, env] -> compileAssociativeOp(op, binx, biny, env, code)
esac
esac
esac
}

fun compileComparisonOp(op, x, y, env, code) {
var dl = "%dl";

if (stackOpnd(x) && stackOpnd(y)) then [env.push(y), code <+ Mov (L (0), edx) <+ Mov (x, eax) <+ Binop ("cmp", eax, y) <+ Set (suffix(op), dl) <+ Mov (edx, y)]
else [env.push(y), code <+ Mov (L (0), edx) <+ Binop ("cmp", x, y) <+ Set (suffix(op), dl) <+ Mov (edx, y)]
fi
}

fun compileBinop (op, env, code) {
case env.pop2 of
[x, y, env] ->
case op of
"+" -> compileAssociativeOp(op, x, y, env, code)
| "-" -> compileAssociativeOp(op, x, y, env, code)
| "*" -> compileAssociativeOp(op, x, y, env, code)
| "^" -> compileAssociativeOp(op, x, y, env, code)

| "/" -> compileDivisionOp(op, x, y, env, code)
| "%" -> compileDivisionOp(op, x, y, env, code)

| "&&" -> compileLogicalOp(op, x, y, env, code)
| "!!" -> compileLogicalOp(op, x, y, env, code)

| "<" -> compileComparisonOp(op, x, y, env, code)
| "<=" -> compileComparisonOp(op, x, y, env, code)
| ">" -> compileComparisonOp(op, x, y, env, code)
| ">=" -> compileComparisonOp(op, x, y, env, code)
| "==" -> compileComparisonOp(op, x, y, env, code)
| "!=" -> compileComparisonOp(op, x, y, env, code)

| _ -> failure ("codegeneration for binop %s is not yet implemented\n", op.string)
esac
esac
}

-- Compiles stack machine code into a list of x86 instructions. Takes an environment
-- and stack machine code, returns an updated environment and x86 code.
fun compile (env, code) {
Expand All @@ -289,12 +360,17 @@ fun compile (env, code) {
esac
| LD (x) ->
case env.addGlobal (x).allocate of
[s, env] -> [env, code <+> move (env.loc (x), s)]
esac
[s, env] -> [env, code <+> move (env.loc (x), s)]
esac
| ST (x) ->
case env.addGlobal (x).pop of
[s, env] -> [env, code <+> move (s, env.loc (x))]
esac
[s, env] -> [env, code <+> move (s, env.loc (x))]
esac
| CONST (n) ->
case env.allocate of
[s, env] -> [env, code <+ Mov (L (n), s)]
esac
| BINOP (op) -> compileBinop(op, env, code)
| _ -> failure ("codegeneration for instruction %s is not yet implemented\n", i.string)
esac
}, [env, emptyBuffer ()], code)
Expand Down
Loading