2018-05-16 09:24:40 +03:00
|
|
|
open GT
|
2019-10-14 19:44:33 +03:00
|
|
|
open Language
|
|
|
|
|
open SM
|
|
|
|
|
|
2018-03-05 00:54:50 +03:00
|
|
|
(* X86 codegeneration interface *)
|
|
|
|
|
|
|
|
|
|
(* The registers: *)
|
2018-03-07 10:13:34 +03:00
|
|
|
let regs = [|"%ebx"; "%ecx"; "%esi"; "%edi"; "%eax"; "%edx"; "%ebp"; "%esp"|]
|
2018-03-05 00:54:50 +03:00
|
|
|
|
2019-09-10 00:46:10 +03:00
|
|
|
(* We can not freely operate with all register; only 3 by now *)
|
2018-03-05 00:54:50 +03:00
|
|
|
let num_of_regs = Array.length regs - 5
|
|
|
|
|
|
|
|
|
|
(* We need to know the word size to calculate offsets correctly *)
|
2018-05-16 09:24:40 +03:00
|
|
|
let word_size = 4;;
|
2018-03-05 00:54:50 +03:00
|
|
|
|
|
|
|
|
(* We need to distinguish the following operand types: *)
|
2019-09-10 00:46:10 +03:00
|
|
|
@type opnd =
|
2019-10-16 21:07:27 +03:00
|
|
|
| R of int (* hard register *)
|
|
|
|
|
| S of int (* a position on the hardware stack *)
|
2019-12-29 01:12:40 +03:00
|
|
|
| C (* a saved closure *)
|
2019-10-16 21:07:27 +03:00
|
|
|
| M of string (* a named memory location *)
|
|
|
|
|
| L of int (* an immediate operand *)
|
|
|
|
|
| I of int * opnd (* an indirect operand with offset *)
|
2018-05-16 09:24:40 +03:00
|
|
|
with show
|
2018-03-05 00:54:50 +03:00
|
|
|
|
2018-11-21 14:23:35 +03:00
|
|
|
let show_opnd = show(opnd)
|
2019-09-10 00:46:10 +03:00
|
|
|
|
|
|
|
|
(* For convenience we define the following synonyms for the registers: *)
|
2018-03-05 00:54:50 +03:00
|
|
|
let ebx = R 0
|
|
|
|
|
let ecx = R 1
|
|
|
|
|
let esi = R 2
|
|
|
|
|
let edi = R 3
|
|
|
|
|
let eax = R 4
|
|
|
|
|
let edx = R 5
|
|
|
|
|
let ebp = R 6
|
|
|
|
|
let esp = R 7
|
|
|
|
|
|
|
|
|
|
(* Now x86 instruction (we do not need all of them): *)
|
|
|
|
|
type instr =
|
2019-04-10 22:15:08 +03:00
|
|
|
(* copies a value from the first to the second operand *) | Mov of opnd * opnd
|
|
|
|
|
(* loads an address of the first operand into the second *) | Lea of opnd * opnd
|
|
|
|
|
(* makes a binary operation; note, the first operand *) | Binop of string * opnd * opnd
|
|
|
|
|
(* designates x86 operator, not the source language one *)
|
|
|
|
|
(* 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
|
2019-09-10 00:46:10 +03:00
|
|
|
(* suffix, which determines the value being set, the *)
|
2019-04-10 22:15:08 +03:00
|
|
|
(* 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
|
|
|
|
|
(* call a function by a name *) | Call of string
|
2019-10-16 01:13:52 +03:00
|
|
|
(* call a function by indirect address *) | CallI of opnd
|
2019-04-10 22:15:08 +03:00
|
|
|
(* returns from a function *) | Ret
|
|
|
|
|
(* a label in the code *) | Label of string
|
|
|
|
|
(* a conditional jump *) | CJmp of string * string
|
|
|
|
|
(* a non-conditional jump *) | Jmp of string
|
|
|
|
|
(* directive *) | Meta of string
|
2018-10-23 23:18:00 +03:00
|
|
|
|
2019-04-10 22:15:08 +03:00
|
|
|
(* arithmetic correction: decrement *) | Dec of opnd
|
|
|
|
|
(* arithmetic correction: or 0x0001 *) | Or1 of opnd
|
|
|
|
|
(* arithmetic correction: shl 1 *) | Sal1 of opnd
|
|
|
|
|
(* arithmetic correction: shr 1 *) | Sar1 of opnd
|
2019-09-10 00:46:10 +03:00
|
|
|
| Repmovsl
|
2018-03-05 00:54:50 +03:00
|
|
|
(* Instruction printer *)
|
|
|
|
|
let show instr =
|
|
|
|
|
let binop = function
|
|
|
|
|
| "+" -> "addl"
|
|
|
|
|
| "-" -> "subl"
|
|
|
|
|
| "*" -> "imull"
|
|
|
|
|
| "&&" -> "andl"
|
2019-09-10 00:46:10 +03:00
|
|
|
| "!!" -> "orl"
|
2018-03-05 00:54:50 +03:00
|
|
|
| "^" -> "xorl"
|
|
|
|
|
| "cmp" -> "cmpl"
|
|
|
|
|
| _ -> failwith "unknown binary operator"
|
|
|
|
|
in
|
2019-04-10 22:15:08 +03:00
|
|
|
let rec opnd = function
|
2019-10-16 21:07:27 +03:00
|
|
|
| R i -> regs.(i)
|
2019-12-29 01:12:40 +03:00
|
|
|
| C -> "4(%ebp)"
|
2019-10-16 21:07:27 +03:00
|
|
|
| S i -> if i >= 0
|
|
|
|
|
then Printf.sprintf "-%d(%%ebp)" ((i+1) * word_size)
|
|
|
|
|
else Printf.sprintf "%d(%%ebp)" (8+(-i-1) * word_size)
|
|
|
|
|
| M x -> x
|
|
|
|
|
| L i -> Printf.sprintf "$%d" i
|
|
|
|
|
| I (0, x) -> Printf.sprintf "(%s)" (opnd x)
|
|
|
|
|
| I (n, x) -> Printf.sprintf "%d(%s)" n (opnd x)
|
2018-03-05 00:54:50 +03:00
|
|
|
in
|
|
|
|
|
match instr with
|
|
|
|
|
| Cltd -> "\tcltd"
|
|
|
|
|
| Set (suf, s) -> Printf.sprintf "\tset%s\t%s" suf s
|
|
|
|
|
| IDiv s1 -> Printf.sprintf "\tidivl\t%s" (opnd s1)
|
|
|
|
|
| Binop (op, s1, s2) -> Printf.sprintf "\t%s\t%s,\t%s" (binop op) (opnd s1) (opnd s2)
|
|
|
|
|
| Mov (s1, s2) -> Printf.sprintf "\tmovl\t%s,\t%s" (opnd s1) (opnd s2)
|
2019-04-10 22:15:08 +03:00
|
|
|
| Lea (x, y) -> Printf.sprintf "\tleal\t%s,\t%s" (opnd x) (opnd y)
|
2018-03-05 00:54:50 +03:00
|
|
|
| Push s -> Printf.sprintf "\tpushl\t%s" (opnd s)
|
|
|
|
|
| Pop s -> Printf.sprintf "\tpopl\t%s" (opnd s)
|
|
|
|
|
| Ret -> "\tret"
|
|
|
|
|
| Call p -> Printf.sprintf "\tcall\t%s" p
|
2019-10-16 01:13:52 +03:00
|
|
|
| CallI o -> Printf.sprintf "\tcall\t*(%s)" (opnd o)
|
2018-03-11 22:30:01 +03:00
|
|
|
| Label l -> Printf.sprintf "%s:\n" l
|
|
|
|
|
| Jmp l -> Printf.sprintf "\tjmp\t%s" l
|
|
|
|
|
| CJmp (s , l) -> Printf.sprintf "\tj%s\t%s" s l
|
2018-04-11 00:47:46 +03:00
|
|
|
| Meta s -> Printf.sprintf "%s\n" s
|
2018-10-23 23:18:00 +03:00
|
|
|
| Dec s -> Printf.sprintf "\tdecl\t%s" (opnd s)
|
|
|
|
|
| Or1 s -> Printf.sprintf "\torl\t$0x0001,\t%s" (opnd s)
|
|
|
|
|
| Sal1 s -> Printf.sprintf "\tsall\t%s" (opnd s)
|
|
|
|
|
| Sar1 s -> Printf.sprintf "\tsarl\t%s" (opnd s)
|
2018-12-12 12:42:38 +03:00
|
|
|
| Repmovsl -> Printf.sprintf "\trep movsl\t"
|
2019-09-10 00:46:10 +03:00
|
|
|
|
2018-03-05 00:54:50 +03:00
|
|
|
(* Opening stack machine to use instructions without fully qualified names *)
|
|
|
|
|
open SM
|
|
|
|
|
|
|
|
|
|
(* Symbolic stack machine evaluator
|
|
|
|
|
|
|
|
|
|
compile : env -> prg -> env * instr list
|
|
|
|
|
|
2018-03-07 10:13:34 +03:00
|
|
|
Take an environment, a stack machine program, and returns a pair --- the updated environment and the list
|
2018-03-05 00:54:50 +03:00
|
|
|
of x86 instructions
|
|
|
|
|
*)
|
2019-12-24 03:59:05 +03:00
|
|
|
let compile cmd env code =
|
2019-04-11 16:24:57 +03:00
|
|
|
(* SM.print_prg code; *)
|
2018-05-16 09:24:40 +03:00
|
|
|
flush stdout;
|
2018-03-05 00:54:50 +03:00
|
|
|
let suffix = function
|
|
|
|
|
| "<" -> "l"
|
|
|
|
|
| "<=" -> "le"
|
|
|
|
|
| "==" -> "e"
|
|
|
|
|
| "!=" -> "ne"
|
|
|
|
|
| ">=" -> "ge"
|
|
|
|
|
| ">" -> "g"
|
2019-09-10 00:46:10 +03:00
|
|
|
| _ -> failwith "unknown operator"
|
2019-10-14 19:44:33 +03:00
|
|
|
in
|
2019-09-10 00:46:10 +03:00
|
|
|
let rec compile' env scode =
|
2018-03-05 00:54:50 +03:00
|
|
|
let on_stack = function S _ -> true | _ -> false in
|
2018-05-16 09:24:40 +03:00
|
|
|
let mov x s = if on_stack x && on_stack s then [Mov (x, eax); Mov (eax, s)] else [Mov (x, s)] in
|
2019-10-16 01:13:52 +03:00
|
|
|
let callc env n =
|
|
|
|
|
let pushr, popr =
|
|
|
|
|
List.split @@ List.map (fun r -> (Push r, Pop r)) (env#live_registers n)
|
|
|
|
|
in
|
2019-10-16 21:07:27 +03:00
|
|
|
let pushr, popr = env#save_closure @ pushr, env#rest_closure @ popr in
|
2019-10-16 01:13:52 +03:00
|
|
|
let env, code =
|
|
|
|
|
let rec push_args env acc = function
|
|
|
|
|
| 0 -> env, acc
|
|
|
|
|
| n -> let x, env = env#pop in
|
|
|
|
|
push_args env ((Push x)::acc) (n-1)
|
|
|
|
|
in
|
|
|
|
|
let env, pushs = push_args env [] n in
|
|
|
|
|
let pushs = List.rev pushs in
|
|
|
|
|
let closure, env = env#pop in
|
2019-12-29 01:12:40 +03:00
|
|
|
let call_closure =
|
|
|
|
|
if on_stack closure
|
|
|
|
|
then [Mov (closure, edx); Mov (edx, eax); CallI eax]
|
|
|
|
|
else [Mov (closure, edx); CallI closure]
|
|
|
|
|
in
|
|
|
|
|
env, pushr @ pushs @ call_closure @ [Binop ("+", L (word_size * List.length pushs), esp)] @ (List.rev popr)
|
2019-10-16 01:13:52 +03:00
|
|
|
in
|
|
|
|
|
let y, env = env#allocate in env, code @ [Mov (eax, y)]
|
|
|
|
|
in
|
2019-10-15 01:54:57 +03:00
|
|
|
let call env f n =
|
2019-12-29 02:12:50 +03:00
|
|
|
let f =
|
|
|
|
|
match f.[0] with '.' -> "B" ^ String.sub f 1 (String.length f - 1) | _ -> f
|
2018-04-30 17:18:41 +03:00
|
|
|
in
|
2019-12-29 02:12:50 +03:00
|
|
|
let pushr, popr =
|
|
|
|
|
List.split @@ List.map (fun r -> (Push r, Pop r)) (env#live_registers n)
|
|
|
|
|
in
|
|
|
|
|
let pushr, popr = env#save_closure @ pushr, env#rest_closure @ popr in
|
|
|
|
|
let env, code =
|
|
|
|
|
let rec push_args env acc = function
|
|
|
|
|
| 0 -> env, acc
|
|
|
|
|
| n -> let x, env = env#pop in
|
|
|
|
|
push_args env ((Push x)::acc) (n-1)
|
|
|
|
|
in
|
|
|
|
|
let env, pushs = push_args env [] n in
|
|
|
|
|
let pushs =
|
|
|
|
|
match f with
|
|
|
|
|
| "Barray" -> List.rev @@ (Push (L n)) :: pushs
|
|
|
|
|
| "Bsexp" -> List.rev @@ (Push (L n)) :: pushs
|
|
|
|
|
| "Bsta" -> pushs
|
|
|
|
|
| _ -> List.rev pushs
|
|
|
|
|
in
|
|
|
|
|
env, pushr @ pushs @ [Call f; Binop ("+", L (word_size * List.length pushs), esp)] @ (List.rev popr)
|
|
|
|
|
in
|
|
|
|
|
let y, env = env#allocate in env, code @ [Mov (eax, y)]
|
2018-04-30 17:18:41 +03:00
|
|
|
in
|
2018-03-05 00:54:50 +03:00
|
|
|
match scode with
|
|
|
|
|
| [] -> env, []
|
|
|
|
|
| instr :: scode' ->
|
2020-01-15 22:33:46 +03:00
|
|
|
let stack = "" (* env#show_stack*) in
|
2018-03-05 00:54:50 +03:00
|
|
|
let env', code' =
|
|
|
|
|
match instr with
|
2019-11-24 02:30:32 +03:00
|
|
|
| PUBLIC name -> env#register_public name, []
|
|
|
|
|
| EXTERN name -> env#register_extern name, []
|
|
|
|
|
|
2019-12-29 01:12:40 +03:00
|
|
|
| CLOSURE (name, closure) ->
|
2019-10-16 01:13:52 +03:00
|
|
|
let pushr, popr =
|
|
|
|
|
List.split @@ List.map (fun r -> (Push r, Pop r)) (env#live_registers 0)
|
|
|
|
|
in
|
|
|
|
|
let closure_len = List.length closure in
|
|
|
|
|
let push_closure =
|
|
|
|
|
List.map (fun d -> Push (env#loc d)) @@ List.rev closure
|
|
|
|
|
in
|
|
|
|
|
let s, env = env#allocate in
|
|
|
|
|
(env,
|
|
|
|
|
pushr @
|
|
|
|
|
push_closure @
|
|
|
|
|
[Push (M ("$" ^ name));
|
|
|
|
|
Push (L closure_len);
|
|
|
|
|
Call "Bclosure";
|
|
|
|
|
Binop ("+", L (word_size * (closure_len + 2)), esp);
|
|
|
|
|
Mov (eax, s)] @
|
2019-12-29 01:12:40 +03:00
|
|
|
List.rev popr @ env#reload_closure)
|
2019-10-16 01:13:52 +03:00
|
|
|
|
2018-03-08 01:00:01 +03:00
|
|
|
| CONST n ->
|
2018-03-11 22:30:01 +03:00
|
|
|
let s, env' = env#allocate in
|
2018-10-23 23:18:00 +03:00
|
|
|
(env', [Mov (L ((n lsl 1) lor 1), s)])
|
2019-09-10 00:46:10 +03:00
|
|
|
|
2018-04-30 17:18:41 +03:00
|
|
|
| STRING s ->
|
|
|
|
|
let s, env = env#string s in
|
|
|
|
|
let l, env = env#allocate in
|
2019-10-15 01:54:57 +03:00
|
|
|
let env, call = call env ".string" 1 in
|
2018-04-30 17:18:41 +03:00
|
|
|
(env, Mov (M ("$" ^ s), l) :: call)
|
2019-04-10 22:15:08 +03:00
|
|
|
|
|
|
|
|
| LDA x ->
|
|
|
|
|
let s, env' = (env#variable x)#allocate in
|
|
|
|
|
env',
|
|
|
|
|
(match s with
|
|
|
|
|
| S _ | M _ -> [Lea (env'#loc x, eax); Mov (eax, s)]
|
|
|
|
|
| _ -> [Lea (env'#loc x, s)]
|
|
|
|
|
)
|
2019-09-10 00:46:10 +03:00
|
|
|
|
2018-03-05 00:54:50 +03:00
|
|
|
| LD x ->
|
2018-11-21 14:23:35 +03:00
|
|
|
let s, env' = (env#variable x)#allocate in
|
2018-03-05 00:54:50 +03:00
|
|
|
env',
|
|
|
|
|
(match s with
|
2018-04-11 00:47:46 +03:00
|
|
|
| S _ | M _ -> [Mov (env'#loc x, eax); Mov (eax, s)]
|
|
|
|
|
| _ -> [Mov (env'#loc x, s)]
|
2018-05-16 09:24:40 +03:00
|
|
|
)
|
2019-04-11 17:31:45 +03:00
|
|
|
|
2019-09-10 00:46:10 +03:00
|
|
|
| ST x ->
|
2019-04-11 17:31:45 +03:00
|
|
|
let env' = env#variable x in
|
|
|
|
|
let s = env'#peek in
|
|
|
|
|
env',
|
|
|
|
|
(match s with
|
|
|
|
|
| S _ | M _ -> [Mov (s, eax); Mov (eax, env'#loc x)]
|
|
|
|
|
| _ -> [Mov (s, env'#loc x)]
|
|
|
|
|
)
|
2019-09-10 00:46:10 +03:00
|
|
|
|
2019-04-10 22:15:08 +03:00
|
|
|
| STA ->
|
2019-10-15 01:54:57 +03:00
|
|
|
call env ".sta" 3
|
2019-09-10 00:46:10 +03:00
|
|
|
|
2019-04-11 17:31:45 +03:00
|
|
|
| STI ->
|
2019-04-10 22:15:08 +03:00
|
|
|
let v, x, env' = env#pop2 in
|
2019-04-11 16:24:57 +03:00
|
|
|
env'#push x,
|
2019-04-10 22:15:08 +03:00
|
|
|
(match x with
|
2019-10-16 21:07:27 +03:00
|
|
|
| S _ | M _ -> [Mov (v, edx); Mov (x, eax); Mov (edx, I (0, eax)); Mov (edx, x)] @ env#reload_closure
|
|
|
|
|
| _ -> [Mov (v, eax); Mov (eax, I (0, x)); Mov (eax, x)]
|
2019-09-10 00:46:10 +03:00
|
|
|
)
|
|
|
|
|
|
2018-03-05 00:54:50 +03:00
|
|
|
| BINOP op ->
|
2018-03-11 15:41:35 +03:00
|
|
|
let x, y, env' = env#pop2 in
|
|
|
|
|
env'#push y,
|
2018-03-11 22:30:01 +03:00
|
|
|
(match op with
|
2018-10-23 23:18:00 +03:00
|
|
|
| "/" ->
|
|
|
|
|
[Mov (y, eax);
|
|
|
|
|
Sar1 eax;
|
|
|
|
|
Cltd;
|
|
|
|
|
(* x := x >> 1 ?? *)
|
|
|
|
|
Sar1 x; (*!!!*)
|
|
|
|
|
IDiv x;
|
|
|
|
|
Sal1 eax;
|
|
|
|
|
Or1 eax;
|
|
|
|
|
Mov (eax, y)
|
|
|
|
|
]
|
|
|
|
|
| "%" ->
|
2018-03-11 15:41:35 +03:00
|
|
|
[Mov (y, eax);
|
2018-10-23 23:18:00 +03:00
|
|
|
Sar1 eax;
|
2018-03-11 15:41:35 +03:00
|
|
|
Cltd;
|
2018-10-23 23:18:00 +03:00
|
|
|
(* x := x >> 1 ?? *)
|
|
|
|
|
Sar1 x; (*!!!*)
|
2018-03-11 15:41:35 +03:00
|
|
|
IDiv x;
|
2018-10-23 23:18:00 +03:00
|
|
|
Sal1 edx;
|
|
|
|
|
Or1 edx;
|
|
|
|
|
Mov (edx, y)
|
2019-10-16 21:07:27 +03:00
|
|
|
] @ env#reload_closure
|
2018-03-11 15:41:35 +03:00
|
|
|
| "<" | "<=" | "==" | "!=" | ">=" | ">" ->
|
|
|
|
|
(match x with
|
|
|
|
|
| M _ | S _ ->
|
2018-03-07 10:13:34 +03:00
|
|
|
[Binop ("^", eax, eax);
|
2018-03-05 00:54:50 +03:00
|
|
|
Mov (x, edx);
|
|
|
|
|
Binop ("cmp", edx, y);
|
|
|
|
|
Set (suffix op, "%al");
|
2018-10-23 23:18:00 +03:00
|
|
|
Sal1 eax;
|
|
|
|
|
Or1 eax;
|
2018-03-05 00:54:50 +03:00
|
|
|
Mov (eax, y)
|
2019-10-16 21:07:27 +03:00
|
|
|
] @ env#reload_closure
|
2018-03-11 15:41:35 +03:00
|
|
|
| _ ->
|
2018-03-05 00:54:50 +03:00
|
|
|
[Binop ("^" , eax, eax);
|
|
|
|
|
Binop ("cmp", x, y);
|
|
|
|
|
Set (suffix op, "%al");
|
2018-10-23 23:18:00 +03:00
|
|
|
Sal1 eax;
|
2019-09-10 00:46:10 +03:00
|
|
|
Or1 eax;
|
2018-03-05 00:54:50 +03:00
|
|
|
Mov (eax, y)
|
|
|
|
|
]
|
2018-03-11 15:41:35 +03:00
|
|
|
)
|
|
|
|
|
| "*" ->
|
2018-10-23 23:18:00 +03:00
|
|
|
if on_stack y
|
|
|
|
|
then [Dec y; Mov (x, eax); Sar1 eax; Binop (op, y, eax); Or1 eax; Mov (eax, y)]
|
|
|
|
|
else [Dec y; Mov (x, eax); Sar1 eax; Binop (op, eax, y); Or1 y]
|
2018-03-11 15:41:35 +03:00
|
|
|
| "&&" ->
|
2018-10-23 23:18:00 +03:00
|
|
|
[Dec x; (*!!!*)
|
|
|
|
|
Mov (x, eax);
|
2018-03-11 15:41:35 +03:00
|
|
|
Binop (op, x, eax);
|
|
|
|
|
Mov (L 0, eax);
|
|
|
|
|
Set ("ne", "%al");
|
2018-10-23 23:18:00 +03:00
|
|
|
|
|
|
|
|
Dec y; (*!!!*)
|
2018-03-11 15:41:35 +03:00
|
|
|
Mov (y, edx);
|
|
|
|
|
Binop (op, y, edx);
|
|
|
|
|
Mov (L 0, edx);
|
|
|
|
|
Set ("ne", "%dl");
|
2019-09-10 00:46:10 +03:00
|
|
|
|
2018-03-11 15:41:35 +03:00
|
|
|
Binop (op, edx, eax);
|
|
|
|
|
Set ("ne", "%al");
|
2018-10-23 23:18:00 +03:00
|
|
|
Sal1 eax;
|
|
|
|
|
Or1 eax;
|
2018-03-11 15:41:35 +03:00
|
|
|
Mov (eax, y)
|
2019-10-16 21:07:27 +03:00
|
|
|
] @ env#reload_closure
|
2018-03-11 15:41:35 +03:00
|
|
|
| "!!" ->
|
|
|
|
|
[Mov (y, eax);
|
2018-10-23 23:18:00 +03:00
|
|
|
Sar1 eax;
|
|
|
|
|
Sar1 x; (*!!!*)
|
2018-03-11 15:41:35 +03:00
|
|
|
Binop (op, x, eax);
|
|
|
|
|
Mov (L 0, eax);
|
|
|
|
|
Set ("ne", "%al");
|
2018-10-23 23:18:00 +03:00
|
|
|
Sal1 eax;
|
|
|
|
|
Or1 eax;
|
2018-03-11 15:41:35 +03:00
|
|
|
Mov (eax, y)
|
2019-09-10 00:46:10 +03:00
|
|
|
]
|
2018-10-23 23:18:00 +03:00
|
|
|
| "+" ->
|
2019-09-10 00:46:10 +03:00
|
|
|
if on_stack x && on_stack y
|
2018-10-23 23:18:00 +03:00
|
|
|
then [Mov (x, eax); Dec eax; Binop ("+", eax, y)]
|
|
|
|
|
else [Binop (op, x, y); Dec y]
|
|
|
|
|
| "-" ->
|
2019-09-10 00:46:10 +03:00
|
|
|
if on_stack x && on_stack y
|
2018-10-23 23:18:00 +03:00
|
|
|
then [Mov (x, eax); Binop (op, eax, y); Or1 y]
|
|
|
|
|
else [Binop (op, x, y); Or1 y]
|
2018-03-11 15:41:35 +03:00
|
|
|
)
|
2018-05-16 09:24:40 +03:00
|
|
|
| LABEL s -> (if env#is_barrier then (env#drop_barrier)#retrieve_stack s else env), [Label s]
|
2019-09-10 00:46:10 +03:00
|
|
|
|
2018-05-16 09:24:40 +03:00
|
|
|
| JMP l -> (env#set_stack l)#set_barrier, [Jmp l]
|
2019-09-10 00:46:10 +03:00
|
|
|
|
2018-03-11 22:30:01 +03:00
|
|
|
| CJMP (s, l) ->
|
|
|
|
|
let x, env = env#pop in
|
2018-10-23 23:18:00 +03:00
|
|
|
env#set_stack l, [Sar1 x; (*!!!*) Binop ("cmp", L 0, x); CJmp (s, l)]
|
2019-09-10 00:46:10 +03:00
|
|
|
|
2019-12-28 01:59:04 +03:00
|
|
|
| BEGIN (f, nargs, nlocals, closure) ->
|
2019-10-14 19:44:33 +03:00
|
|
|
env#assert_empty_stack;
|
2019-10-16 01:13:52 +03:00
|
|
|
let has_closure = closure <> [] in
|
|
|
|
|
let env = env#enter f nlocals has_closure in
|
2019-10-16 21:07:27 +03:00
|
|
|
env, (if has_closure then [Push edx] else []) @
|
|
|
|
|
[Push ebp;
|
|
|
|
|
Mov (esp, ebp);
|
|
|
|
|
Binop ("-", M ("$" ^ env#lsize), esp);
|
|
|
|
|
Mov (esp, edi);
|
|
|
|
|
Mov (M "$filler", esi);
|
|
|
|
|
Mov (M ("$" ^ (env#allocated_size)), ecx);
|
|
|
|
|
Repmovsl
|
|
|
|
|
] @
|
|
|
|
|
(if f = "main" then [Call "L__gc_init"] else [])
|
2019-10-16 01:13:52 +03:00
|
|
|
|
2019-09-10 00:46:10 +03:00
|
|
|
| END ->
|
2020-01-05 01:26:13 +03:00
|
|
|
let x, env = env#pop in
|
2019-12-24 03:59:05 +03:00
|
|
|
env#assert_empty_stack;
|
2019-10-16 21:07:27 +03:00
|
|
|
let name = env#fname in
|
|
|
|
|
env#leave, [
|
2020-01-05 01:26:13 +03:00
|
|
|
Mov (x, eax); (*!!*)
|
2019-10-16 21:07:27 +03:00
|
|
|
Label env#epilogue;
|
|
|
|
|
Mov (ebp, esp);
|
|
|
|
|
Pop ebp
|
|
|
|
|
] @
|
|
|
|
|
env#rest_closure @
|
|
|
|
|
(if name = "main" then [Binop ("^", eax, eax)] else []) @
|
|
|
|
|
[Ret;
|
|
|
|
|
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)
|
|
|
|
|
]
|
2019-09-10 00:46:10 +03:00
|
|
|
|
|
|
|
|
| RET ->
|
2020-01-05 01:26:13 +03:00
|
|
|
let x = env#peek in
|
2019-04-10 22:15:08 +03:00
|
|
|
env, [Mov (x, eax); Jmp env#epilogue]
|
2019-10-14 19:44:33 +03:00
|
|
|
|
2019-10-15 01:54:57 +03:00
|
|
|
| CALL (f, n) -> call env f n
|
|
|
|
|
|
2019-10-16 01:13:52 +03:00
|
|
|
| CALLC n -> callc env n
|
2019-10-14 19:44:33 +03:00
|
|
|
|
2018-05-11 02:40:52 +03:00
|
|
|
| SEXP (t, n) ->
|
2018-05-16 09:24:40 +03:00
|
|
|
let s, env = env#allocate in
|
2019-10-15 01:54:57 +03:00
|
|
|
let env, code = call env ".sexp" (n+1) in
|
2018-05-16 09:24:40 +03:00
|
|
|
env, [Mov (L env#hash t, s)] @ code
|
|
|
|
|
|
2018-11-05 18:21:41 +03:00
|
|
|
| DROP ->
|
|
|
|
|
snd env#pop, []
|
2019-09-10 00:46:10 +03:00
|
|
|
|
2018-05-16 09:24:40 +03:00
|
|
|
| DUP ->
|
|
|
|
|
let x = env#peek in
|
|
|
|
|
let s, env = env#allocate in
|
|
|
|
|
env, mov x s
|
2019-09-10 00:46:10 +03:00
|
|
|
|
2018-05-16 09:24:40 +03:00
|
|
|
| SWAP ->
|
|
|
|
|
let x, y = env#peek2 in
|
|
|
|
|
env, [Push x; Push y; Pop x; Pop y]
|
2019-09-10 00:46:10 +03:00
|
|
|
|
2018-11-05 20:17:11 +03:00
|
|
|
| TAG (t, n) ->
|
|
|
|
|
let s1, env = env#allocate in
|
|
|
|
|
let s2, env = env#allocate in
|
2019-10-15 01:54:57 +03:00
|
|
|
let env, code = call env ".tag" 3 in
|
2018-11-06 00:21:38 +03:00
|
|
|
env, [Mov (L env#hash t, s1); Mov (L n, s2)] @ code
|
|
|
|
|
|
|
|
|
|
| ARRAY n ->
|
|
|
|
|
let s, env = env#allocate in
|
2019-10-15 01:54:57 +03:00
|
|
|
let env, code = call env ".array_patt" 2 in
|
2018-11-06 00:21:38 +03:00
|
|
|
env, [Mov (L n, s)] @ code
|
|
|
|
|
|
2019-10-15 01:54:57 +03:00
|
|
|
| PATT StrCmp -> call env ".string_patt" 2
|
2018-11-06 00:21:38 +03:00
|
|
|
|
|
|
|
|
| PATT patt ->
|
2019-10-15 01:54:57 +03:00
|
|
|
call env
|
2018-11-06 00:21:38 +03:00
|
|
|
(match patt with
|
|
|
|
|
| Boxed -> ".boxed_patt"
|
|
|
|
|
| UnBoxed -> ".unboxed_patt"
|
|
|
|
|
| Array -> ".array_tag_patt"
|
|
|
|
|
| String -> ".string_tag_patt"
|
|
|
|
|
| Sexp -> ".sexp_tag_patt"
|
2019-10-16 01:13:52 +03:00
|
|
|
| Closure -> ".closure_tag_patt"
|
2019-10-15 01:54:57 +03:00
|
|
|
) 1
|
2019-12-24 03:59:05 +03:00
|
|
|
|
2019-12-26 00:17:34 +03:00
|
|
|
| FAIL ((line, col), value) ->
|
|
|
|
|
let v, env = if value then env#peek, env else env#pop in
|
2019-12-24 03:59:05 +03:00
|
|
|
let s, env = env#string cmd#get_infile in
|
|
|
|
|
env, [Push (L col); Push (L line); Push (M ("$" ^ s)); Push v; Call "Bmatch_failure"; Binop ("+", L (3 * word_size), esp)]
|
|
|
|
|
|
2019-10-14 19:44:33 +03:00
|
|
|
| i ->
|
|
|
|
|
invalid_arg (Printf.sprintf "invalid SM insn: %s\n" (GT.show(insn) i))
|
2018-03-05 00:54:50 +03:00
|
|
|
in
|
|
|
|
|
let env'', code'' = compile' env' scode' in
|
2018-05-16 09:24:40 +03:00
|
|
|
env'', [Meta (Printf.sprintf "# %s / % s" (GT.show(SM.insn) instr) stack)] @ code' @ code''
|
2018-03-05 00:54:50 +03:00
|
|
|
in
|
|
|
|
|
compile' env code
|
2019-10-05 00:16:50 +03:00
|
|
|
|
2019-09-10 00:46:10 +03:00
|
|
|
(* A set of strings *)
|
|
|
|
|
module S = Set.Make (String)
|
2018-03-05 00:54:50 +03:00
|
|
|
|
2018-04-30 17:18:41 +03:00
|
|
|
(* A map indexed by strings *)
|
2019-09-10 00:46:10 +03:00
|
|
|
module M = Map.Make (String)
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2018-03-05 00:54:50 +03:00
|
|
|
(* Environment implementation *)
|
2019-10-16 01:13:52 +03:00
|
|
|
class env prg =
|
2019-08-23 16:10:56 +03:00
|
|
|
let chars = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" in
|
2018-05-16 09:24:40 +03:00
|
|
|
let make_assoc l i = List.combine l (List.init (List.length l) (fun x -> x + i)) in
|
|
|
|
|
let rec assoc x = function [] -> raise Not_found | l :: ls -> try List.assoc x l with Not_found -> assoc x ls in
|
2018-03-05 00:54:50 +03:00
|
|
|
object (self)
|
2019-10-16 01:13:52 +03:00
|
|
|
inherit SM.indexer prg
|
|
|
|
|
val globals = S.empty (* a set of global variables *)
|
|
|
|
|
val stringm = M.empty (* a string map *)
|
|
|
|
|
val scount = 0 (* string count *)
|
|
|
|
|
val stack_slots = 0 (* maximal number of stack positions *)
|
|
|
|
|
val static_size = 0 (* static data size *)
|
|
|
|
|
val stack = [] (* symbolic stack *)
|
|
|
|
|
val args = [] (* function arguments *)
|
|
|
|
|
val locals = [] (* function local variables *)
|
|
|
|
|
val fname = "" (* function name *)
|
|
|
|
|
val stackmap = M.empty (* labels to stack map *)
|
|
|
|
|
val barrier = false (* barrier condition *)
|
2018-12-12 12:42:38 +03:00
|
|
|
val max_locals_size = 0
|
2019-10-16 01:13:52 +03:00
|
|
|
val has_closure = false
|
2019-11-24 02:30:32 +03:00
|
|
|
val publics = S.empty
|
|
|
|
|
val externs = S.empty
|
|
|
|
|
|
|
|
|
|
method publics = S.elements publics
|
|
|
|
|
|
|
|
|
|
method register_public name = {< publics = S.add name publics >}
|
|
|
|
|
method register_extern name = {< externs = S.add name externs >}
|
|
|
|
|
|
2018-12-12 12:42:38 +03:00
|
|
|
method max_locals_size = max_locals_size
|
2019-10-16 21:07:27 +03:00
|
|
|
|
|
|
|
|
method has_closure = has_closure
|
|
|
|
|
|
|
|
|
|
method save_closure =
|
|
|
|
|
if has_closure then [Push edx] else []
|
|
|
|
|
|
|
|
|
|
method rest_closure =
|
|
|
|
|
if has_closure then [Pop edx] else []
|
2019-09-10 00:46:10 +03:00
|
|
|
|
2019-10-16 21:07:27 +03:00
|
|
|
method reload_closure =
|
2019-12-29 01:12:40 +03:00
|
|
|
if has_closure then [Mov (C (*S 0*), edx)] else []
|
2019-10-16 21:07:27 +03:00
|
|
|
|
|
|
|
|
method fname = fname
|
|
|
|
|
|
|
|
|
|
method leave =
|
2018-12-12 12:42:38 +03:00
|
|
|
if stack_slots > max_locals_size
|
|
|
|
|
then {< max_locals_size = stack_slots >}
|
|
|
|
|
else self
|
2019-09-10 00:46:10 +03:00
|
|
|
|
2018-05-16 09:24:40 +03:00
|
|
|
method show_stack =
|
|
|
|
|
GT.show(list) (GT.show(opnd)) stack
|
2019-09-10 00:46:10 +03:00
|
|
|
|
2018-05-16 09:24:40 +03:00
|
|
|
method print_locals =
|
|
|
|
|
Printf.printf "LOCALS: size = %d\n" static_size;
|
|
|
|
|
List.iter
|
|
|
|
|
(fun l ->
|
|
|
|
|
Printf.printf "(";
|
|
|
|
|
List.iter (fun (a, i) -> Printf.printf "%s=%d " a i) l;
|
|
|
|
|
Printf.printf ")\n"
|
|
|
|
|
) locals;
|
|
|
|
|
Printf.printf "END LOCALS\n"
|
|
|
|
|
|
2019-04-11 16:24:57 +03:00
|
|
|
(* Assert empty stack *)
|
|
|
|
|
method assert_empty_stack = assert (stack = [])
|
2019-09-10 00:46:10 +03:00
|
|
|
|
2018-05-16 09:24:40 +03:00
|
|
|
(* check barrier condition *)
|
|
|
|
|
method is_barrier = barrier
|
|
|
|
|
|
|
|
|
|
(* set barrier *)
|
|
|
|
|
method set_barrier = {< barrier = true >}
|
|
|
|
|
|
|
|
|
|
(* drop barrier *)
|
|
|
|
|
method drop_barrier = {< barrier = false >}
|
2019-09-10 00:46:10 +03:00
|
|
|
|
2018-05-16 09:24:40 +03:00
|
|
|
(* associates a stack to a label *)
|
2018-05-16 16:50:36 +03:00
|
|
|
method set_stack l = (*Printf.printf "Setting stack for %s\n" l;*) {< stackmap = M.add l stack stackmap >}
|
2019-09-10 00:46:10 +03:00
|
|
|
|
2018-05-16 09:24:40 +03:00
|
|
|
(* retrieves a stack for a label *)
|
2018-05-16 16:50:36 +03:00
|
|
|
method retrieve_stack l = (*Printf.printf "Retrieving stack for %s\n" l;*)
|
2018-05-16 09:24:40 +03:00
|
|
|
try {< stack = M.find l stackmap >} with Not_found -> self
|
2019-09-10 00:46:10 +03:00
|
|
|
|
2018-03-05 00:54:50 +03:00
|
|
|
(* gets a name for a global variable *)
|
2018-04-11 00:47:46 +03:00
|
|
|
method loc x =
|
2019-10-14 19:44:33 +03:00
|
|
|
match x with
|
2019-11-29 23:56:03 +03:00
|
|
|
| Value.Global name -> M ("global_" ^ name)
|
2019-10-16 01:13:52 +03:00
|
|
|
| Value.Fun name -> M ("$" ^ name)
|
2019-10-16 21:07:27 +03:00
|
|
|
| Value.Local i -> S i
|
|
|
|
|
| Value.Arg i -> S (- (i + if has_closure then 2 else 1))
|
|
|
|
|
| Value.Access i -> I (word_size * (i+1), edx)
|
|
|
|
|
|
2018-03-05 00:54:50 +03:00
|
|
|
(* allocates a fresh position on a symbolic stack *)
|
2019-09-10 00:46:10 +03:00
|
|
|
method allocate =
|
2018-03-05 00:54:50 +03:00
|
|
|
let x, n =
|
2018-11-21 14:23:35 +03:00
|
|
|
let rec allocate' = function
|
|
|
|
|
| [] -> ebx , 0
|
|
|
|
|
| (S n)::_ -> S (n+1) , n+2
|
|
|
|
|
| (R n)::_ when n < num_of_regs -> R (n+1) , stack_slots
|
|
|
|
|
| _ -> S static_size, static_size+1
|
|
|
|
|
in
|
|
|
|
|
allocate' stack
|
2018-03-05 00:54:50 +03:00
|
|
|
in
|
|
|
|
|
x, {< stack_slots = max n stack_slots; stack = x::stack >}
|
|
|
|
|
|
|
|
|
|
(* pushes an operand to the symbolic stack *)
|
|
|
|
|
method push y = {< stack = y::stack >}
|
|
|
|
|
|
|
|
|
|
(* pops one operand from the symbolic stack *)
|
2018-04-11 00:47:46 +03:00
|
|
|
method pop = let x::stack' = stack in x, {< stack = stack' >}
|
2018-03-05 00:54:50 +03:00
|
|
|
|
|
|
|
|
(* pops two operands from the symbolic stack *)
|
|
|
|
|
method pop2 = let x::y::stack' = stack in x, y, {< stack = stack' >}
|
|
|
|
|
|
2018-05-16 09:24:40 +03:00
|
|
|
(* peeks the top of the stack (the stack does not change) *)
|
|
|
|
|
method peek = List.hd stack
|
|
|
|
|
|
|
|
|
|
(* peeks two topmost values from the stack (the stack itself does not change) *)
|
|
|
|
|
method peek2 = let x::y::_ = stack in x, y
|
|
|
|
|
|
|
|
|
|
(* tag hash: gets a hash for a string tag *)
|
|
|
|
|
method hash tag =
|
2018-10-23 14:29:30 +03:00
|
|
|
let h = Pervasives.ref 0 in
|
2018-05-16 09:24:40 +03:00
|
|
|
for i = 0 to min (String.length tag - 1) 4 do
|
|
|
|
|
h := (!h lsl 6) lor (String.index chars tag.[i])
|
|
|
|
|
done;
|
2019-09-10 00:46:10 +03:00
|
|
|
!h
|
|
|
|
|
|
2018-11-21 14:23:35 +03:00
|
|
|
(* registers a variable in the environment *)
|
|
|
|
|
method variable x =
|
2019-10-14 19:44:33 +03:00
|
|
|
match x with
|
2019-11-29 23:56:03 +03:00
|
|
|
| Value.Global name -> {< globals = S.add ("global_" ^ name) globals >}
|
2019-10-14 19:44:33 +03:00
|
|
|
| _ -> self
|
2018-03-05 00:54:50 +03:00
|
|
|
|
2018-04-30 17:18:41 +03:00
|
|
|
(* registers a string constant *)
|
|
|
|
|
method string x =
|
2019-12-31 00:59:28 +03:00
|
|
|
let escape x =
|
|
|
|
|
let n = String.length x in
|
|
|
|
|
let buf = Buffer.create (n*2) in
|
|
|
|
|
let rec iterate i =
|
|
|
|
|
if i < n
|
|
|
|
|
then (
|
2020-01-03 01:38:49 +03:00
|
|
|
(match x.[i] with
|
|
|
|
|
| '"' -> Buffer.add_string buf "\\\""
|
|
|
|
|
| '\n' -> Buffer.add_string buf "\n"
|
|
|
|
|
| '\t' -> Buffer.add_string buf "\t"
|
|
|
|
|
| c -> Buffer.add_char buf c
|
|
|
|
|
);
|
2019-12-31 00:59:28 +03:00
|
|
|
iterate (i+1)
|
|
|
|
|
)
|
|
|
|
|
in
|
|
|
|
|
iterate 0;
|
|
|
|
|
Buffer.contents buf
|
|
|
|
|
in
|
|
|
|
|
let x = escape x in
|
2018-04-30 17:18:41 +03:00
|
|
|
try M.find x stringm, self
|
|
|
|
|
with Not_found ->
|
|
|
|
|
let y = Printf.sprintf "string_%d" scount in
|
|
|
|
|
let m = M.add x y stringm in
|
|
|
|
|
y, {< scount = scount + 1; stringm = m>}
|
2019-09-10 00:46:10 +03:00
|
|
|
|
|
|
|
|
(* gets all global variables *)
|
2019-11-24 02:30:32 +03:00
|
|
|
method globals = S.elements (S.diff globals externs)
|
2018-03-05 00:54:50 +03:00
|
|
|
|
2019-09-10 00:46:10 +03:00
|
|
|
(* gets all string definitions *)
|
2018-04-30 17:18:41 +03:00
|
|
|
method strings = M.bindings stringm
|
|
|
|
|
|
2018-04-11 00:47:46 +03:00
|
|
|
(* gets a number of stack positions allocated *)
|
2018-12-12 12:42:38 +03:00
|
|
|
method allocated = stack_slots
|
2019-10-15 01:54:57 +03:00
|
|
|
|
2018-12-12 12:42:38 +03:00
|
|
|
method allocated_size = Printf.sprintf "LS%s_SIZE" fname
|
2019-10-14 19:44:33 +03:00
|
|
|
|
2018-04-11 00:47:46 +03:00
|
|
|
(* enters a function *)
|
2019-10-16 01:13:52 +03:00
|
|
|
method enter f nlocals has_closure =
|
2019-10-16 21:07:27 +03:00
|
|
|
{< static_size = nlocals; stack_slots = nlocals; stack = []; fname = f; has_closure = has_closure >}
|
2018-03-05 00:54:50 +03:00
|
|
|
|
2018-04-11 00:47:46 +03:00
|
|
|
(* returns a label for the epilogue *)
|
|
|
|
|
method epilogue = Printf.sprintf "L%s_epilogue" fname
|
2019-09-10 00:46:10 +03:00
|
|
|
|
2018-04-11 00:47:46 +03:00
|
|
|
(* returns a name for local size meta-symbol *)
|
|
|
|
|
method lsize = Printf.sprintf "L%s_SIZE" fname
|
2019-10-14 19:44:33 +03:00
|
|
|
|
2018-04-11 00:47:46 +03:00
|
|
|
(* returns a list of live registers *)
|
2018-04-30 17:18:41 +03:00
|
|
|
method live_registers depth =
|
|
|
|
|
let rec inner d acc = function
|
|
|
|
|
| [] -> acc
|
|
|
|
|
| (R _ as r)::tl -> inner (d+1) (if d >= depth then (r::acc) else acc) tl
|
|
|
|
|
| _::tl -> inner (d+1) acc tl
|
|
|
|
|
in
|
|
|
|
|
inner 0 [] stack
|
2019-09-10 00:46:10 +03:00
|
|
|
|
2018-04-11 00:47:46 +03:00
|
|
|
end
|
2019-09-10 00:46:10 +03:00
|
|
|
|
2018-03-05 01:01:36 +03:00
|
|
|
(* 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
|
2018-03-05 00:54:50 +03:00
|
|
|
*)
|
2019-11-27 03:14:25 +03:00
|
|
|
let genasm cmd prog =
|
|
|
|
|
let sm = SM.compile cmd prog in
|
2019-12-24 03:59:05 +03:00
|
|
|
let env, code = compile cmd (new env sm) sm in
|
2019-11-24 02:30:32 +03:00
|
|
|
let globals =
|
2019-12-25 20:29:53 +03:00
|
|
|
List.map (fun s -> Meta (Printf.sprintf "\t.globl\t%s" s)) env#publics
|
2019-11-24 02:30:32 +03:00
|
|
|
in
|
2019-12-25 20:29:53 +03:00
|
|
|
let data = [Meta "\t.section custom_data,\"aw\",@progbits";
|
|
|
|
|
Meta (Printf.sprintf "filler:\t.fill\t%d, 4, 1" env#max_locals_size)] @
|
2019-11-24 02:30:32 +03:00
|
|
|
(List.map (fun s -> Meta (Printf.sprintf "%s:\t.int\t1" s)) env#globals) @
|
|
|
|
|
(List.map (fun (s, v) -> Meta (Printf.sprintf "%s:\t.string\t\"%s\"" v s)) env#strings)
|
2018-12-12 12:42:38 +03:00
|
|
|
in
|
2018-03-05 00:54:50 +03:00
|
|
|
let asm = Buffer.create 1024 in
|
|
|
|
|
List.iter
|
|
|
|
|
(fun i -> Buffer.add_string asm (Printf.sprintf "%s\n" @@ show i))
|
2019-11-24 02:30:32 +03:00
|
|
|
(globals @ data @ [Meta "\t.text"] @ code);
|
2018-03-05 00:54:50 +03:00
|
|
|
Buffer.contents asm
|
|
|
|
|
|
|
|
|
|
(* Builds a program: generates the assembler file and compiles it with the gcc toolchain *)
|
2019-11-24 02:30:32 +03:00
|
|
|
let build cmd prog =
|
2019-11-27 03:14:25 +03:00
|
|
|
let find_objects imports paths =
|
|
|
|
|
let module S = Set.Make (String) in
|
|
|
|
|
let rec iterate acc s = function
|
|
|
|
|
| [] -> acc
|
|
|
|
|
| import::imports ->
|
|
|
|
|
if S.mem import s
|
|
|
|
|
then iterate acc s imports
|
|
|
|
|
else
|
|
|
|
|
let path, intfs = Interface.find import paths in
|
|
|
|
|
iterate
|
2020-01-14 17:08:35 +03:00
|
|
|
((Filename.concat path (import ^ ".o")) :: acc)
|
2019-11-27 03:14:25 +03:00
|
|
|
(S.add import s)
|
|
|
|
|
((List.map (function `Import name -> name | _ -> invalid_arg "must not happen") @@
|
|
|
|
|
List.filter (function `Import _ -> true | _ -> false) intfs) @
|
|
|
|
|
imports)
|
|
|
|
|
in
|
2019-12-18 18:44:01 +03:00
|
|
|
iterate [] (S.add "Std" S.empty) imports
|
2019-11-27 03:14:25 +03:00
|
|
|
in
|
2019-12-12 17:42:45 +03:00
|
|
|
cmd#dump_file "s" (genasm cmd prog);
|
|
|
|
|
cmd#dump_file "i" (Interface.gen prog);
|
|
|
|
|
let inc = try Sys.getenv "RC_RUNTIME" with _ -> "../runtime" in
|
2019-11-24 02:30:32 +03:00
|
|
|
match cmd#get_mode with
|
|
|
|
|
| `Default ->
|
2019-11-29 23:56:03 +03:00
|
|
|
let objs = find_objects (fst @@ fst prog) cmd#get_include_paths in
|
2019-11-27 03:14:25 +03:00
|
|
|
let buf = Buffer.create 255 in
|
|
|
|
|
List.iter (fun o -> Buffer.add_string buf o; Buffer.add_string buf " ") objs;
|
2020-01-14 17:08:35 +03:00
|
|
|
let gcc_cmdline = Printf.sprintf "gcc -g -m32 -o %s %s.s %s %s/runtime.a" cmd#basename cmd#basename (Buffer.contents buf) inc in
|
|
|
|
|
Sys.command gcc_cmdline
|
2019-11-24 02:30:32 +03:00
|
|
|
| `Compile ->
|
2019-12-12 17:42:45 +03:00
|
|
|
Sys.command (Printf.sprintf "gcc -g -m32 -c %s.s" cmd#basename)
|
2019-11-24 02:30:32 +03:00
|
|
|
| _ -> invalid_arg "must not happen"
|