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/baselib.ml

51 lines
2.3 KiB
OCaml
Raw Normal View History

2022-12-06 20:39:15 +01:00
open Ast
2022-12-09 14:45:59 +01:00
open Mips
2022-12-06 22:22:48 +01:00
module Env = Map.Make (String)
2022-12-06 20:39:15 +01:00
2022-12-09 16:12:32 +01:00
let _types_ =
Env.of_seq
(List.to_seq
[ "%add", Func_t (Int_t, [ Int_t; Int_t ])
2022-12-09 16:39:44 +01:00
; "%sub", Func_t (Int_t, [ Int_t; Int_t ])
2022-12-09 16:12:32 +01:00
; "%mul", Func_t (Int_t, [ Int_t; Int_t ])
2022-12-09 16:39:44 +01:00
; "%div", Func_t (Int_t, [ Int_t; Int_t ])
2022-12-22 16:57:24 +01:00
; "%rem", Func_t (Int_t, [ Int_t; Int_t ])
; "%seq", Func_t (Bool_t, [ Int_t; Int_t ])
; "%sge", Func_t (Bool_t, [ Int_t; Int_t ])
; "%sgt", Func_t (Bool_t, [ Int_t; Int_t ])
; "%sle", Func_t (Bool_t, [ Int_t; Int_t ])
; "%slt", Func_t (Bool_t, [ Int_t; Int_t ])
; "%sne", Func_t (Bool_t, [ Int_t; Int_t ])
2022-12-22 19:15:42 +01:00
; "%and", Func_t (Int_t, [ Int_t; Int_t ])
2022-12-22 19:20:59 +01:00
; "%or", Func_t (Int_t, [ Int_t; Int_t ])
2022-12-13 14:36:45 +01:00
; "puti", Func_t (Void_t, [ Int_t ])
; "puts", Func_t (Void_t, [ Str_t ])
; "geti", Func_t (Int_t, [])
2022-12-22 16:51:15 +01:00
; "abs", Func_t (Int_t, [ Int_t ])
2022-12-09 16:12:32 +01:00
])
;;
2022-12-09 14:45:59 +01:00
let builtins =
2022-12-09 14:45:59 +01:00
List.fold_left
(fun env (fn, impl) -> Env.add fn impl env)
Env.empty
2022-12-09 16:58:17 +01:00
[ "%add", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); Add (V0, T0, T1) ]
; "%sub", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); Sub (V0, T0, T1) ]
; "%mul", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); Mul (V0, T0, T1) ]
; "%div", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); Div (V0, T0, T1) ]
2022-12-22 16:57:24 +01:00
; "%rem", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); Rem (V0, T0, T1) ]
; "%seq", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); Seq (V0, T0, T1) ]
; "%sge", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); Sge (V0, T0, T1) ]
; "%sgt", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); Sgt (V0, T0, T1) ]
; "%sle", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); Sle (V0, T0, T1) ]
; "%slt", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); Slt (V0, T0, T1) ]
; "%sne", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); Sne (V0, T0, T1) ]
2022-12-22 19:15:42 +01:00
; "%and", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); And (V0, T0, T1) ]
2022-12-22 19:20:59 +01:00
; "%or", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); Or (V0, T0, T1) ]
2022-12-10 17:06:08 +01:00
; "puti", [ Lw (A0, Mem (SP, 0)); Li (V0, Syscall.print_int); Syscall ]
2022-12-13 14:36:45 +01:00
; "puts", [ Lw (A0, Mem (SP, 0)); Li (V0, Syscall.print_str); Syscall ]
; "geti", [ Lw (A0, Mem (SP, 0)); Li (V0, Syscall.read_int); Syscall ]
2022-12-22 16:51:15 +01:00
; "abs", [ Lw (T0, Mem (SP, 0)); Abs (V0, T0) ]
2022-12-09 16:12:32 +01:00
]
2022-12-09 14:45:59 +01:00
;;