add -fsimpleopt-before-flatten
[ghc-hetmet.git] / compiler / cmm / CmmLex.x
1 -----------------------------------------------------------------------------
2 --
3 -- (c) The University of Glasgow, 2004-2006
4 --
5 -- Lexer for concrete Cmm.  We try to stay close to the C-- spec, but there
6 -- are a few minor differences:
7 --
8 --   * extra keywords for our macros, and float32/float64 types
9 --   * global registers (Sp,Hp, etc.)
10 --
11 -----------------------------------------------------------------------------
12
13 {
14 {-# LANGUAGE BangPatterns #-}
15 {-# OPTIONS -Wwarn -w #-}
16 -- The above -Wwarn supression flag is a temporary kludge.
17 -- While working on this module you are encouraged to remove it and fix
18 -- any warnings in the module. See
19 --     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
20 -- for details
21
22 module CmmLex (
23    CmmToken(..), cmmlex,
24   ) where
25
26 import OldCmm
27 import Lexer
28
29 import SrcLoc
30 import UniqFM
31 import StringBuffer
32 import FastString
33 import Ctype
34 import Util
35 --import TRACE
36 }
37
38 $whitechar   = [\ \t\n\r\f\v\xa0] -- \xa0 is Unicode no-break space
39 $white_no_nl = $whitechar # \n
40
41 $ascdigit  = 0-9
42 $unidigit  = \x01 -- Trick Alex into handling Unicode. See alexGetChar.
43 $digit     = [$ascdigit $unidigit]
44 $octit     = 0-7
45 $hexit     = [$digit A-F a-f]
46
47 $unilarge  = \x03 -- Trick Alex into handling Unicode. See alexGetChar.
48 $asclarge  = [A-Z \xc0-\xd6 \xd8-\xde]
49 $large     = [$asclarge $unilarge]
50
51 $unismall  = \x04 -- Trick Alex into handling Unicode. See alexGetChar.
52 $ascsmall  = [a-z \xdf-\xf6 \xf8-\xff]
53 $small     = [$ascsmall $unismall \_]
54
55 $namebegin = [$large $small \. \$ \@]
56 $namechar  = [$namebegin $digit]
57
58 @decimal     = $digit+
59 @octal       = $octit+
60 @hexadecimal = $hexit+
61 @exponent    = [eE] [\-\+]? @decimal
62
63 @floating_point = @decimal \. @decimal @exponent? | @decimal @exponent
64
65 @escape      = \\ ([abfnrt\\\'\"\?] | x $hexit{1,2} | $octit{1,3})
66 @strchar     = ($printable # [\"\\]) | @escape
67
68 cmm :-
69
70 $white_no_nl+           ;
71 ^\# pragma .* \n        ; -- Apple GCC 3.3 CPP generates pragmas in its output
72
73 ^\# (line)?             { begin line_prag }
74
75 -- single-line line pragmas, of the form
76 --    # <line> "<file>" <extra-stuff> \n
77 <line_prag> $digit+                     { setLine line_prag1 }
78 <line_prag1> \" [^\"]* \"       { setFile line_prag2 }
79 <line_prag2> .*                         { pop }
80
81 <0> {
82   \n                    ;
83
84   [\:\;\{\}\[\]\(\)\=\`\~\/\*\%\-\+\&\^\|\>\<\,\!]      { special_char }
85   
86   ".."                  { kw CmmT_DotDot }
87   "::"                  { kw CmmT_DoubleColon }
88   ">>"                  { kw CmmT_Shr }
89   "<<"                  { kw CmmT_Shl }
90   ">="                  { kw CmmT_Ge }
91   "<="                  { kw CmmT_Le }
92   "=="                  { kw CmmT_Eq }
93   "!="                  { kw CmmT_Ne }
94   "&&"                  { kw CmmT_BoolAnd }
95   "||"                  { kw CmmT_BoolOr }
96   
97   P@decimal             { global_regN (\n -> VanillaReg n VGcPtr) }
98   R@decimal             { global_regN (\n -> VanillaReg n VNonGcPtr) }
99   F@decimal             { global_regN FloatReg }
100   D@decimal             { global_regN DoubleReg }
101   L@decimal             { global_regN LongReg }
102   Sp                    { global_reg Sp }
103   SpLim                 { global_reg SpLim }
104   Hp                    { global_reg Hp }
105   HpLim                 { global_reg HpLim }
106   CurrentTSO            { global_reg CurrentTSO }
107   CurrentNursery        { global_reg CurrentNursery }
108   HpAlloc               { global_reg HpAlloc }
109   BaseReg               { global_reg BaseReg }
110   
111   $namebegin $namechar* { name }
112   
113   0 @octal              { tok_octal }
114   @decimal              { tok_decimal }
115   0[xX] @hexadecimal    { tok_hexadecimal }
116   @floating_point       { strtoken tok_float }
117   
118   \" @strchar* \"       { strtoken tok_string }
119 }
120
121 {
122 data CmmToken
123   = CmmT_SpecChar  Char
124   | CmmT_DotDot
125   | CmmT_DoubleColon
126   | CmmT_Shr
127   | CmmT_Shl
128   | CmmT_Ge
129   | CmmT_Le
130   | CmmT_Eq
131   | CmmT_Ne
132   | CmmT_BoolAnd
133   | CmmT_BoolOr
134   | CmmT_CLOSURE
135   | CmmT_INFO_TABLE
136   | CmmT_INFO_TABLE_RET
137   | CmmT_INFO_TABLE_FUN
138   | CmmT_INFO_TABLE_CONSTR
139   | CmmT_INFO_TABLE_SELECTOR
140   | CmmT_else
141   | CmmT_export
142   | CmmT_section
143   | CmmT_align
144   | CmmT_goto
145   | CmmT_if
146   | CmmT_jump
147   | CmmT_foreign
148   | CmmT_never
149   | CmmT_prim
150   | CmmT_return
151   | CmmT_returns
152   | CmmT_import
153   | CmmT_switch
154   | CmmT_case
155   | CmmT_default
156   | CmmT_bits8
157   | CmmT_bits16
158   | CmmT_bits32
159   | CmmT_bits64
160   | CmmT_float32
161   | CmmT_float64
162   | CmmT_gcptr
163   | CmmT_GlobalReg GlobalReg
164   | CmmT_Name      FastString
165   | CmmT_String    String
166   | CmmT_Int       Integer
167   | CmmT_Float     Rational
168   | CmmT_EOF
169 #ifdef DEBUG
170   deriving (Show)
171 #endif
172
173 -- -----------------------------------------------------------------------------
174 -- Lexer actions
175
176 type Action = SrcSpan -> StringBuffer -> Int -> P (Located CmmToken)
177
178 begin :: Int -> Action
179 begin code _span _str _len = do pushLexState code; lexToken
180
181 pop :: Action
182 pop _span _buf _len = do popLexState; lexToken
183
184 special_char :: Action
185 special_char span buf len = return (L span (CmmT_SpecChar (currentChar buf)))
186
187 kw :: CmmToken -> Action
188 kw tok span buf len = return (L span tok)
189
190 global_regN :: (Int -> GlobalReg) -> Action
191 global_regN con span buf len 
192   = return (L span (CmmT_GlobalReg (con (fromIntegral n))))
193   where buf' = stepOn buf
194         n = parseUnsignedInteger buf' (len-1) 10 octDecDigit
195
196 global_reg :: GlobalReg -> Action
197 global_reg r span buf len = return (L span (CmmT_GlobalReg r))
198
199 strtoken :: (String -> CmmToken) -> Action
200 strtoken f span buf len = 
201   return (L span $! (f $! lexemeToString buf len))
202
203 name :: Action
204 name span buf len = 
205   case lookupUFM reservedWordsFM fs of
206         Just tok -> return (L span tok)
207         Nothing  -> return (L span (CmmT_Name fs))
208   where
209         fs = lexemeToFastString buf len
210
211 reservedWordsFM = listToUFM $
212         map (\(x, y) -> (mkFastString x, y)) [
213         ( "CLOSURE",            CmmT_CLOSURE ),
214         ( "INFO_TABLE",         CmmT_INFO_TABLE ),
215         ( "INFO_TABLE_RET",     CmmT_INFO_TABLE_RET ),
216         ( "INFO_TABLE_FUN",     CmmT_INFO_TABLE_FUN ),
217         ( "INFO_TABLE_CONSTR",  CmmT_INFO_TABLE_CONSTR ),
218         ( "INFO_TABLE_SELECTOR",CmmT_INFO_TABLE_SELECTOR ),
219         ( "else",               CmmT_else ),
220         ( "export",             CmmT_export ),
221         ( "section",            CmmT_section ),
222         ( "align",              CmmT_align ),
223         ( "goto",               CmmT_goto ),
224         ( "if",                 CmmT_if ),
225         ( "jump",               CmmT_jump ),
226         ( "foreign",            CmmT_foreign ),
227         ( "never",              CmmT_never ),
228         ( "prim",               CmmT_prim ),
229         ( "return",             CmmT_return ),
230         ( "returns",            CmmT_returns ),
231         ( "import",             CmmT_import ),
232         ( "switch",             CmmT_switch ),
233         ( "case",               CmmT_case ),
234         ( "default",            CmmT_default ),
235         ( "bits8",              CmmT_bits8 ),
236         ( "bits16",             CmmT_bits16 ),
237         ( "bits32",             CmmT_bits32 ),
238         ( "bits64",             CmmT_bits64 ),
239         ( "float32",            CmmT_float32 ),
240         ( "float64",            CmmT_float64 ),
241 -- New forms
242         ( "b8",                 CmmT_bits8 ),
243         ( "b16",                CmmT_bits16 ),
244         ( "b32",                CmmT_bits32 ),
245         ( "b64",                CmmT_bits64 ),
246         ( "f32",                CmmT_float32 ),
247         ( "f64",                CmmT_float64 ),
248         ( "gcptr",              CmmT_gcptr )
249         ]
250
251 tok_decimal span buf len 
252   = return (L span (CmmT_Int  $! parseUnsignedInteger buf len 10 octDecDigit))
253
254 tok_octal span buf len 
255   = return (L span (CmmT_Int  $! parseUnsignedInteger (offsetBytes 1 buf) (len-1) 8 octDecDigit))
256
257 tok_hexadecimal span buf len 
258   = return (L span (CmmT_Int  $! parseUnsignedInteger (offsetBytes 2 buf) (len-2) 16 hexDigit))
259
260 tok_float str = CmmT_Float $! readRational str
261
262 tok_string str = CmmT_String (read str)
263                  -- urk, not quite right, but it'll do for now
264
265 -- -----------------------------------------------------------------------------
266 -- Line pragmas
267
268 setLine :: Int -> Action
269 setLine code span buf len = do
270   let line = parseUnsignedInteger buf len 10 octDecDigit
271   setSrcLoc (mkSrcLoc (srcSpanFile span) (fromIntegral line - 1) 1)
272         -- subtract one: the line number refers to the *following* line
273   -- trace ("setLine "  ++ show line) $ do
274   popLexState
275   pushLexState code
276   lexToken
277
278 setFile :: Int -> Action
279 setFile code span buf len = do
280   let file = lexemeToFastString (stepOn buf) (len-2)
281   setSrcLoc (mkSrcLoc file (srcSpanEndLine span) (srcSpanEndCol span))
282   popLexState
283   pushLexState code
284   lexToken
285
286 -- -----------------------------------------------------------------------------
287 -- This is the top-level function: called from the parser each time a
288 -- new token is to be read from the input.
289
290 cmmlex :: (Located CmmToken -> P a) -> P a
291 cmmlex cont = do
292   tok@(L _ tok__) <- lexToken
293   --trace ("token: " ++ show tok__) $ do
294   cont tok
295
296 lexToken :: P (Located CmmToken)
297 lexToken = do
298   inp@(loc1,buf) <- getInput
299   sc <- getLexState
300   case alexScan inp sc of
301     AlexEOF -> do let span = mkSrcSpan loc1 loc1
302                   setLastToken span 0
303                   return (L span CmmT_EOF)
304     AlexError (loc2,_) -> do failLocMsgP loc1 loc2 "lexical error"
305     AlexSkip inp2 _ -> do
306         setInput inp2
307         lexToken
308     AlexToken inp2@(end,buf2) len t -> do
309         setInput inp2
310         let span = mkSrcSpan loc1 end
311         span `seq` setLastToken span len
312         t span buf len
313
314 -- -----------------------------------------------------------------------------
315 -- Monad stuff
316
317 -- Stuff that Alex needs to know about our input type:
318 type AlexInput = (SrcLoc,StringBuffer)
319
320 alexInputPrevChar :: AlexInput -> Char
321 alexInputPrevChar (_,s) = prevChar s '\n'
322
323 alexGetChar :: AlexInput -> Maybe (Char,AlexInput)
324 alexGetChar (loc,s) 
325   | atEnd s   = Nothing
326   | otherwise = c `seq` loc' `seq` s' `seq` Just (c, (loc', s'))
327   where c = currentChar s
328         loc' = advanceSrcLoc loc c
329         s'   = stepOn s
330
331 getInput :: P AlexInput
332 getInput = P $ \s@PState{ loc=l, buffer=b } -> POk s (l,b)
333
334 setInput :: AlexInput -> P ()
335 setInput (l,b) = P $ \s -> POk s{ loc=l, buffer=b } ()
336 }