mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-10 08:48:49 +00:00
Inserted comments
This commit is contained in:
parent
06fb783c80
commit
0561c22217
1 changed files with 17 additions and 15 deletions
30
src/X86.ml
30
src/X86.ml
|
|
@ -13,8 +13,8 @@ let word_size = 4
|
||||||
type opnd =
|
type opnd =
|
||||||
| R of int (* hard register *)
|
| R of int (* hard register *)
|
||||||
| S of int (* a position on the hardware stack *)
|
| S of int (* a position on the hardware stack *)
|
||||||
| M of string (* named memory location *)
|
| M of string (* a named memory location *)
|
||||||
| L of int (* immediate operand *)
|
| L of int (* an immediate operand *)
|
||||||
|
|
||||||
(* 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 ebx = R 0
|
||||||
|
|
@ -28,15 +28,18 @@ let esp = R 7
|
||||||
|
|
||||||
(* Now x86 instruction (we do not need all of them): *)
|
(* Now x86 instruction (we do not need all of them): *)
|
||||||
type instr =
|
type instr =
|
||||||
| Cltd
|
(* copies a value from the first to the second operand *) | Mov of opnd * opnd
|
||||||
| Set of string * string
|
(* makes a binary operation; note, the first operand *) | Binop of string * opnd * opnd
|
||||||
| IDiv of opnd
|
(* designates x86 operator, not the source language one *)
|
||||||
| Binop of string * opnd * opnd
|
(* x86 integer division, see instruction set reference *) | IDiv of opnd
|
||||||
| Mov of opnd * opnd
|
(* see instruction set reference *) | Cltd
|
||||||
| Push of opnd
|
(* sets a value from flags; the first operand is the *) | Set of string * string
|
||||||
| Pop of opnd
|
(* suffix, which determines the value being set, the *)
|
||||||
| Ret
|
(* the second --- (sub)register name *)
|
||||||
| Call of string
|
(* 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
|
||||||
|
(* returns from a function *) | Ret
|
||||||
|
|
||||||
(* Instruction printer *)
|
(* Instruction printer *)
|
||||||
let show instr =
|
let show instr =
|
||||||
|
|
@ -74,7 +77,7 @@ open SM
|
||||||
|
|
||||||
compile : env -> prg -> env * instr list
|
compile : env -> prg -> env * instr list
|
||||||
|
|
||||||
Take an environment, a stack machine program, and returns a pair --- updated environment and the list
|
Take an environment, a stack machine program, and returns a pair --- the updated environment and the list
|
||||||
of x86 instructions
|
of x86 instructions
|
||||||
*)
|
*)
|
||||||
let compile env code =
|
let compile env code =
|
||||||
|
|
@ -131,7 +134,7 @@ let compile env code =
|
||||||
| "<" | "<=" | "==" | "!=" | ">=" | ">" ->
|
| "<" | "<=" | "==" | "!=" | ">=" | ">" ->
|
||||||
(match x with
|
(match x with
|
||||||
| M _ | S _ ->
|
| M _ | S _ ->
|
||||||
[Binop ("^" , eax, eax);
|
[Binop ("^", eax, eax);
|
||||||
Mov (x, edx);
|
Mov (x, edx);
|
||||||
Binop ("cmp", edx, y);
|
Binop ("cmp", edx, y);
|
||||||
Set (suffix op, "%al");
|
Set (suffix op, "%al");
|
||||||
|
|
@ -185,7 +188,6 @@ let compile env code =
|
||||||
(* A set of strings *)
|
(* A set of strings *)
|
||||||
module S = Set.Make (String)
|
module S = Set.Make (String)
|
||||||
|
|
||||||
|
|
||||||
(* Environment implementation *)
|
(* Environment implementation *)
|
||||||
class env =
|
class env =
|
||||||
object (self)
|
object (self)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue