45f411b942867adc99196a73363a689f97f026c2
[ghc-hetmet.git] / ghc / compiler / cmm / CmmParse.y
1 -----------------------------------------------------------------------------
2 --
3 -- (c) The University of Glasgow, 2004
4 --
5 -- Parser for concrete Cmm.
6 --
7 -----------------------------------------------------------------------------
8
9 {
10 module CmmParse ( parseCmmFile ) where
11
12 import CgMonad
13 import CgHeapery
14 import CgUtils
15 import CgProf
16 import CgTicky
17 import CgInfoTbls
18 import CgForeignCall
19 import CgTailCall       ( pushUnboxedTuple )
20 import CgStackery       ( emitPushUpdateFrame )
21 import ClosureInfo      ( C_SRT(..) )
22 import CgCallConv       ( smallLiveness )
23 import CgClosure        ( emitBlackHoleCode )
24 import CostCentre       ( dontCareCCS )
25
26 import Cmm
27 import PprCmm
28 import CmmUtils         ( mkIntCLit )
29 import CmmLex
30 import CLabel
31 import MachOp
32 import SMRep            ( fixedHdrSize, CgRep(..) )
33 import Lexer
34
35 import ForeignCall      ( CCallConv(..) )
36 import Literal          ( mkMachInt )
37 import Unique
38 import UniqFM
39 import SrcLoc
40 import DynFlags         ( DynFlags, DynFlag(..) )
41 import Packages         ( HomeModules )
42 import StaticFlags      ( opt_SccProfilingOn )
43 import ErrUtils         ( printError, dumpIfSet_dyn, showPass )
44 import StringBuffer     ( hGetStringBuffer )
45 import FastString
46 import Panic            ( panic )
47 import Constants        ( wORD_SIZE )
48 import Outputable
49
50 import Monad            ( when )
51
52 #include "HsVersions.h"
53 }
54
55 %token
56         ':'     { L _ (CmmT_SpecChar ':') }
57         ';'     { L _ (CmmT_SpecChar ';') }
58         '{'     { L _ (CmmT_SpecChar '{') }
59         '}'     { L _ (CmmT_SpecChar '}') }
60         '['     { L _ (CmmT_SpecChar '[') }
61         ']'     { L _ (CmmT_SpecChar ']') }
62         '('     { L _ (CmmT_SpecChar '(') }
63         ')'     { L _ (CmmT_SpecChar ')') }
64         '='     { L _ (CmmT_SpecChar '=') }
65         '`'     { L _ (CmmT_SpecChar '`') }
66         '~'     { L _ (CmmT_SpecChar '~') }
67         '/'     { L _ (CmmT_SpecChar '/') }
68         '*'     { L _ (CmmT_SpecChar '*') }
69         '%'     { L _ (CmmT_SpecChar '%') }
70         '-'     { L _ (CmmT_SpecChar '-') }
71         '+'     { L _ (CmmT_SpecChar '+') }
72         '&'     { L _ (CmmT_SpecChar '&') }
73         '^'     { L _ (CmmT_SpecChar '^') }
74         '|'     { L _ (CmmT_SpecChar '|') }
75         '>'     { L _ (CmmT_SpecChar '>') }
76         '<'     { L _ (CmmT_SpecChar '<') }
77         ','     { L _ (CmmT_SpecChar ',') }
78         '!'     { L _ (CmmT_SpecChar '!') }
79
80         '..'    { L _ (CmmT_DotDot) }
81         '::'    { L _ (CmmT_DoubleColon) }
82         '>>'    { L _ (CmmT_Shr) }
83         '<<'    { L _ (CmmT_Shl) }
84         '>='    { L _ (CmmT_Ge) }
85         '<='    { L _ (CmmT_Le) }
86         '=='    { L _ (CmmT_Eq) }
87         '!='    { L _ (CmmT_Ne) }
88         '&&'    { L _ (CmmT_BoolAnd) }
89         '||'    { L _ (CmmT_BoolOr) }
90
91         'CLOSURE'       { L _ (CmmT_CLOSURE) }
92         'INFO_TABLE'    { L _ (CmmT_INFO_TABLE) }
93         'INFO_TABLE_RET'{ L _ (CmmT_INFO_TABLE_RET) }
94         'INFO_TABLE_FUN'{ L _ (CmmT_INFO_TABLE_FUN) }
95         'INFO_TABLE_CONSTR'{ L _ (CmmT_INFO_TABLE_CONSTR) }
96         'INFO_TABLE_SELECTOR'{ L _ (CmmT_INFO_TABLE_SELECTOR) }
97         'else'          { L _ (CmmT_else) }
98         'export'        { L _ (CmmT_export) }
99         'section'       { L _ (CmmT_section) }
100         'align'         { L _ (CmmT_align) }
101         'goto'          { L _ (CmmT_goto) }
102         'if'            { L _ (CmmT_if) }
103         'jump'          { L _ (CmmT_jump) }
104         'foreign'       { L _ (CmmT_foreign) }
105         'import'        { L _ (CmmT_import) }
106         'switch'        { L _ (CmmT_switch) }
107         'case'          { L _ (CmmT_case) }
108         'default'       { L _ (CmmT_default) }
109         'bits8'         { L _ (CmmT_bits8) }
110         'bits16'        { L _ (CmmT_bits16) }
111         'bits32'        { L _ (CmmT_bits32) }
112         'bits64'        { L _ (CmmT_bits64) }
113         'float32'       { L _ (CmmT_float32) }
114         'float64'       { L _ (CmmT_float64) }
115
116         GLOBALREG       { L _ (CmmT_GlobalReg   $$) }
117         NAME            { L _ (CmmT_Name        $$) }
118         STRING          { L _ (CmmT_String      $$) }
119         INT             { L _ (CmmT_Int         $$) }
120         FLOAT           { L _ (CmmT_Float       $$) }
121
122 %monad { P } { >>= } { return }
123 %lexer { cmmlex } { L _ CmmT_EOF }
124 %name cmmParse cmm
125 %tokentype { Located CmmToken }
126
127 -- C-- operator precedences, taken from the C-- spec
128 %right '||'     -- non-std extension, called %disjoin in C--
129 %right '&&'     -- non-std extension, called %conjoin in C--
130 %right '!'
131 %nonassoc '>=' '>' '<=' '<' '!=' '=='
132 %left '|'
133 %left '^'
134 %left '&'
135 %left '>>' '<<'
136 %left '-' '+'
137 %left '/' '*' '%'
138 %right '~'
139
140 %%
141
142 cmm     :: { ExtCode }
143         : {- empty -}                   { return () }
144         | cmmtop cmm                    { do $1; $2 }
145
146 cmmtop  :: { ExtCode }
147         : cmmproc                       { $1 }
148         | cmmdata                       { $1 }
149         | decl                          { $1 } 
150         | 'CLOSURE' '(' NAME ',' NAME lits ')' ';'  
151                 { do lits <- sequence $6;
152                      staticClosure $3 $5 (map getLit lits) }
153
154 -- The only static closures in the RTS are dummy closures like
155 -- stg_END_TSO_QUEUE_closure and stg_dummy_ret.  We don't need
156 -- to provide the full generality of static closures here.
157 -- In particular:
158 --      * CCS can always be CCS_DONT_CARE
159 --      * closure is always extern
160 --      * payload is always empty
161 --      * we can derive closure and info table labels from a single NAME
162
163 cmmdata :: { ExtCode }
164         : 'section' STRING '{' statics '}' 
165                 { do ss <- sequence $4;
166                      code (emitData (section $2) (concat ss)) }
167
168 statics :: { [ExtFCode [CmmStatic]] }
169         : {- empty -}                   { [] }
170         | static statics                { $1 : $2 }
171
172 -- Strings aren't used much in the RTS HC code, so it doesn't seem
173 -- worth allowing inline strings.  C-- doesn't allow them anyway.
174 static  :: { ExtFCode [CmmStatic] }
175         : NAME ':'      { return [CmmDataLabel (mkRtsDataLabelFS $1)] }
176         | type expr ';' { do e <- $2;
177                              return [CmmStaticLit (getLit e)] }
178         | type ';'                      { return [CmmUninitialised
179                                                         (machRepByteWidth $1)] }
180         | 'bits8' '[' ']' STRING ';'    { return [CmmString $4] }
181         | 'bits8' '[' INT ']' ';'       { return [CmmUninitialised 
182                                                         (fromIntegral $3)] }
183         | typenot8 '[' INT ']' ';'      { return [CmmUninitialised 
184                                                 (machRepByteWidth $1 * 
185                                                         fromIntegral $3)] }
186         | 'align' INT ';'               { return [CmmAlign (fromIntegral $2)] }
187         | 'CLOSURE' '(' NAME lits ')'
188                 { do lits <- sequence $4;
189                      return $ map CmmStaticLit $
190                        mkStaticClosure (mkRtsInfoLabelFS $3) 
191                          dontCareCCS (map getLit lits) [] [] [] }
192         -- arrays of closures required for the CHARLIKE & INTLIKE arrays
193
194 lits    :: { [ExtFCode CmmExpr] }
195         : {- empty -}           { [] }
196         | ',' expr lits         { $2 : $3 }
197
198 cmmproc :: { ExtCode }
199         : info '{' body '}'
200                 { do  (info_lbl, info1, info2) <- $1;
201                       stmts <- getCgStmtsEC (loopDecls $3)
202                       blks <- code (cgStmtsToBlocks stmts)
203                       code (emitInfoTableAndCode info_lbl info1 info2 [] blks) }
204
205         | info ';'
206                 { do (info_lbl, info1, info2) <- $1;
207                      code (emitInfoTableAndCode info_lbl info1 info2 [] []) }
208
209         | NAME '{' body '}'
210                 { do stmts <- getCgStmtsEC (loopDecls $3);
211                      blks <- code (cgStmtsToBlocks stmts)
212                      code (emitProc [] (mkRtsCodeLabelFS $1) [] blks) }
213
214 info    :: { ExtFCode (CLabel, [CmmLit],[CmmLit]) }
215         : 'INFO_TABLE' '(' NAME ',' INT ',' INT ',' INT ',' STRING ',' STRING ')'
216                 -- ptrs, nptrs, closure type, description, type
217                 { stdInfo $3 $5 $7 0 $9 $11 $13 }
218         
219         | 'INFO_TABLE_FUN' '(' NAME ',' INT ',' INT ',' INT ',' STRING ',' STRING ',' INT ')'
220                 -- ptrs, nptrs, closure type, description, type, fun type
221                 { funInfo $3 $5 $7 $9 $11 $13 $15 }
222         
223         | 'INFO_TABLE_CONSTR' '(' NAME ',' INT ',' INT ',' INT ',' INT ',' STRING ',' STRING ')'
224                 -- ptrs, nptrs, tag, closure type, description, type
225                 { stdInfo $3 $5 $7 $9 $11 $13 $15 }
226         
227         | 'INFO_TABLE_SELECTOR' '(' NAME ',' INT ',' INT ',' STRING ',' STRING ')'
228                 -- selector, closure type, description, type
229                 { basicInfo $3 (mkIntCLit (fromIntegral $5)) 0 $7 $9 $11 }
230
231         | 'INFO_TABLE_RET' '(' NAME ',' INT ',' INT ',' INT maybe_vec ')'
232                 { retInfo $3 $5 $7 $9 $10 }
233
234 maybe_vec :: { [CmmLit] }
235         : {- empty -}                   { [] }
236         | ',' NAME maybe_vec            { CmmLabel (mkRtsCodeLabelFS $2) : $3 }
237
238 body    :: { ExtCode }
239         : {- empty -}                   { return () }
240         | decl body                     { do $1; $2 }
241         | stmt body                     { do $1; $2 }
242
243 decl    :: { ExtCode }
244         : type names ';'                { mapM_ (newLocal $1) $2 }
245         | 'import' names ';'            { return () }  -- ignore imports
246         | 'export' names ';'            { return () }  -- ignore exports
247
248 names   :: { [FastString] }
249         : NAME                  { [$1] }
250         | NAME ',' names        { $1 : $3 }
251
252 stmt    :: { ExtCode }
253         : ';'                                   { nopEC }
254
255         | block_id ':'                          { code (labelC $1) }
256
257         | lreg '=' expr ';'                     
258                 { do reg <- $1; e <- $3; stmtEC (CmmAssign reg e) }
259         | type '[' expr ']' '=' expr ';'
260                 { doStore $1 $3 $6 }
261         | 'foreign' STRING expr '(' hint_exprs0 ')' vols ';'
262                 {% foreignCall $2 [] $3 $5 $7 }
263         | lreg '=' 'foreign' STRING expr '(' hint_exprs0 ')' vols ';'
264                 {% let result = do r <- $1; return (r,NoHint) in
265                    foreignCall $4 [result] $5 $7 $9 }
266         | STRING lreg '=' 'foreign' STRING expr '(' hint_exprs0 ')' vols ';'
267                 {% do h <- parseHint $1;
268                       let result = do r <- $2; return (r,h) in
269                       foreignCall $5 [result] $6 $8 $10 }
270         -- stmt-level macros, stealing syntax from ordinary C-- function calls.
271         -- Perhaps we ought to use the %%-form?
272         | NAME '(' exprs0 ')' ';'
273                 {% stmtMacro $1 $3  }
274         | 'switch' maybe_range expr '{' arms default '}'
275                 { doSwitch $2 $3 $5 $6 }
276         | 'goto' block_id ';'
277                 { stmtEC (CmmBranch $2) }
278         | 'jump' expr {-maybe_actuals-} ';'
279                 { do e <- $2; stmtEC (CmmJump e []) }
280         | 'if' bool_expr '{' body '}' else      
281                 { ifThenElse $2 $4 $6 }
282
283 bool_expr :: { ExtFCode BoolExpr }
284         : bool_op                       { $1 }
285         | expr                          { do e <- $1; return (BoolTest e) }
286
287 bool_op :: { ExtFCode BoolExpr }
288         : bool_expr '&&' bool_expr      { do e1 <- $1; e2 <- $3; 
289                                           return (BoolAnd e1 e2) }
290         | bool_expr '||' bool_expr      { do e1 <- $1; e2 <- $3; 
291                                           return (BoolOr e1 e2)  }
292         | '!' bool_expr                 { do e <- $2; return (BoolNot e) }
293         | '(' bool_op ')'               { $2 }
294
295 -- This is not C-- syntax.  What to do?
296 vols    :: { Maybe [GlobalReg] }
297         : {- empty -}                   { Nothing }
298         | '[' ']'                       { Just [] }
299         | '[' globals ']'               { Just $2 }
300
301 globals :: { [GlobalReg] }
302         : GLOBALREG                     { [$1] }
303         | GLOBALREG ',' globals         { $1 : $3 }
304
305 maybe_range :: { Maybe (Int,Int) }
306         : '[' INT '..' INT ']'  { Just (fromIntegral $2, fromIntegral $4) }
307         | {- empty -}           { Nothing }
308
309 arms    :: { [([Int],ExtCode)] }
310         : {- empty -}                   { [] }
311         | arm arms                      { $1 : $2 }
312
313 arm     :: { ([Int],ExtCode) }
314         : 'case' ints ':' '{' body '}'  { ($2, $5) }
315
316 ints    :: { [Int] }
317         : INT                           { [ fromIntegral $1 ] }
318         | INT ',' ints                  { fromIntegral $1 : $3 }
319
320 default :: { Maybe ExtCode }
321         : 'default' ':' '{' body '}'    { Just $4 }
322         -- taking a few liberties with the C-- syntax here; C-- doesn't have
323         -- 'default' branches
324         | {- empty -}                   { Nothing }
325
326 else    :: { ExtCode }
327         : {- empty -}                   { nopEC }
328         | 'else' '{' body '}'           { $3 }
329
330 -- we have to write this out longhand so that Happy's precedence rules
331 -- can kick in.
332 expr    :: { ExtFCode CmmExpr } 
333         : expr '/' expr                 { mkMachOp MO_U_Quot [$1,$3] }
334         | expr '*' expr                 { mkMachOp MO_Mul [$1,$3] }
335         | expr '%' expr                 { mkMachOp MO_U_Rem [$1,$3] }
336         | expr '-' expr                 { mkMachOp MO_Sub [$1,$3] }
337         | expr '+' expr                 { mkMachOp MO_Add [$1,$3] }
338         | expr '>>' expr                { mkMachOp MO_U_Shr [$1,$3] }
339         | expr '<<' expr                { mkMachOp MO_Shl [$1,$3] }
340         | expr '&' expr                 { mkMachOp MO_And [$1,$3] }
341         | expr '^' expr                 { mkMachOp MO_Xor [$1,$3] }
342         | expr '|' expr                 { mkMachOp MO_Or [$1,$3] }
343         | expr '>=' expr                { mkMachOp MO_U_Ge [$1,$3] }
344         | expr '>' expr                 { mkMachOp MO_U_Gt [$1,$3] }
345         | expr '<=' expr                { mkMachOp MO_U_Le [$1,$3] }
346         | expr '<' expr                 { mkMachOp MO_U_Lt [$1,$3] }
347         | expr '!=' expr                { mkMachOp MO_Ne [$1,$3] }
348         | expr '==' expr                { mkMachOp MO_Eq [$1,$3] }
349         | '~' expr                      { mkMachOp MO_Not [$2] }
350         | '-' expr                      { mkMachOp MO_S_Neg [$2] }
351         | expr0 '`' NAME '`' expr0      {% do { mo <- nameToMachOp $3 ;
352                                                 return (mkMachOp mo [$1,$5]) } }
353         | expr0                         { $1 }
354
355 expr0   :: { ExtFCode CmmExpr }
356         : INT   maybe_ty         { return (CmmLit (CmmInt $1 $2)) }
357         | FLOAT maybe_ty         { return (CmmLit (CmmFloat $1 $2)) }
358         | STRING                 { do s <- code (mkStringCLit $1); 
359                                       return (CmmLit s) }
360         | reg                    { $1 }
361         | type '[' expr ']'      { do e <- $3; return (CmmLoad e $1) }
362         | '%' NAME '(' exprs0 ')' {% exprOp $2 $4 }
363         | '(' expr ')'           { $2 }
364
365
366 -- leaving out the type of a literal gives you the native word size in C--
367 maybe_ty :: { MachRep }
368         : {- empty -}                   { wordRep }
369         | '::' type                     { $2 }
370
371 hint_exprs0 :: { [ExtFCode (CmmExpr, MachHint)] }
372         : {- empty -}                   { [] }
373         | hint_exprs                    { $1 }
374
375 hint_exprs :: { [ExtFCode (CmmExpr, MachHint)] }
376         : hint_expr                     { [$1] }
377         | hint_expr ',' hint_exprs      { $1 : $3 }
378
379 hint_expr :: { ExtFCode (CmmExpr, MachHint) }
380         : expr                          { do e <- $1; return (e, inferHint e) }
381         | expr STRING                   {% do h <- parseHint $2;
382                                               return $ do
383                                                 e <- $1; return (e,h) }
384
385 exprs0  :: { [ExtFCode CmmExpr] }
386         : {- empty -}                   { [] }
387         | exprs                         { $1 }
388
389 exprs   :: { [ExtFCode CmmExpr] }
390         : expr                          { [ $1 ] }
391         | expr ',' exprs                { $1 : $3 }
392
393 reg     :: { ExtFCode CmmExpr }
394         : NAME                  { lookupName $1 }
395         | GLOBALREG             { return (CmmReg (CmmGlobal $1)) }
396
397 lreg    :: { ExtFCode CmmReg }
398         : NAME                  { do e <- lookupName $1;
399                                      return $
400                                        case e of 
401                                         CmmReg r -> r
402                                         other -> pprPanic "CmmParse:" (ftext $1 <> text " not a register") }
403         | GLOBALREG             { return (CmmGlobal $1) }
404
405 block_id :: { BlockId }
406         : NAME                  { BlockId (newTagUnique (getUnique $1) 'L') }
407                         -- TODO: ugh.  The unique of a FastString has a null
408                         -- tag, so we have to put our own tag on.  We should
409                         -- really make a new unique for every label, and keep
410                         -- them in an environment.
411
412 type    :: { MachRep }
413         : 'bits8'               { I8 }
414         | typenot8              { $1 }
415
416 typenot8 :: { MachRep }
417         : 'bits16'              { I16 }
418         | 'bits32'              { I32 }
419         | 'bits64'              { I64 }
420         | 'float32'             { F32 }
421         | 'float64'             { F64 }
422 {
423 section :: String -> Section
424 section "text"   = Text
425 section "data"   = Data
426 section "rodata" = ReadOnlyData
427 section "bss"    = UninitialisedData
428 section s        = OtherSection s
429
430 -- mkMachOp infers the type of the MachOp from the type of its first
431 -- argument.  We assume that this is correct: for MachOps that don't have
432 -- symmetrical args (e.g. shift ops), the first arg determines the type of
433 -- the op.
434 mkMachOp :: (MachRep -> MachOp) -> [ExtFCode CmmExpr] -> ExtFCode CmmExpr
435 mkMachOp fn args = do
436   arg_exprs <- sequence args
437   return (CmmMachOp (fn (cmmExprRep (head arg_exprs))) arg_exprs)
438
439 getLit :: CmmExpr -> CmmLit
440 getLit (CmmLit l) = l
441 getLit (CmmMachOp (MO_S_Neg _) [CmmLit (CmmInt i r)])  = CmmInt (negate i) r
442 getLit _ = panic "invalid literal" -- TODO messy failure
443
444 nameToMachOp :: FastString -> P (MachRep -> MachOp)
445 nameToMachOp name = 
446   case lookupUFM machOps name of
447         Nothing -> fail ("unknown primitive " ++ unpackFS name)
448         Just m  -> return m
449
450 exprOp :: FastString -> [ExtFCode CmmExpr] -> P (ExtFCode CmmExpr)
451 exprOp name args_code =
452   case lookupUFM exprMacros name of
453      Just f  -> return $ do
454         args <- sequence args_code
455         return (f args)
456      Nothing -> do
457         mo <- nameToMachOp name
458         return $ mkMachOp mo args_code
459
460 exprMacros :: UniqFM ([CmmExpr] -> CmmExpr)
461 exprMacros = listToUFM [
462   ( FSLIT("ENTRY_CODE"),   \ [x] -> entryCode x ),
463   ( FSLIT("GET_ENTRY"),    \ [x] -> entryCode (closureInfoPtr x) ),
464   ( FSLIT("STD_INFO"),     \ [x] -> infoTable x ),
465   ( FSLIT("GET_STD_INFO"), \ [x] -> infoTable (closureInfoPtr x) ),
466   ( FSLIT("GET_FUN_INFO"), \ [x] -> funInfoTable (closureInfoPtr x) ),
467   ( FSLIT("INFO_TYPE"),    \ [x] -> infoTableClosureType x ),
468   ( FSLIT("INFO_PTRS"),    \ [x] -> infoTablePtrs x ),
469   ( FSLIT("INFO_NPTRS"),   \ [x] -> infoTableNonPtrs x ),
470   ( FSLIT("RET_VEC"),      \ [info, conZ] -> retVec info conZ )
471   ]
472
473 -- we understand a subset of C-- primitives:
474 machOps = listToUFM $
475         map (\(x, y) -> (mkFastString x, y)) [
476         ( "add",        MO_Add ),
477         ( "sub",        MO_Sub ),
478         ( "eq",         MO_Eq ),
479         ( "ne",         MO_Ne ),
480         ( "mul",        MO_Mul ),
481         ( "neg",        MO_S_Neg ),
482         ( "quot",       MO_S_Quot ),
483         ( "rem",        MO_S_Rem ),
484         ( "divu",       MO_U_Quot ),
485         ( "modu",       MO_U_Rem ),
486
487         ( "ge",         MO_S_Ge ),
488         ( "le",         MO_S_Le ),
489         ( "gt",         MO_S_Gt ),
490         ( "lt",         MO_S_Lt ),
491
492         ( "geu",        MO_U_Ge ),
493         ( "leu",        MO_U_Le ),
494         ( "gtu",        MO_U_Gt ),
495         ( "ltu",        MO_U_Lt ),
496
497         ( "flt",        MO_S_Lt ),
498         ( "fle",        MO_S_Le ),
499         ( "feq",        MO_Eq ),
500         ( "fne",        MO_Ne ),
501         ( "fgt",        MO_S_Gt ),
502         ( "fge",        MO_S_Ge ),
503         ( "fneg",       MO_S_Neg ),
504
505         ( "and",        MO_And ),
506         ( "or",         MO_Or ),
507         ( "xor",        MO_Xor ),
508         ( "com",        MO_Not ),
509         ( "shl",        MO_Shl ),
510         ( "shrl",       MO_U_Shr ),
511         ( "shra",       MO_S_Shr ),
512
513         ( "lobits8",  flip MO_U_Conv I8  ),
514         ( "lobits16", flip MO_U_Conv I16 ),
515         ( "lobits32", flip MO_U_Conv I32 ),
516         ( "lobits64", flip MO_U_Conv I64 ),
517         ( "sx16",     flip MO_S_Conv I16 ),
518         ( "sx32",     flip MO_S_Conv I32 ),
519         ( "sx64",     flip MO_S_Conv I64 ),
520         ( "zx16",     flip MO_U_Conv I16 ),
521         ( "zx32",     flip MO_U_Conv I32 ),
522         ( "zx64",     flip MO_U_Conv I64 ),
523         ( "f2f32",    flip MO_S_Conv F32 ),  -- TODO; rounding mode
524         ( "f2f64",    flip MO_S_Conv F64 ),  -- TODO; rounding mode
525         ( "f2i8",     flip MO_S_Conv I8 ),
526         ( "f2i16",    flip MO_S_Conv I8 ),
527         ( "f2i32",    flip MO_S_Conv I8 ),
528         ( "f2i64",    flip MO_S_Conv I8 ),
529         ( "i2f32",    flip MO_S_Conv F32 ),
530         ( "i2f64",    flip MO_S_Conv F64 )
531         ]
532
533 parseHint :: String -> P MachHint
534 parseHint "ptr"    = return PtrHint
535 parseHint "signed" = return SignedHint
536 parseHint "float"  = return FloatHint
537 parseHint str      = fail ("unrecognised hint: " ++ str)
538
539 -- labels are always pointers, so we might as well infer the hint
540 inferHint :: CmmExpr -> MachHint
541 inferHint (CmmLit (CmmLabel _)) = PtrHint
542 inferHint (CmmReg (CmmGlobal g)) | isPtrGlobalReg g = PtrHint
543 inferHint _ = NoHint
544
545 isPtrGlobalReg Sp               = True
546 isPtrGlobalReg SpLim            = True
547 isPtrGlobalReg Hp               = True
548 isPtrGlobalReg HpLim            = True
549 isPtrGlobalReg CurrentTSO       = True
550 isPtrGlobalReg CurrentNursery   = True
551 isPtrGlobalReg _                = False
552
553 happyError :: P a
554 happyError = srcParseFail
555
556 -- -----------------------------------------------------------------------------
557 -- Statement-level macros
558
559 stmtMacro :: FastString -> [ExtFCode CmmExpr] -> P ExtCode
560 stmtMacro fun args_code = do
561   case lookupUFM stmtMacros fun of
562     Nothing -> fail ("unknown macro: " ++ unpackFS fun)
563     Just fcode -> return $ do
564         args <- sequence args_code
565         code (fcode args)
566
567 stmtMacros :: UniqFM ([CmmExpr] -> Code)
568 stmtMacros = listToUFM [
569   ( FSLIT("CCS_ALLOC"),            \[words,ccs]  -> profAlloc words ccs ),
570   ( FSLIT("CLOSE_NURSERY"),        \[]  -> emitCloseNursery ),
571   ( FSLIT("ENTER_CCS_PAP_CL"),     \[e] -> enterCostCentrePAP e ),
572   ( FSLIT("ENTER_CCS_THUNK"),      \[e] -> enterCostCentreThunk e ),
573   ( FSLIT("HP_CHK_GEN"),           \[words,liveness,reentry] -> 
574                                       hpChkGen words liveness reentry ),
575   ( FSLIT("HP_CHK_NP_ASSIGN_SP0"), \[e,f] -> hpChkNodePointsAssignSp0 e f ),
576   ( FSLIT("LOAD_THREAD_STATE"),    \[] -> emitLoadThreadState ),
577   ( FSLIT("LDV_ENTER"),            \[e] -> ldvEnter e ),
578   ( FSLIT("LDV_RECORD_CREATE"),    \[e] -> ldvRecordCreate e ),
579   ( FSLIT("OPEN_NURSERY"),         \[]  -> emitOpenNursery ),
580   ( FSLIT("PUSH_UPD_FRAME"),       \[sp,e] -> emitPushUpdateFrame sp e ),
581   ( FSLIT("SAVE_THREAD_STATE"),    \[] -> emitSaveThreadState ),
582   ( FSLIT("SET_HDR"),              \[ptr,info,ccs] -> 
583                                         emitSetDynHdr ptr info ccs ),
584   ( FSLIT("STK_CHK_GEN"),          \[words,liveness,reentry] -> 
585                                       stkChkGen words liveness reentry ),
586   ( FSLIT("STK_CHK_NP"),           \[e] -> stkChkNodePoints e ),
587   ( FSLIT("TICK_ALLOC_PRIM"),      \[hdr,goods,slop] -> 
588                                         tickyAllocPrim hdr goods slop ),
589   ( FSLIT("TICK_ALLOC_PAP"),       \[goods,slop] -> 
590                                         tickyAllocPAP goods slop ),
591   ( FSLIT("TICK_ALLOC_UP_THK"),    \[goods,slop] -> 
592                                         tickyAllocThunk goods slop ),
593   ( FSLIT("UPD_BH_UPDATABLE"),       \[] -> emitBlackHoleCode False ),
594   ( FSLIT("UPD_BH_SINGLE_ENTRY"),    \[] -> emitBlackHoleCode True ),
595
596   ( FSLIT("RET_P"),     \[a] ->       emitRetUT [(PtrArg,a)]),
597   ( FSLIT("RET_N"),     \[a] ->       emitRetUT [(NonPtrArg,a)]),
598   ( FSLIT("RET_PP"),    \[a,b] ->     emitRetUT [(PtrArg,a),(PtrArg,b)]),
599   ( FSLIT("RET_NN"),    \[a,b] ->     emitRetUT [(NonPtrArg,a),(NonPtrArg,b)]),
600   ( FSLIT("RET_NP"),    \[a,b] ->     emitRetUT [(NonPtrArg,a),(PtrArg,b)]),
601   ( FSLIT("RET_PPP"),   \[a,b,c] ->   emitRetUT [(PtrArg,a),(PtrArg,b),(PtrArg,c)]),
602   ( FSLIT("RET_NNP"),   \[a,b,c] ->   emitRetUT [(NonPtrArg,a),(NonPtrArg,b),(PtrArg,c)]),
603   ( FSLIT("RET_NNNP"),  \[a,b,c,d] -> emitRetUT [(NonPtrArg,a),(NonPtrArg,b),(NonPtrArg,c),(PtrArg,d)]),
604   ( FSLIT("RET_NPNP"),  \[a,b,c,d] -> emitRetUT [(NonPtrArg,a),(PtrArg,b),(NonPtrArg,c),(PtrArg,d)])
605
606  ]
607
608 -- -----------------------------------------------------------------------------
609 -- Our extended FCode monad.
610
611 -- We add a mapping from names to CmmExpr, to support local variable names in
612 -- the concrete C-- code.  The unique supply of the underlying FCode monad
613 -- is used to grab a new unique for each local variable.
614
615 -- In C--, a local variable can be declared anywhere within a proc,
616 -- and it scopes from the beginning of the proc to the end.  Hence, we have
617 -- to collect declarations as we parse the proc, and feed the environment
618 -- back in circularly (to avoid a two-pass algorithm).
619
620 type Decls = [(FastString,CmmExpr)]
621 type Env   = UniqFM CmmExpr
622
623 newtype ExtFCode a = EC { unEC :: Env -> Decls -> FCode (Decls, a) }
624
625 type ExtCode = ExtFCode ()
626
627 returnExtFC a = EC $ \e s -> return (s, a)
628 thenExtFC (EC m) k = EC $ \e s -> do (s',r) <- m e s; unEC (k r) e s'
629
630 instance Monad ExtFCode where
631   (>>=) = thenExtFC
632   return = returnExtFC
633
634 -- This function takes the variable decarations and imports and makes 
635 -- an environment, which is looped back into the computation.  In this
636 -- way, we can have embedded declarations that scope over the whole
637 -- procedure, and imports that scope over the entire module.
638 loopDecls :: ExtFCode a -> ExtFCode a
639 loopDecls (EC fcode) = 
640    EC $ \e s -> fixC (\ ~(decls,a) -> fcode (addListToUFM e decls) [])
641
642 getEnv :: ExtFCode Env
643 getEnv = EC $ \e s -> return (s, e)
644
645 addVarDecl :: FastString -> CmmExpr -> ExtCode
646 addVarDecl var expr = EC $ \e s -> return ((var,expr):s, ())
647
648 newLocal :: MachRep -> FastString -> ExtCode
649 newLocal ty name  = do
650    u <- code newUnique
651    addVarDecl name (CmmReg (CmmLocal (LocalReg u ty)))
652
653 -- Unknown names are treated as if they had been 'import'ed.
654 -- This saves us a lot of bother in the RTS sources, at the expense of
655 -- deferring some errors to link time.
656 lookupName :: FastString -> ExtFCode CmmExpr
657 lookupName name = do
658   env <- getEnv
659   return $ 
660      case lookupUFM env name of
661         Nothing -> CmmLit (CmmLabel (mkRtsCodeLabelFS name))
662         Just e  -> e
663
664 -- Lifting FCode computations into the ExtFCode monad:
665 code :: FCode a -> ExtFCode a
666 code fc = EC $ \e s -> do r <- fc; return (s, r)
667
668 code2 :: (FCode (Decls,b) -> FCode ((Decls,b),c))
669          -> ExtFCode b -> ExtFCode c
670 code2 f (EC ec) = EC $ \e s -> do ((s',b),c) <- f (ec e s); return (s',c)
671
672 nopEC = code nopC
673 stmtEC stmt = code (stmtC stmt)
674 stmtsEC stmts = code (stmtsC stmts)
675 getCgStmtsEC = code2 getCgStmts'
676
677 forkLabelledCodeEC ec = do
678   stmts <- getCgStmtsEC ec
679   code (forkCgStmts stmts)
680
681 retInfo name size live_bits cl_type vector = do
682   let liveness = smallLiveness (fromIntegral size) (fromIntegral live_bits)
683       info_lbl = mkRtsRetInfoLabelFS name
684       (info1,info2) = mkRetInfoTable info_lbl liveness NoC_SRT 
685                                 (fromIntegral cl_type) vector
686   return (info_lbl, info1, info2)
687
688 stdInfo name ptrs nptrs srt_bitmap cl_type desc_str ty_str =
689   basicInfo name (packHalfWordsCLit ptrs nptrs) 
690         srt_bitmap cl_type desc_str ty_str
691
692 basicInfo name layout srt_bitmap cl_type desc_str ty_str = do
693   lit1 <- if opt_SccProfilingOn 
694                    then code $ mkStringCLit desc_str
695                    else return (mkIntCLit 0)
696   lit2 <- if opt_SccProfilingOn 
697                    then code $ mkStringCLit ty_str
698                    else return (mkIntCLit 0)
699   let info1 = mkStdInfoTable lit1 lit2 (fromIntegral cl_type) 
700                         (fromIntegral srt_bitmap)
701                         layout
702   return (mkRtsInfoLabelFS name, info1, [])
703
704 funInfo name ptrs nptrs cl_type desc_str ty_str fun_type = do
705   (label,info1,_) <- stdInfo name ptrs nptrs 0{-srt_bitmap-}
706                          cl_type desc_str ty_str 
707   let info2 = mkFunGenInfoExtraBits (fromIntegral fun_type) 0 zero zero zero
708                 -- we leave most of the fields zero here.  This is only used
709                 -- to generate the BCO info table in the RTS at the moment.
710   return (label,info1,info2)
711  where
712    zero = mkIntCLit 0
713
714
715 staticClosure :: FastString -> FastString -> [CmmLit] -> ExtCode
716 staticClosure cl_label info payload
717   = code $ emitDataLits (mkRtsDataLabelFS cl_label) lits
718   where  lits = mkStaticClosure (mkRtsInfoLabelFS info) dontCareCCS payload [] [] []
719
720 foreignCall
721         :: String
722         -> [ExtFCode (CmmReg,MachHint)]
723         -> ExtFCode CmmExpr
724         -> [ExtFCode (CmmExpr,MachHint)]
725         -> Maybe [GlobalReg] -> P ExtCode
726 foreignCall "C" results_code expr_code args_code vols
727   = return $ do
728         results <- sequence results_code
729         expr <- expr_code
730         args <- sequence args_code
731         stmtEC (CmmCall (CmmForeignCall expr CCallConv) results args vols)
732 foreignCall conv _ _ _ _
733   = fail ("unknown calling convention: " ++ conv)
734
735 doStore :: MachRep -> ExtFCode CmmExpr  -> ExtFCode CmmExpr -> ExtCode
736 doStore rep addr_code val_code
737   = do addr <- addr_code
738        val <- val_code
739         -- if the specified store type does not match the type of the expr
740         -- on the rhs, then we insert a coercion that will cause the type
741         -- mismatch to be flagged by cmm-lint.  If we don't do this, then
742         -- the store will happen at the wrong type, and the error will not
743         -- be noticed.
744        let coerce_val 
745                 | cmmExprRep val /= rep = CmmMachOp (MO_U_Conv rep rep) [val]
746                 | otherwise             = val
747        stmtEC (CmmStore addr coerce_val)
748
749 -- Return an unboxed tuple.
750 emitRetUT :: [(CgRep,CmmExpr)] -> Code
751 emitRetUT args = do
752   tickyUnboxedTupleReturn (length args)  -- TICK
753   (sp, stmts) <- pushUnboxedTuple 0 args
754   emitStmts stmts
755   when (sp /= 0) $ stmtC (CmmAssign spReg (cmmRegOffW spReg (-sp)))
756   stmtC (CmmJump (entryCode (CmmLoad (cmmRegOffW spReg sp) wordRep)) [])
757
758 -- -----------------------------------------------------------------------------
759 -- If-then-else and boolean expressions
760
761 data BoolExpr
762   = BoolExpr `BoolAnd` BoolExpr
763   | BoolExpr `BoolOr`  BoolExpr
764   | BoolNot BoolExpr
765   | BoolTest CmmExpr
766
767 -- ToDo: smart constructors which simplify the boolean expression.
768
769 ifThenElse cond then_part else_part = do
770      then_id <- code newLabelC
771      join_id <- code newLabelC
772      c <- cond
773      emitCond c then_id
774      else_part
775      stmtEC (CmmBranch join_id)
776      code (labelC then_id)
777      then_part
778      -- fall through to join
779      code (labelC join_id)
780
781 -- 'emitCond cond true_id'  emits code to test whether the cond is true,
782 -- branching to true_id if so, and falling through otherwise.
783 emitCond (BoolTest e) then_id = do
784   stmtEC (CmmCondBranch e then_id)
785 emitCond (BoolNot (BoolTest (CmmMachOp op args))) then_id
786   | Just op' <- maybeInvertComparison op
787   = emitCond (BoolTest (CmmMachOp op' args)) then_id
788 emitCond (BoolNot e) then_id = do
789   else_id <- code newLabelC
790   emitCond e else_id
791   stmtEC (CmmBranch then_id)
792   code (labelC else_id)
793 emitCond (e1 `BoolOr` e2) then_id = do
794   emitCond e1 then_id
795   emitCond e2 then_id
796 emitCond (e1 `BoolAnd` e2) then_id = do
797         -- we'd like to invert one of the conditionals here to avoid an
798         -- extra branch instruction, but we can't use maybeInvertComparison
799         -- here because we can't look too closely at the expression since
800         -- we're in a loop.
801   and_id <- code newLabelC
802   else_id <- code newLabelC
803   emitCond e1 and_id
804   stmtEC (CmmBranch else_id)
805   code (labelC and_id)
806   emitCond e2 then_id
807   code (labelC else_id)
808
809
810 -- -----------------------------------------------------------------------------
811 -- Table jumps
812
813 -- We use a simplified form of C-- switch statements for now.  A
814 -- switch statement always compiles to a table jump.  Each arm can
815 -- specify a list of values (not ranges), and there can be a single
816 -- default branch.  The range of the table is given either by the
817 -- optional range on the switch (eg. switch [0..7] {...}), or by
818 -- the minimum/maximum values from the branches.
819
820 doSwitch :: Maybe (Int,Int) -> ExtFCode CmmExpr -> [([Int],ExtCode)]
821          -> Maybe ExtCode -> ExtCode
822 doSwitch mb_range scrut arms deflt
823    = do 
824         -- Compile code for the default branch
825         dflt_entry <- 
826                 case deflt of
827                   Nothing -> return Nothing
828                   Just e  -> do b <- forkLabelledCodeEC e; return (Just b)
829
830         -- Compile each case branch
831         table_entries <- mapM emitArm arms
832
833         -- Construct the table
834         let
835             all_entries = concat table_entries
836             ixs = map fst all_entries
837             (min,max) 
838                 | Just (l,u) <- mb_range = (l,u)
839                 | otherwise              = (minimum ixs, maximum ixs)
840
841             entries = elems (accumArray (\_ a -> Just a) dflt_entry (min,max)
842                                 all_entries)
843         expr <- scrut
844         -- ToDo: check for out of range and jump to default if necessary
845         stmtEC (CmmSwitch expr entries)
846    where
847         emitArm :: ([Int],ExtCode) -> ExtFCode [(Int,BlockId)]
848         emitArm (ints,code) = do
849            blockid <- forkLabelledCodeEC code
850            return [ (i,blockid) | i <- ints ]
851
852
853 -- -----------------------------------------------------------------------------
854 -- Putting it all together
855
856 -- The initial environment: we define some constants that the compiler
857 -- knows about here.
858 initEnv :: Env
859 initEnv = listToUFM [
860   ( FSLIT("SIZEOF_StgHeader"), 
861         CmmLit (CmmInt (fromIntegral (fixedHdrSize * wORD_SIZE)) wordRep) ),
862   ( FSLIT("SIZEOF_StgInfoTable"),
863         CmmLit (CmmInt (fromIntegral stdInfoTableSizeB) wordRep) )
864   ]
865
866 parseCmmFile :: DynFlags -> HomeModules -> FilePath -> IO (Maybe Cmm)
867 parseCmmFile dflags hmods filename = do
868   showPass dflags "ParseCmm"
869   buf <- hGetStringBuffer filename
870   let
871         init_loc = mkSrcLoc (mkFastString filename) 1 0
872         init_state = (mkPState buf init_loc dflags) { lex_state = [0] }
873                 -- reset the lex_state: the Lexer monad leaves some stuff
874                 -- in there we don't want.
875   case unP cmmParse init_state of
876     PFailed span err -> do printError span err; return Nothing
877     POk _ code -> do
878         cmm <- initC dflags hmods no_module (getCmm (unEC code initEnv [] >> return ()))
879         dumpIfSet_dyn dflags Opt_D_dump_cmm "Cmm" (pprCmms [cmm])
880         return (Just cmm)
881   where
882         no_module = panic "parseCmmFile: no module"
883 }