informatica:ae:asmsource
Differenze
Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.
| Prossima revisione | Revisione precedente | ||
| informatica:ae:asmsource [08/04/2011 alle 13:05 (15 anni fa)] – creata Marco Danelutto | informatica:ae:asmsource [08/04/2011 alle 13:13 (15 anni fa)] (versione attuale) – Marco Danelutto | ||
|---|---|---|---|
| Linea 1: | Linea 1: | ||
| ===== asm.ml ===== | ===== asm.ml ===== | ||
| + | [[http:// | ||
| <code ocaml " | <code ocaml " | ||
| Linea 326: | Linea 327: | ||
| List.map delab p;; | List.map delab p;; | ||
| - | |||
| - | (** shortcut to maps ... *) | ||
| - | type assoc = Ass of string * int;; | ||
| - | |||
| - | (** checks whether a key is in a map *) | ||
| - | let rec hasKey k = function | ||
| - | [] -> false | ||
| - | | Ass(kk, | ||
| - | |||
| - | (** looks up a key in a map*) | ||
| - | let rec valueOfKey k labs = | ||
| - | match labs with | ||
| - | [] -> failwith "key not found" | ||
| - | | Ass(kk, | ||
| - | |||
| - | (** execution environment (the state of the processor + | ||
| - | the labels compiled. | ||
| - | an environment is | ||
| - | pc, reg, mem, labels | ||
| - | *) | ||
| - | type penv = | ||
| - | Penv of int ref * int ref array * int ref array * assoc list;; | ||
| - | |||
| - | (** pretty print the environment *) | ||
| - | let dump penv = | ||
| - | match penv with | ||
| - | | ||
| - | printf "PC=%d \n" !pc; | ||
| - | pp_reg_set r; | ||
| - | pp_mem m | ||
| - | ;; | ||
| - | |||
| - | (** execute one instruction within an environment | ||
| - | @param i the instruction to be executed | ||
| - | @param env the initial environment. it is modified via side effects *) | ||
| - | let exec_i i env = | ||
| - | match env with | ||
| - | Penv(pc, | ||
| - | (match i with | ||
| - | ADD(Reg(a), | ||
| - | | | ||
| - | | | ||
| - | | | ||
| - | | | ||
| - | | | ||
| - | | | ||
| - | | | ||
| - | | | ||
| - | let ind = !(r.(a)) + !(r.(b)) in | ||
| - | r.(c) := !(m.(ind)); pc := !pc + 1 | ||
| - | | | ||
| - | let ind = !(r.(a)) + b in | ||
| - | r.(c) := !(m.(ind)); pc := !pc + 1 | ||
| - | | | ||
| - | let ind = !(r.(a)) + !(r.(b)) in | ||
| - | m.(ind) := !(r.(c)); pc := !pc + 1 | ||
| - | | | ||
| - | let ind = !(r.(a)) + b in | ||
| - | m.(ind) := !(r.(c)); pc := !pc + 1 | ||
| - | | | ||
| - | r.(ret):= !pc + 1; | ||
| - | pc := !(r.(f)) | ||
| - | | | ||
| - | | | ||
| - | let l = valueOfKey ll labs in | ||
| - | pc := !pc + l | ||
| - | | | ||
| - | | ||
| - | then pc := !pc + l | ||
| - | else pc := !pc + 1 | ||
| - | | | ||
| - | | ||
| - | then pc := !pc + l | ||
| - | else pc := !pc + 1 | ||
| - | | | ||
| - | | ||
| - | then pc := !pc + l | ||
| - | else pc := !pc + 1 | ||
| - | | | ||
| - | | ||
| - | then pc := !pc + l | ||
| - | else pc := !pc + 1 | ||
| - | | | ||
| - | | ||
| - | then pc := !pc + l | ||
| - | else pc := !pc + 1 | ||
| - | | | ||
| - | | ||
| - | then pc := !pc + l | ||
| - | else pc := !pc + 1 | ||
| - | | _ -> printf " | ||
| - | ;; | ||
| - | |||
| - | (** compile labels. Takes a program with labels and returns | ||
| - | a map with the label addresses | ||
| - | @param pgm the program | ||
| - | @param addr the initial address of the program | ||
| - | let rec labels pgm addr = | ||
| - | match pgm with | ||
| - | [] -> [] | ||
| - | | i::ri -> | ||
| - | (match i with | ||
| - | | ||
| - | | LabInstr(LabLab(l), | ||
| - | | LabInstr(LabOff(l), | ||
| - | ) | ||
| - | ;; | ||
| </ | </ | ||
| + | [[http:// | ||
informatica/ae/asmsource.1302267929.txt.gz · Ultima modifica: 08/04/2011 alle 13:05 (15 anni fa) da Marco Danelutto
