| 1 |
(* |
| 2 |
* Compile the machine description into an internal digestable form |
| 3 |
*) |
| 4 |
functor MDLCompile |
| 5 |
(structure Env : MDL_ENV |
| 6 |
structure AstPP : MDL_AST_PRETTY_PRINTER |
| 7 |
structure AstUtil : MDL_AST_UTIL |
| 8 |
structure Trans : MDL_AST_TRANSLATION |
| 9 |
structure Consts : MDL_AST_CONSTANTS |
| 10 |
structure Rewriter : MDL_AST_REWRITER |
| 11 |
structure TypeUtils : MDL_TYPE_UTILS |
| 12 |
sharing Env.Ast = AstPP.Ast = Trans.Ast = AstUtil.Ast = |
| 13 |
Rewriter.Ast = Consts.Ast = TypeUtils.Ast |
| 14 |
) : MDL_COMPILE = |
| 15 |
struct |
| 16 |
|
| 17 |
structure Env = Env |
| 18 |
structure Ast = Env.Ast |
| 19 |
structure Trans = Trans |
| 20 |
structure Rewriter = Rewriter |
| 21 |
structure Consts = Consts |
| 22 |
structure Error = MDLError |
| 23 |
structure AstPP = AstPP |
| 24 |
structure Util = AstUtil |
| 25 |
structure TypeUtils = TypeUtils |
| 26 |
structure H = HashTable |
| 27 |
|
| 28 |
open Error Ast Util |
| 29 |
|
| 30 |
type filename = string |
| 31 |
|
| 32 |
infix ++ |
| 33 |
|
| 34 |
val op ++ = Env.++ |
| 35 |
|
| 36 |
datatype 'a slot = EMPTY of string | SLOT of string * 'a |
| 37 |
|
| 38 |
(* machine description *) |
| 39 |
datatype md = MD of |
| 40 |
{env : Env.env ref, |
| 41 |
endianess : Ast.endianess slot ref, |
| 42 |
archKind : Ast.archKind slot ref, |
| 43 |
asmCase : Ast.assemblycase slot ref, |
| 44 |
name : string slot ref, |
| 45 |
filename : filename, |
| 46 |
cells : Ast.storagedecl list ref, |
| 47 |
locations : Ast.locbind list ref, |
| 48 |
formats : (int option * Ast.formatbind) list ref, |
| 49 |
instructions: Ast.consbind list slot ref, |
| 50 |
debug : string list ref, |
| 51 |
cpus : Ast.cpubind list slot ref, |
| 52 |
pipelines : Ast.pipelinebind list slot ref, |
| 53 |
resources : Ast.id list slot ref, |
| 54 |
latencies : Ast.latencybind list slot ref |
| 55 |
} |
| 56 |
|
| 57 |
fun getSlot(ref(EMPTY name)) = Error.fail(name^" has not been declared") |
| 58 |
| getSlot(ref(SLOT(_,x))) = x |
| 59 |
|
| 60 |
fun getSlot'(ref(EMPTY _)) = [] |
| 61 |
| getSlot'(ref(SLOT(_,x))) = x |
| 62 |
|
| 63 |
fun putSlot(s as ref(EMPTY name),x) = s := SLOT(name,x) |
| 64 |
| putSlot(s as ref(SLOT(name,_)),x) = |
| 65 |
Error.error("duplicate declaration of "^name) |
| 66 |
|
| 67 |
fun putSlot'(s as ref(EMPTY name),x) = s := SLOT(name,x) |
| 68 |
| putSlot'(s as ref(SLOT(name,_)),x) = s := SLOT(name,x) |
| 69 |
|
| 70 |
(* Extract info from a machine description *) |
| 71 |
fun endianess(MD{endianess, ...}) = getSlot(endianess) |
| 72 |
fun archKind(MD{archKind, ...}) = getSlot(archKind) |
| 73 |
fun asmCase(MD{asmCase, ...}) = getSlot(asmCase) |
| 74 |
fun name(MD{name, ...}) = getSlot(name) |
| 75 |
fun filename(MD{filename, ...}) = filename |
| 76 |
fun env(MD{env, ...}) = !env |
| 77 |
fun cells(MD{cells, ...}) = !cells |
| 78 |
fun locations(MD{locations, ...}) = !locations |
| 79 |
fun formats(MD{formats, ...}) = !formats |
| 80 |
fun instructions(MD{instructions, ...}) = getSlot instructions |
| 81 |
fun debugging(MD{debug, ...}) x = List.exists (fn x' => x = x') (!debug) |
| 82 |
fun resources(MD{resources, ...}) = getSlot' resources |
| 83 |
fun latencies(MD{latencies, ...}) = getSlot' latencies |
| 84 |
fun cpus(MD{cpus, ...}) = getSlot' cpus |
| 85 |
fun pipelines(MD{pipelines, ...}) = getSlot' pipelines |
| 86 |
|
| 87 |
(* |
| 88 |
fun cellSets(MD{cells, ...}) = |
| 89 |
ListMergeSort.sort |
| 90 |
(fn (CELLdecl{from=f1, ...}, CELLdecl{from=f2, ...}) => !f1 > !f2) |
| 91 |
(List.filter (fn CELLdecl{cellset=true, alias=NONE, ...} => true |
| 92 |
| CELLdecl _ => false) (!cells) |
| 93 |
) |
| 94 |
|
| 95 |
fun cellSetsAliases(MD{cells, ...}) = |
| 96 |
ListMergeSort.sort |
| 97 |
(fn (CELLdecl{from=f1, ...}, CELLdecl{from=f2, ...}) => !f1 > !f2) |
| 98 |
(List.filter (fn CELLdecl{cellset=true, ...} => true |
| 99 |
| CELLdecl{alias=SOME _, ...} => true |
| 100 |
| _ => false) (!cells)) |
| 101 |
*) |
| 102 |
|
| 103 |
fun lookupCellKind(MD{cells, ...}) k = |
| 104 |
let fun loop [] = fail("cellkind "^k^" not found") |
| 105 |
| loop((c as CELLdecl{id, nickname, ...})::cs) = |
| 106 |
if k = id orelse k = nickname then c else loop cs |
| 107 |
in loop (!cells) end |
| 108 |
|
| 109 |
fun lookupDatatype(MD{env, ...}) t = |
| 110 |
let val instrEnv = Env.lookupStr (!env) (IDENT([],"Instruction")) |
| 111 |
val datatypes = Env.datatypeDefinitions instrEnv |
| 112 |
fun loop [] = fail("datatype "^t^" not found") |
| 113 |
| loop((db as DATATYPEbind{id, ...})::dbs) = |
| 114 |
if t = id then db else loop dbs |
| 115 |
in loop datatypes end |
| 116 |
|
| 117 |
fun hasCopyImpl md = |
| 118 |
List.exists(fn CONSbind{id="COPY",ty=SOME(RECORDty fields),...} => |
| 119 |
List.exists(fn (id,_) => id = "impl") fields |
| 120 |
| _ => false |
| 121 |
) (instructions md) |
| 122 |
|
| 123 |
(* Extract info from the environment *) |
| 124 |
val declOf = Env.declOf o env |
| 125 |
val fctArgOf = Env.fctArgOf o env |
| 126 |
val typeOf = Env.typeOf o env |
| 127 |
|
| 128 |
(* Require the definitions of these things *) |
| 129 |
fun require md envName {values,types} = |
| 130 |
let val decls = declOf md envName |
| 131 |
val hash = HashString.hashString |
| 132 |
exception NotDefined |
| 133 |
val valueTbl = H.mkTable(hash,op =)(32,NotDefined) |
| 134 |
val typeTbl = H.mkTable(hash,op =) (32,NotDefined) |
| 135 |
fun enterDty(DATATYPEbind{id,...}) = H.insert typeTbl (id,()) |
| 136 |
| enterDty(DATATYPEEQbind{id, ...}) = H.insert typeTbl (id,()) |
| 137 |
fun enterTy(TYPEbind(id, _, _)) = H.insert typeTbl (id,()) |
| 138 |
fun enterFb(FUNbind(id, _)) = H.insert valueTbl (id,()) |
| 139 |
|
| 140 |
fun decl _ (d as DATATYPEdecl(dts,ts)) = |
| 141 |
(app enterDty dts; app enterTy ts; d) |
| 142 |
| decl _ (d as FUNdecl fbs) = (app enterFb fbs; d) |
| 143 |
| decl _ d = d |
| 144 |
val NO = Rewriter.noRewrite |
| 145 |
val _ = #decl(Rewriter.rewrite{decl=decl, exp=NO, pat=NO, |
| 146 |
ty=NO, sexp=NO}) decls |
| 147 |
fun check kind table id = |
| 148 |
(H.lookup table id) |
| 149 |
handle _ => warning("missing "^kind^" "^envName^"."^id) |
| 150 |
in app (check "function" valueTbl) values; |
| 151 |
app (check "type" typeTbl) types |
| 152 |
end |
| 153 |
|
| 154 |
(* Compile an AST into a machine description *) |
| 155 |
|
| 156 |
fun compile(filename, decls) = |
| 157 |
let val endianess = ref(EMPTY "endianess") |
| 158 |
val archKind = ref(EMPTY "architecture") |
| 159 |
val asmCase = ref(EMPTY "assembly case") |
| 160 |
val name = ref(EMPTY "module name") |
| 161 |
val instructions= ref(EMPTY "instructions") |
| 162 |
val pipelines = ref(EMPTY "pipelines") |
| 163 |
val resources = ref(EMPTY "resources") |
| 164 |
val latencies = ref(EMPTY "latencies") |
| 165 |
val cpus = ref(EMPTY "cpus") |
| 166 |
val env = ref Env.empty |
| 167 |
val cells = ref [] |
| 168 |
val locations = ref [] |
| 169 |
val debug = ref [] |
| 170 |
val formats = ref [] |
| 171 |
val md = MD{env =env, |
| 172 |
endianess=endianess, |
| 173 |
archKind =archKind, |
| 174 |
asmCase =asmCase, |
| 175 |
name =name, |
| 176 |
filename =filename, |
| 177 |
cells =cells, |
| 178 |
locations=locations, |
| 179 |
formats =formats, |
| 180 |
instructions=instructions, |
| 181 |
debug =debug, |
| 182 |
cpus =cpus, |
| 183 |
resources=resources, |
| 184 |
pipelines=pipelines, |
| 185 |
latencies=latencies |
| 186 |
} |
| 187 |
fun decl d = env := ((!env) ++ Env.elab (!env) d) |
| 188 |
fun D d = |
| 189 |
case d of |
| 190 |
(* ML code *) |
| 191 |
DATATYPEdecl _ => decl d |
| 192 |
| FUNdecl _ => decl d |
| 193 |
| VALdecl _ => decl d |
| 194 |
| VALSIGdecl _ => decl d |
| 195 |
| TYPESIGdecl _ => decl d |
| 196 |
| LOCALdecl _ => decl d |
| 197 |
| STRUCTUREdecl _ => decl d |
| 198 |
| INFIXdecl _ => decl d |
| 199 |
| INFIXRdecl _ => decl d |
| 200 |
| NONFIXdecl _ => decl d |
| 201 |
| OPENdecl _ => decl d |
| 202 |
| SEQdecl ds => Ds ds |
| 203 |
| $ _ => () |
| 204 |
| MARKdecl(l,d) => (setLoc l; D d) |
| 205 |
|
| 206 |
(* MD Gen specific constructions *) |
| 207 |
| FORMATdecl(bits,f) => formats := !formats @ |
| 208 |
map (fn f => (bits,f)) f |
| 209 |
| STORAGEdecl d => cells := !cells @ d |
| 210 |
| LOCATIONSdecl d => locations := !locations @ d |
| 211 |
| INSTRUCTIONdecl c => (putSlot(instructions,c); decl d) |
| 212 |
| ARCHdecl(n,ds) => (putSlot(name,n); Ds(ds)) |
| 213 |
| BITSORDERINGdecl _ => error "bitsordering" |
| 214 |
| ARCHKINDdecl k => putSlot(archKind, k) |
| 215 |
| ENDIANESSdecl e => putSlot(endianess, e) |
| 216 |
| NAMEdecl n => putSlot'(name, n) |
| 217 |
| ASSEMBLYCASEdecl c => putSlot(asmCase, c) |
| 218 |
| DEBUGdecl id => (debug := id :: !debug) |
| 219 |
| PIPELINEdecl p => putSlot(pipelines, p) |
| 220 |
| CPUdecl c => putSlot(cpus, c) |
| 221 |
| RESOURCEdecl r => putSlot(resources, r) |
| 222 |
| LATENCYdecl l => putSlot(latencies, l) |
| 223 |
| _ => error "compile" |
| 224 |
|
| 225 |
and Ds [] = () |
| 226 |
| Ds (d::ds) = (D d; Ds ds) |
| 227 |
|
| 228 |
in Error.init(); |
| 229 |
Ds decls; |
| 230 |
md |
| 231 |
end |
| 232 |
|
| 233 |
|
| 234 |
(* |
| 235 |
* Code Generation methods |
| 236 |
*) |
| 237 |
|
| 238 |
type module = string |
| 239 |
type arguments = string list |
| 240 |
type signatureName = string |
| 241 |
|
| 242 |
infix ++ |
| 243 |
|
| 244 |
val op ++ = PP.++ |
| 245 |
|
| 246 |
val toupper = String.map Char.toUpper |
| 247 |
val tolower = String.map Char.toLower |
| 248 |
|
| 249 |
fun signame md suffix = toupper(name md)^suffix |
| 250 |
fun strname md suffix = name md^suffix |
| 251 |
fun fctname md suffix = name md^suffix |
| 252 |
|
| 253 |
fun mkSigCon "" = PP.nop |
| 254 |
| mkSigCon sign = PP.sp ++ PP.! ":" ++ PP.! sign |
| 255 |
|
| 256 |
fun mkSig md name body = |
| 257 |
PP.line(PP.! "signature" ++ PP.! (signame md name) ++ PP.! "=") ++ |
| 258 |
PP.line(PP.! "sig") ++ |
| 259 |
PP.block(AstPP.decls body) ++ |
| 260 |
PP.line(PP.! "end") ++ PP.nl |
| 261 |
|
| 262 |
fun mkFct' md name args sign body = |
| 263 |
PP.line(PP.! "functor" ++ PP.! (fctname md name) ++ |
| 264 |
PP.group("(",")") (AstPP.decl args) ++ |
| 265 |
mkSigCon sign ++ PP.! "=") ++ |
| 266 |
PP.line(PP.! "struct") ++ |
| 267 |
PP.block(AstPP.decls body) ++ |
| 268 |
PP.line(PP.! "end") ++ PP.nl |
| 269 |
|
| 270 |
fun mkFct md name args sign body = mkFct' md name ($ args) sign body |
| 271 |
|
| 272 |
fun mkStr md name sign body = |
| 273 |
PP.line(PP.! "structure" ++ PP.! (strname md name) ++ |
| 274 |
mkSigCon sign ++ PP.!"=") ++ |
| 275 |
PP.line(PP.! "struct") ++ |
| 276 |
PP.block(AstPP.decls body) ++ |
| 277 |
PP.line(PP.! "end") ++ PP.nl |
| 278 |
|
| 279 |
fun mkCode body = PP.block(AstPP.decls body) |
| 280 |
|
| 281 |
|
| 282 |
fun pathName md module suffix = |
| 283 |
let fun getName m = |
| 284 |
OS.Path.concat(OS.Path.dir m,tolower(name md)^OS.Path.file m) |
| 285 |
val pathname = OS.Path.concat( |
| 286 |
OS.Path.dir(filename md),getName(module^suffix)) |
| 287 |
in pathname end |
| 288 |
|
| 289 |
(* Emit text into a file *) |
| 290 |
fun outfile md module suffix text = |
| 291 |
if !errorCount > 0 then () else |
| 292 |
let val file = pathName md module suffix |
| 293 |
(* val file = moduleName(module^".sml") *) (* For testing *) |
| 294 |
val oldText = |
| 295 |
let val stream = TextIO.openIn file |
| 296 |
in TextIO.inputN(stream,1024*1024) before TextIO.closeIn stream |
| 297 |
end handle _ => "" |
| 298 |
val header = |
| 299 |
"(*\n"^ |
| 300 |
" * WARNING: This file was automatically generated by MDLGen (v3.1)\n"^ |
| 301 |
" * from the machine description file \""^(filename md)^"\".\n"^ |
| 302 |
" * DO NOT EDIT this file directly\n"^ |
| 303 |
" *)\n"^ |
| 304 |
"\n\n" |
| 305 |
val newText = header^text |
| 306 |
in if !errorCount = 0 then |
| 307 |
(print(" Generating module "^file^" ... "); |
| 308 |
if oldText <> newText then |
| 309 |
let val dir = OS.Path.dir file |
| 310 |
val _ = OS.FileSys.mkDir dir handle _ => () |
| 311 |
val stream = TextIO.openOut file |
| 312 |
in |
| 313 |
TextIO.output(stream,newText); |
| 314 |
TextIO.closeOut stream; |
| 315 |
print("done\n") |
| 316 |
end |
| 317 |
else print("file is unchanged\n") |
| 318 |
) |
| 319 |
else () |
| 320 |
end |
| 321 |
|
| 322 |
(* Emit code into a file *) |
| 323 |
fun codegen md module code = |
| 324 |
let val newText = PP.text(PP.setmode "code" ++ PP.concat code) |
| 325 |
in outfile md module ".sml" newText |
| 326 |
end |
| 327 |
|
| 328 |
fun errorHandler md suffix = ERRORfun(strname md suffix) |
| 329 |
|
| 330 |
(* Emit a function that dispatches to subfunctions according to the |
| 331 |
* cell kind |
| 332 |
*) |
| 333 |
fun mkQueryByCellKind md name = |
| 334 |
let val cellKinds = cells md |
| 335 |
val clientDefined = |
| 336 |
List.filter (fn CELLdecl{id, alias, ...} => |
| 337 |
not(isSome alias) andalso |
| 338 |
not(MLRiscDefs.isPredefinedCellKind id) andalso |
| 339 |
not(MLRiscDefs.isPseudoCellKind id)) cellKinds |
| 340 |
|
| 341 |
val newlyDefined = |
| 342 |
case clientDefined of |
| 343 |
[] => [CLAUSE([WILDpat],NONE,APP("error",STRINGexp name))] |
| 344 |
| _ => |
| 345 |
[CLAUSE([IDpat "k"],NONE, |
| 346 |
foldr(fn (CELLdecl{id, alias, ...}, e) => |
| 347 |
IFexp(APP("=",TUPLEexp[ID "k",IDexp(IDENT(["C"],id))]), |
| 348 |
ID(name^id), |
| 349 |
e)) |
| 350 |
(APP("error",STRINGexp name)) clientDefined) |
| 351 |
] |
| 352 |
|
| 353 |
val predefined = |
| 354 |
foldr (fn (CELLdecl{id, alias, ...}, c) => |
| 355 |
if MLRiscDefs.isPredefinedCellKind id andalso |
| 356 |
not(MLRiscDefs.isPseudoCellKind id) |
| 357 |
then |
| 358 |
CLAUSE([CONSpat(IDENT(["C"],id),NONE)],NONE, |
| 359 |
case alias of |
| 360 |
NONE => ID(name^id) |
| 361 |
| SOME alias => APP(name,IDexp(IDENT(["C"],alias))) |
| 362 |
)::c |
| 363 |
else c |
| 364 |
) newlyDefined cellKinds |
| 365 |
|
| 366 |
in FUNdecl[FUNbind(name, predefined)] |
| 367 |
end |
| 368 |
|
| 369 |
(* |
| 370 |
* Do everything on user defined cellkinds |
| 371 |
*) |
| 372 |
fun forallUserCellKinds md f = |
| 373 |
map f (List.filter (fn CELLdecl{id, alias, ...} => |
| 374 |
not(MLRiscDefs.isPseudoCellKind id) |
| 375 |
andalso not(isSome alias) |
| 376 |
) (cells md)) |
| 377 |
|
| 378 |
|
| 379 |
end |