Debian Codegraph @

lib/mlrisc-lib/MLRISC/c-call/varargs/interp/x86-gen-fn.sml

raw
1 (* x86-gen-fn.sml
2 *
3 * X86-specific portion of the interpreter.
4 *)
5
6 functor X86GenFn (
7 structure Gen : C_CALL_GEN
8 structure MLRISCGen : MLRISC_GEN
9 where T = Gen.T
10 val push : MLRISCGen.T.rexp -> MLRISCGen.T.stm
11 val leave : MLRISCGen.T.stm
12 ) = struct
13
14 val defTy = 32
15
16 structure T = MLRISCGen.T
17 structure C = X86Cells
18 structure CB = CellsBasis
19 structure Consts = VarargConstants
20
21 fun gpr r = T.GPR(T.REG(32, r))
22 val calleeSaveRegs = List.map gpr [C.ebx, C.esi, C.edi]
23
24 structure InterpGen = GenFn (
25 structure T = Gen.T
26 val gprs = []
27 val fprs = []
28 val gprWidths = [8, 16, 32]
29 val fprWidths = [32, 64]
30 val spReg = T.REG(32, C.esp)
31 val defaultWidth = 32
32 val callerSaves = [C.eax, C.ecx, C.edx]
33 val callerSavesF = []
34 structure CCallGen = Gen
35 structure SA = Gen.SA)
36
37 fun lit i = T.LI (T.I.fromInt (defTy, i))
38
39 (* get the ith argument in the calling sequence *)
40 fun getArg i =
41 T.LOAD(defTy, T.ADD(defTy, T.REG(defTy, C.ebp), lit (4*i+8)), T.Region.memory)
42
43 (* MLRISC code for the x86 vararg interpreter *)
44 fun gen () = let
45 val largsReg = C.newReg()
46 (* we align the frame to a 16-bytes to support Mac OS. *)
47 val frameSzB = 1024*4-2*4
48 val interpFunPtr = getArg 0
49 val endOfLargs = getArg 2
50 in
51 List.concat [
52 (* preserve callee-save registers *)
53 [T.LIVE calleeSaveRegs],
54 [push (T.REG(defTy, C.ebp))],
55 [T.COPY (defTy, [C.ebp], [C.esp])],
56 [T.MV(defTy, largsReg, getArg 1)],
57 (* allocate stack space for the arguments *)
58 [T.MV(defTy, C.esp, T.SUB(defTy, T.REG(defTy, C.esp), lit frameSzB))],
59 InterpGen.gen {interpFunPtr=interpFunPtr, largsReg=largsReg, endOfLargs=endOfLargs},
60 [leave],
61 [T.LIVE calleeSaveRegs],
62 [T.RET []]
63 ]
64 end
65
66 fun main (cmd, args) = let
67 val _ = Label.reset()
68 val lab = Label.global Consts.varargInterpreter
69 val stms = gen()
70 val asmOutStrm = TextIO.openOut "vararg-interp-x86-linux.s"
71 val _ = TextIO.output(asmOutStrm, Consts.header^"\n")
72 fun doit () = MLRISCGen.gen(lab, stms, [T.GPR (T.REG (defTy, C.eax))])
73 val _ = AsmStream.withStream asmOutStrm doit ()
74 val _ = TextIO.closeOut asmOutStrm
75 in
76 0
77 end
78
79 end