| 1 |
|
| 2 |
(* |
| 3 |
* Generate the <arch>Instr signature and functor. |
| 4 |
* This structure contains the definition of the instruction set. |
| 5 |
*) |
| 6 |
|
| 7 |
functor MDLGenInstr(Comp : MDL_COMPILE) : MDL_GEN_MODULE = |
| 8 |
struct |
| 9 |
|
| 10 |
structure Ast = Comp.Ast |
| 11 |
structure Comp = Comp |
| 12 |
|
| 13 |
open Ast Comp.Util |
| 14 |
|
| 15 |
val toLower = String.map Char.toLower |
| 16 |
|
| 17 |
val instructionDatatype = |
| 18 |
$["and instruction =", |
| 19 |
" LIVE of {regs: C.cellset, spilled: C.cellset}", |
| 20 |
"| KILL of {regs: C.cellset, spilled: C.cellset}", |
| 21 |
"| COPY of {k: CellsBasis.cellkind, ", |
| 22 |
" sz: int, (* in bits *)", |
| 23 |
" dst: CellsBasis.cell list,", |
| 24 |
" src: CellsBasis.cell list,", |
| 25 |
" tmp: ea option (* NONE if |dst| = {src| = 1 *)}", |
| 26 |
"| ANNOTATION of {i:instruction, a:Annotations.annotation}", |
| 27 |
"| INSTR of instr" |
| 28 |
] |
| 29 |
|
| 30 |
fun gen md = |
| 31 |
let (* name of the structure/signature *) |
| 32 |
val strName = Comp.strname md "Instr" |
| 33 |
val sigName = Comp.signame md "INSTR" |
| 34 |
|
| 35 |
(* The datatype that defines the instruction set *) |
| 36 |
val instructions = Comp.instructions md |
| 37 |
val instrDatatype = |
| 38 |
DATATYPEdecl([DATATYPE("instr",[],instructions)],[]) |
| 39 |
|
| 40 |
(* Arguments to the instruction functor *) |
| 41 |
val args = ["T: MLTREE"] |
| 42 |
|
| 43 |
(* the shorthand functions *) |
| 44 |
val instrTy = IDty(IDENT([],"instruction")) |
| 45 |
val shortHandSig = |
| 46 |
map (fn CONSbind{id,ty=NONE,...} => |
| 47 |
VALSIGdecl([toLower id],instrTy) |
| 48 |
| CONSbind{id,ty=SOME ty, ...} => |
| 49 |
VALSIGdecl([toLower id],FUNty(ty,instrTy))) |
| 50 |
instructions |
| 51 |
val shortHandFuns = |
| 52 |
VALdecl( |
| 53 |
map (fn CONSbind{id,ty=NONE,...} => |
| 54 |
VALbind(IDpat(toLower id), APP("INSTR",ID id)) |
| 55 |
| CONSbind{id,ty=SOME _,...} => |
| 56 |
VALbind(IDpat(toLower id), |
| 57 |
APP("o",TUPLEexp[ID "INSTR",ID id]))) |
| 58 |
instructions) |
| 59 |
|
| 60 |
(* The signature *) |
| 61 |
val sigBody = |
| 62 |
[$ ["structure C : "^Comp.signame md "CELLS", |
| 63 |
"structure CB : CELLS_BASIS = CellsBasis", |
| 64 |
"structure T : MLTREE", |
| 65 |
"structure Constant: CONSTANT", |
| 66 |
"structure Region : REGION", |
| 67 |
" sharing Constant = T.Constant", |
| 68 |
" sharing Region = T.Region" |
| 69 |
], |
| 70 |
Comp.typeOf md "Instruction", |
| 71 |
instrDatatype, |
| 72 |
instructionDatatype |
| 73 |
] @ shortHandSig |
| 74 |
|
| 75 |
(* The functor *) |
| 76 |
val strBody = |
| 77 |
[$ ["structure C = "^Comp.strname md "Cells", |
| 78 |
"structure CB = CellsBasis", |
| 79 |
"structure T = T", |
| 80 |
"structure Region = T.Region", |
| 81 |
"structure Constant = T.Constant" |
| 82 |
], |
| 83 |
Comp.declOf md "Instruction", |
| 84 |
instrDatatype, |
| 85 |
instructionDatatype, |
| 86 |
shortHandFuns |
| 87 |
] |
| 88 |
|
| 89 |
val _ = Comp.require md "Instruction" |
| 90 |
{types =["ea","operand", "addressing_mode"], |
| 91 |
values=[] |
| 92 |
} |
| 93 |
|
| 94 |
in Comp.codegen md "instructions/Instr" |
| 95 |
[Comp.mkSig md "INSTR" (map Comp.Trans.stripMarks sigBody), |
| 96 |
Comp.mkFct md "Instr" args sigName |
| 97 |
(map Comp.Trans.stripMarks strBody) |
| 98 |
] |
| 99 |
end |
| 100 |
end |