This repository has been archived on 2022-12-27. You can view files and clone it, but cannot push or open issues or pull requests.
compilateurMIPS/mips.ml

33 lines
751 B
OCaml
Raw Normal View History

2022-12-06 22:22:48 +01:00
type reg = V0
2022-12-06 20:39:15 +01:00
type label = string
2022-12-06 22:22:48 +01:00
type instr = Li of reg * int
type directive = Asciiz of string
2022-12-06 20:39:15 +01:00
type decl = label * directive
2022-12-06 22:22:48 +01:00
type asm =
{ text : instr list
; data : decl list
}
2022-12-06 20:39:15 +01:00
let ps = Printf.sprintf (* alias raccourci *)
let fmt_reg = function
2022-12-06 22:22:48 +01:00
| V0 -> "$v0"
;;
2022-12-06 20:39:15 +01:00
let fmt_instr = function
| Li (r, i) -> ps " li %s, %d" (fmt_reg r) i
2022-12-06 22:22:48 +01:00
;;
2022-12-06 20:39:15 +01:00
let fmt_dir = function
2022-12-06 22:22:48 +01:00
| Asciiz s -> ps ".asciiz \"%s\"" s
;;
2022-12-06 20:39:15 +01:00
let emit oc asm =
2022-12-06 22:22:48 +01:00
Printf.fprintf oc ".text\n.globl main\nmain:\n";
List.iter (fun i -> Printf.fprintf oc "%s\n" (fmt_instr i)) asm.text;
Printf.fprintf oc " move $a0, $v0\n li $v0, 1\n syscall\n jr $ra\n";
Printf.fprintf oc "\n.data\n";
2022-12-06 20:39:15 +01:00
List.iter (fun (l, d) -> Printf.fprintf oc "%s: %s\n" l (fmt_dir d)) asm.data
2022-12-06 22:22:48 +01:00
;;