1 -----------------------------------------------------------------------------
3 -- (c) The University of Glasgow, 2004-2006
5 -- Lexer for concrete Cmm. We try to stay close to the C-- spec, but there
6 -- are a few minor differences:
8 -- * extra keywords for our macros, and float32/float64 types
9 -- * global registers (Sp,Hp, etc.)
11 -----------------------------------------------------------------------------
14 {-# OPTIONS -Wwarn -w #-}
15 -- The above -Wwarn supression flag is a temporary kludge.
16 -- While working on this module you are encouraged to remove it and fix
17 -- any warnings in the module. See
18 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
37 $whitechar = [\ \t\n\r\f\v\xa0] -- \xa0 is Unicode no-break space
38 $white_no_nl = $whitechar # \n
41 $unidigit = \x01 -- Trick Alex into handling Unicode. See alexGetChar.
42 $digit = [$ascdigit $unidigit]
44 $hexit = [$digit A-F a-f]
46 $unilarge = \x03 -- Trick Alex into handling Unicode. See alexGetChar.
47 $asclarge = [A-Z \xc0-\xd6 \xd8-\xde]
48 $large = [$asclarge $unilarge]
50 $unismall = \x04 -- Trick Alex into handling Unicode. See alexGetChar.
51 $ascsmall = [a-z \xdf-\xf6 \xf8-\xff]
52 $small = [$ascsmall $unismall \_]
54 $namebegin = [$large $small \. \$ \@]
55 $namechar = [$namebegin $digit]
59 @hexadecimal = $hexit+
60 @exponent = [eE] [\-\+]? @decimal
62 @floating_point = @decimal \. @decimal @exponent? | @decimal @exponent
64 @escape = \\ ([abfnrt\\\'\"\?] | x $hexit{1,2} | $octit{1,3})
65 @strchar = ($printable # [\"\\]) | @escape
70 ^\# pragma .* \n ; -- Apple GCC 3.3 CPP generates pragmas in its output
72 ^\# (line)? { begin line_prag }
74 -- single-line line pragmas, of the form
75 -- # <line> "<file>" <extra-stuff> \n
76 <line_prag> $digit+ { setLine line_prag1 }
77 <line_prag1> \" [^\"]* \" { setFile line_prag2 }
78 <line_prag2> .* { pop }
83 [\:\;\{\}\[\]\(\)\=\`\~\/\*\%\-\+\&\^\|\>\<\,\!] { special_char }
85 ".." { kw CmmT_DotDot }
86 "::" { kw CmmT_DoubleColon }
93 "&&" { kw CmmT_BoolAnd }
94 "||" { kw CmmT_BoolOr }
96 P@decimal { global_regN (\n -> VanillaReg n VGcPtr) }
97 R@decimal { global_regN (\n -> VanillaReg n VNonGcPtr) }
98 F@decimal { global_regN FloatReg }
99 D@decimal { global_regN DoubleReg }
100 L@decimal { global_regN LongReg }
102 SpLim { global_reg SpLim }
104 HpLim { global_reg HpLim }
105 CurrentTSO { global_reg CurrentTSO }
106 CurrentNursery { global_reg CurrentNursery }
107 HpAlloc { global_reg HpAlloc }
108 BaseReg { global_reg BaseReg }
110 $namebegin $namechar* { name }
112 0 @octal { tok_octal }
113 @decimal { tok_decimal }
114 0[xX] @hexadecimal { tok_hexadecimal }
115 @floating_point { strtoken tok_float }
117 \" @strchar* \" { strtoken tok_string }
135 | CmmT_INFO_TABLE_RET
136 | CmmT_INFO_TABLE_FUN
137 | CmmT_INFO_TABLE_CONSTR
138 | CmmT_INFO_TABLE_SELECTOR
162 | CmmT_GlobalReg GlobalReg
163 | CmmT_Name FastString
166 | CmmT_Float Rational
172 -- -----------------------------------------------------------------------------
175 type Action = SrcSpan -> StringBuffer -> Int -> P (Located CmmToken)
177 begin :: Int -> Action
178 begin code _span _str _len = do pushLexState code; lexToken
181 pop _span _buf _len = do popLexState; lexToken
183 special_char :: Action
184 special_char span buf len = return (L span (CmmT_SpecChar (currentChar buf)))
186 kw :: CmmToken -> Action
187 kw tok span buf len = return (L span tok)
189 global_regN :: (Int -> GlobalReg) -> Action
190 global_regN con span buf len
191 = return (L span (CmmT_GlobalReg (con (fromIntegral n))))
192 where buf' = stepOn buf
193 n = parseUnsignedInteger buf' (len-1) 10 octDecDigit
195 global_reg :: GlobalReg -> Action
196 global_reg r span buf len = return (L span (CmmT_GlobalReg r))
198 strtoken :: (String -> CmmToken) -> Action
199 strtoken f span buf len =
200 return (L span $! (f $! lexemeToString buf len))
204 case lookupUFM reservedWordsFM fs of
205 Just tok -> return (L span tok)
206 Nothing -> return (L span (CmmT_Name fs))
208 fs = lexemeToFastString buf len
210 reservedWordsFM = listToUFM $
211 map (\(x, y) -> (mkFastString x, y)) [
212 ( "CLOSURE", CmmT_CLOSURE ),
213 ( "INFO_TABLE", CmmT_INFO_TABLE ),
214 ( "INFO_TABLE_RET", CmmT_INFO_TABLE_RET ),
215 ( "INFO_TABLE_FUN", CmmT_INFO_TABLE_FUN ),
216 ( "INFO_TABLE_CONSTR", CmmT_INFO_TABLE_CONSTR ),
217 ( "INFO_TABLE_SELECTOR",CmmT_INFO_TABLE_SELECTOR ),
218 ( "else", CmmT_else ),
219 ( "export", CmmT_export ),
220 ( "section", CmmT_section ),
221 ( "align", CmmT_align ),
222 ( "goto", CmmT_goto ),
224 ( "jump", CmmT_jump ),
225 ( "foreign", CmmT_foreign ),
226 ( "never", CmmT_never ),
227 ( "prim", CmmT_prim ),
228 ( "return", CmmT_return ),
229 ( "returns", CmmT_returns ),
230 ( "import", CmmT_import ),
231 ( "switch", CmmT_switch ),
232 ( "case", CmmT_case ),
233 ( "default", CmmT_default ),
234 ( "bits8", CmmT_bits8 ),
235 ( "bits16", CmmT_bits16 ),
236 ( "bits32", CmmT_bits32 ),
237 ( "bits64", CmmT_bits64 ),
238 ( "float32", CmmT_float32 ),
239 ( "float64", CmmT_float64 ),
241 ( "b8", CmmT_bits8 ),
242 ( "b16", CmmT_bits16 ),
243 ( "b32", CmmT_bits32 ),
244 ( "b64", CmmT_bits64 ),
245 ( "f32", CmmT_float32 ),
246 ( "f64", CmmT_float64 ),
247 ( "gcptr", CmmT_gcptr )
250 tok_decimal span buf len
251 = return (L span (CmmT_Int $! parseUnsignedInteger buf len 10 octDecDigit))
253 tok_octal span buf len
254 = return (L span (CmmT_Int $! parseUnsignedInteger (offsetBytes 1 buf) (len-1) 8 octDecDigit))
256 tok_hexadecimal span buf len
257 = return (L span (CmmT_Int $! parseUnsignedInteger (offsetBytes 2 buf) (len-2) 16 hexDigit))
259 tok_float str = CmmT_Float $! readRational str
261 tok_string str = CmmT_String (read str)
262 -- urk, not quite right, but it'll do for now
264 -- -----------------------------------------------------------------------------
267 setLine :: Int -> Action
268 setLine code span buf len = do
269 let line = parseUnsignedInteger buf len 10 octDecDigit
270 setSrcLoc (mkSrcLoc (srcSpanFile span) (fromIntegral line - 1) 1)
271 -- subtract one: the line number refers to the *following* line
272 -- trace ("setLine " ++ show line) $ do
277 setFile :: Int -> Action
278 setFile code span buf len = do
279 let file = lexemeToFastString (stepOn buf) (len-2)
280 setSrcLoc (mkSrcLoc file (srcSpanEndLine span) (srcSpanEndCol span))
285 -- -----------------------------------------------------------------------------
286 -- This is the top-level function: called from the parser each time a
287 -- new token is to be read from the input.
289 cmmlex :: (Located CmmToken -> P a) -> P a
291 tok@(L _ tok__) <- lexToken
292 --trace ("token: " ++ show tok__) $ do
295 lexToken :: P (Located CmmToken)
297 inp@(loc1,buf) <- getInput
299 case alexScan inp sc of
300 AlexEOF -> do let span = mkSrcSpan loc1 loc1
302 return (L span CmmT_EOF)
303 AlexError (loc2,_) -> do failLocMsgP loc1 loc2 "lexical error"
304 AlexSkip inp2 _ -> do
307 AlexToken inp2@(end,buf2) len t -> do
309 let span = mkSrcSpan loc1 end
310 span `seq` setLastToken span len
313 -- -----------------------------------------------------------------------------
316 -- Stuff that Alex needs to know about our input type:
317 type AlexInput = (SrcLoc,StringBuffer)
319 alexInputPrevChar :: AlexInput -> Char
320 alexInputPrevChar (_,s) = prevChar s '\n'
322 alexGetChar :: AlexInput -> Maybe (Char,AlexInput)
325 | otherwise = c `seq` loc' `seq` s' `seq` Just (c, (loc', s'))
326 where c = currentChar s
327 loc' = advanceSrcLoc loc c
330 getInput :: P AlexInput
331 getInput = P $ \s@PState{ loc=l, buffer=b } -> POk s (l,b)
333 setInput :: AlexInput -> P ()
334 setInput (l,b) = P $ \s -> POk s{ loc=l, buffer=b } ()