Mistake in new Infix located

This commit is contained in:
kverty 2019-09-10 00:46:10 +03:00
parent 8197ea1d20
commit 93c2f6db2d
5 changed files with 235 additions and 87 deletions

View file

@ -1,28 +1,28 @@
open GT
(* X86 codegeneration interface *)
(* The registers: *)
let regs = [|"%ebx"; "%ecx"; "%esi"; "%edi"; "%eax"; "%edx"; "%ebp"; "%esp"|]
(* We can not freely operate with all register; only 3 by now *)
(* We can not freely operate with all register; only 3 by now *)
let num_of_regs = Array.length regs - 5
(* We need to know the word size to calculate offsets correctly *)
let word_size = 4;;
(* We need to distinguish the following operand types: *)
@type opnd =
@type opnd =
| R of int (* hard register *)
| S of int (* a position on the hardware stack *)
| M of string (* a named memory location *)
| L of int (* an immediate operand *)
| I of opnd (* an indirect operand *)
| I of opnd (* an indirect operand *)
with show
let show_opnd = show(opnd)
(* For convenience we define the following synonyms for the registers: *)
(* For convenience we define the following synonyms for the registers: *)
let ebx = R 0
let ecx = R 1
let esi = R 2
@ -41,7 +41,7 @@ type instr =
(* x86 integer division, see instruction set reference *) | IDiv of opnd
(* see instruction set reference *) | Cltd
(* sets a value from flags; the first operand is the *) | Set of string * string
(* suffix, which determines the value being set, the *)
(* suffix, which determines the value being set, the *)
(* the second --- (sub)register name *)
(* pushes the operand on the hardware stack *) | Push of opnd
(* pops from the hardware stack to the operand *) | Pop of opnd
@ -56,7 +56,7 @@ type instr =
(* arithmetic correction: or 0x0001 *) | Or1 of opnd
(* arithmetic correction: shl 1 *) | Sal1 of opnd
(* arithmetic correction: shr 1 *) | Sar1 of opnd
| Repmovsl
| Repmovsl
(* Instruction printer *)
let show instr =
let binop = function
@ -64,7 +64,7 @@ let show instr =
| "-" -> "subl"
| "*" -> "imull"
| "&&" -> "andl"
| "!!" -> "orl"
| "!!" -> "orl"
| "^" -> "xorl"
| "cmp" -> "cmpl"
| _ -> failwith "unknown binary operator"
@ -98,7 +98,7 @@ let show instr =
| Sal1 s -> Printf.sprintf "\tsall\t%s" (opnd s)
| Sar1 s -> Printf.sprintf "\tsarl\t%s" (opnd s)
| Repmovsl -> Printf.sprintf "\trep movsl\t"
(* Opening stack machine to use instructions without fully qualified names *)
open SM
@ -119,9 +119,9 @@ let compile env code =
| "!=" -> "ne"
| ">=" -> "ge"
| ">" -> "g"
| _ -> failwith "unknown operator"
| _ -> failwith "unknown operator"
in
let rec compile' env scode =
let rec compile' env scode =
let on_stack = function S _ -> true | _ -> false in
let mov x s = if on_stack x && on_stack s then [Mov (x, eax); Mov (eax, s)] else [Mov (x, s)] in
let call env f n =
@ -143,7 +143,7 @@ let compile env code =
| "Barray" -> List.rev @@ (Push (L n)) :: pushs
| "Bsexp" -> List.rev @@ (Push (L n)) :: pushs
| "Bsta" -> pushs
| _ -> List.rev pushs
| _ -> List.rev pushs
in
env, pushr @ pushs @ [Call f; Binop ("+", L (4 * List.length pushs), esp)] @ (List.rev popr)
in
@ -158,7 +158,7 @@ let compile env code =
| CONST n ->
let s, env' = env#allocate in
(env', [Mov (L ((n lsl 1) lor 1), s)])
| STRING s ->
let s, env = env#string s in
let l, env = env#allocate in
@ -172,7 +172,7 @@ let compile env code =
| S _ | M _ -> [Lea (env'#loc x, eax); Mov (eax, s)]
| _ -> [Lea (env'#loc x, s)]
)
| LD x ->
let s, env' = (env#variable x)#allocate in
env',
@ -181,7 +181,7 @@ let compile env code =
| _ -> [Mov (env'#loc x, s)]
)
| ST x ->
| ST x ->
let env' = env#variable x in
let s = env'#peek in
env',
@ -189,18 +189,18 @@ let compile env code =
| S _ | M _ -> [Mov (s, eax); Mov (eax, env'#loc x)]
| _ -> [Mov (s, env'#loc x)]
)
| STA ->
call env ".sta" 3
| STI ->
let v, x, env' = env#pop2 in
env'#push x,
(match x with
| S _ | M _ -> [Mov (v, edx); Mov (x, eax); Mov (edx, I eax); Mov (edx, x)]
| _ -> [Mov (v, eax); Mov (eax, I x); Mov (eax, x)]
)
)
| BINOP op ->
let x, y, env' = env#pop2 in
env'#push y,
@ -243,7 +243,7 @@ let compile env code =
Binop ("cmp", x, y);
Set (suffix op, "%al");
Sal1 eax;
Or1 eax;
Or1 eax;
Mov (eax, y)
]
)
@ -263,13 +263,13 @@ let compile env code =
Binop (op, y, edx);
Mov (L 0, edx);
Set ("ne", "%dl");
Binop (op, edx, eax);
Set ("ne", "%al");
Sal1 eax;
Or1 eax;
Mov (eax, y)
]
]
| "!!" ->
[Mov (y, eax);
Sar1 eax;
@ -280,24 +280,24 @@ let compile env code =
Sal1 eax;
Or1 eax;
Mov (eax, y)
]
]
| "+" ->
if on_stack x && on_stack y
if on_stack x && on_stack y
then [Mov (x, eax); Dec eax; Binop ("+", eax, y)]
else [Binop (op, x, y); Dec y]
| "-" ->
if on_stack x && on_stack y
if on_stack x && on_stack y
then [Mov (x, eax); Binop (op, eax, y); Or1 y]
else [Binop (op, x, y); Or1 y]
)
| LABEL s -> (if env#is_barrier then (env#drop_barrier)#retrieve_stack s else env), [Label s]
| JMP l -> (env#set_stack l)#set_barrier, [Jmp l]
| CJMP (s, l) ->
let x, env = env#pop in
env#set_stack l, [Sar1 x; (*!!!*) Binop ("cmp", L 0, x); CJmp (s, l)]
| BEGIN (f, a, l) ->
env#assert_empty_stack;
let env = env#enter f a l in
@ -307,8 +307,8 @@ let compile env code =
Mov (M ("$" ^ (env#allocated_size)), ecx);
Repmovsl
]
| END ->
| END ->
env#endfunc, [Label env#epilogue;
Mov (ebp, esp);
Pop ebp;
@ -316,11 +316,11 @@ let compile env code =
Meta (Printf.sprintf "\t.set\t%s,\t%d" env#lsize (env#allocated * word_size));
Meta (Printf.sprintf "\t.set\t%s,\t%d" env#allocated_size env#allocated)
]
| RET ->
| RET ->
let x, env = env#pop in
env, [Mov (x, eax); Jmp env#epilogue]
| CALL (f, n) -> call env f n
| SEXP (t, n) ->
@ -330,16 +330,16 @@ let compile env code =
| DROP ->
snd env#pop, []
| DUP ->
let x = env#peek in
let s, env = env#allocate in
env, mov x s
| SWAP ->
let x, y = env#peek2 in
env, [Push x; Push y; Pop x; Pop y]
| TAG (t, n) ->
let s1, env = env#allocate in
let s2, env = env#allocate in
@ -362,8 +362,8 @@ let compile env code =
| String -> ".string_tag_patt"
| Sexp -> ".sexp_tag_patt"
) 1
| ENTER xs ->
| ENTER xs ->
let env, code =
List.fold_left
(fun (env, code) v ->
@ -373,7 +373,7 @@ let compile env code =
(env#scope @@ List.rev xs, []) xs
in
env, List.flatten @@ List.rev code
| LEAVE -> env#unscope, []
in
let env'', code'' = compile' env' scode' in
@ -381,11 +381,11 @@ let compile env code =
in
compile' env code
(* A set of strings *)
module S = Set.Make (String)
(* A set of strings *)
module S = Set.Make (String)
(* A map indexed by strings *)
module M = Map.Make (String)
module M = Map.Make (String)
(* Environment implementation *)
class env =
@ -407,15 +407,15 @@ class env =
val max_locals_size = 0
method max_locals_size = max_locals_size
method endfunc =
if stack_slots > max_locals_size
then {< max_locals_size = stack_slots >}
else self
method show_stack =
GT.show(list) (GT.show(opnd)) stack
method print_locals =
Printf.printf "LOCALS: size = %d\n" static_size;
List.iter
@ -428,7 +428,7 @@ class env =
(* Assert empty stack *)
method assert_empty_stack = assert (stack = [])
(* check barrier condition *)
method is_barrier = barrier
@ -437,22 +437,22 @@ class env =
(* drop barrier *)
method drop_barrier = {< barrier = false >}
(* associates a stack to a label *)
method set_stack l = (*Printf.printf "Setting stack for %s\n" l;*) {< stackmap = M.add l stack stackmap >}
(* retrieves a stack for a label *)
method retrieve_stack l = (*Printf.printf "Retrieving stack for %s\n" l;*)
try {< stack = M.find l stackmap >} with Not_found -> self
(* gets a name for a global variable *)
method loc x =
try S (- (List.assoc x args) - 1)
with Not_found ->
with Not_found ->
try S (assoc x locals) with Not_found -> M ("global_" ^ x)
(* allocates a fresh position on a symbolic stack *)
method allocate =
method allocate =
let x, n =
let rec allocate' = function
| [] -> ebx , 0
@ -485,8 +485,8 @@ class env =
for i = 0 to min (String.length tag - 1) 4 do
h := (!h lsl 6) lor (String.index chars tag.[i])
done;
!h
!h
(* registers a variable in the environment *)
method variable x =
match self#loc x with
@ -500,18 +500,18 @@ class env =
let y = Printf.sprintf "string_%d" scount in
let m = M.add x y stringm in
y, {< scount = scount + 1; stringm = m>}
(* gets all global variables *)
(* gets all global variables *)
method globals = S.elements globals
(* gets all string definitions *)
(* gets all string definitions *)
method strings = M.bindings stringm
(* gets a number of stack positions allocated *)
method allocated = stack_slots
method allocated_size = Printf.sprintf "LS%s_SIZE" fname
(* enters a function *)
method enter f a l =
let n = List.length l in
@ -527,10 +527,10 @@ class env =
method unscope =
let n = List.length (List.hd locals) in
{< static_size = static_size - n; locals = List.tl locals >}
(* returns a label for the epilogue *)
method epilogue = Printf.sprintf "L%s_epilogue" fname
(* returns a name for local size meta-symbol *)
method lsize = Printf.sprintf "L%s_SIZE" fname
@ -542,9 +542,9 @@ class env =
| _::tl -> inner (d+1) acc tl
in
inner 0 [] stack
end
(* Generates an assembler text for a program: first compiles the program into
the stack code, then generates x86 assember code, then prints the assembler file
*)
@ -552,7 +552,7 @@ let genasm (ds, stmt) =
let stmt =
Language.Expr.Seq (
Language.Expr.Ignore (Language.Expr.Call (Language.Expr.Var "__gc_init", [])),
Language.Expr.Seq (stmt, Language.Expr.Return (Some (Language.Expr.Call (Language.Expr.Var "raw", [Language.Expr.Const 0]))))
Language.Expr.Seq (stmt, Language.Expr.Return (Some (Language.Expr.Call (Language.Expr.Var "raw", [Language.Expr.Const 0]))))
)
in
let env, code =
@ -582,4 +582,3 @@ let build prog name =
close_out outf;
let inc = try Sys.getenv "RC_RUNTIME" with _ -> "../runtime" in
Sys.command (Printf.sprintf "gcc -g -m32 -o %s %s.s %s/runtime.a" name name inc)