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 {-# 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
38 $whitechar = [\ \t\n\r\f\v\xa0] -- \xa0 is Unicode no-break space
39 $white_no_nl = $whitechar # \n
42 $unidigit = \x01 -- Trick Alex into handling Unicode. See alexGetChar.
43 $digit = [$ascdigit $unidigit]
45 $hexit = [$digit A-F a-f]
47 $unilarge = \x03 -- Trick Alex into handling Unicode. See alexGetChar.
48 $asclarge = [A-Z \xc0-\xd6 \xd8-\xde]
49 $large = [$asclarge $unilarge]
51 $unismall = \x04 -- Trick Alex into handling Unicode. See alexGetChar.
52 $ascsmall = [a-z \xdf-\xf6 \xf8-\xff]
53 $small = [$ascsmall $unismall \_]
55 $namebegin = [$large $small \. \$ \@]
56 $namechar = [$namebegin $digit]
60 @hexadecimal = $hexit+
61 @exponent = [eE] [\-\+]? @decimal
63 @floating_point = @decimal \. @decimal @exponent? | @decimal @exponent
65 @escape = \\ ([abfnrt\\\'\"\?] | x $hexit{1,2} | $octit{1,3})
66 @strchar = ($printable # [\"\\]) | @escape
71 ^\# pragma .* \n ; -- Apple GCC 3.3 CPP generates pragmas in its output
73 ^\# (line)? { begin line_prag }
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 }
84 [\:\;\{\}\[\]\(\)\=\`\~\/\*\%\-\+\&\^\|\>\<\,\!] { special_char }
86 ".." { kw CmmT_DotDot }
87 "::" { kw CmmT_DoubleColon }
94 "&&" { kw CmmT_BoolAnd }
95 "||" { kw CmmT_BoolOr }
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 }
103 SpLim { global_reg SpLim }
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 }
111 $namebegin $namechar* { name }
113 0 @octal { tok_octal }
114 @decimal { tok_decimal }
115 0[xX] @hexadecimal { tok_hexadecimal }
116 @floating_point { strtoken tok_float }
118 \" @strchar* \" { strtoken tok_string }
136 | CmmT_INFO_TABLE_RET
137 | CmmT_INFO_TABLE_FUN
138 | CmmT_INFO_TABLE_CONSTR
139 | CmmT_INFO_TABLE_SELECTOR
163 | CmmT_GlobalReg GlobalReg
164 | CmmT_Name FastString
167 | CmmT_Float Rational
173 -- -----------------------------------------------------------------------------
176 type Action = SrcSpan -> StringBuffer -> Int -> P (Located CmmToken)
178 begin :: Int -> Action
179 begin code _span _str _len = do pushLexState code; lexToken
182 pop _span _buf _len = do popLexState; lexToken
184 special_char :: Action
185 special_char span buf len = return (L span (CmmT_SpecChar (currentChar buf)))
187 kw :: CmmToken -> Action
188 kw tok span buf len = return (L span tok)
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
196 global_reg :: GlobalReg -> Action
197 global_reg r span buf len = return (L span (CmmT_GlobalReg r))
199 strtoken :: (String -> CmmToken) -> Action
200 strtoken f span buf len =
201 return (L span $! (f $! lexemeToString buf len))
205 case lookupUFM reservedWordsFM fs of
206 Just tok -> return (L span tok)
207 Nothing -> return (L span (CmmT_Name fs))
209 fs = lexemeToFastString buf len
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 ),
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 ),
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 )
251 tok_decimal span buf len
252 = return (L span (CmmT_Int $! parseUnsignedInteger buf len 10 octDecDigit))
254 tok_octal span buf len
255 = return (L span (CmmT_Int $! parseUnsignedInteger (offsetBytes 1 buf) (len-1) 8 octDecDigit))
257 tok_hexadecimal span buf len
258 = return (L span (CmmT_Int $! parseUnsignedInteger (offsetBytes 2 buf) (len-2) 16 hexDigit))
260 tok_float str = CmmT_Float $! readRational str
262 tok_string str = CmmT_String (read str)
263 -- urk, not quite right, but it'll do for now
265 -- -----------------------------------------------------------------------------
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
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))
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.
290 cmmlex :: (Located CmmToken -> P a) -> P a
292 tok@(L _ tok__) <- lexToken
293 --trace ("token: " ++ show tok__) $ do
296 lexToken :: P (Located CmmToken)
298 inp@(loc1,buf) <- getInput
300 case alexScan inp sc of
301 AlexEOF -> do let span = mkSrcSpan loc1 loc1
303 return (L span CmmT_EOF)
304 AlexError (loc2,_) -> do failLocMsgP loc1 loc2 "lexical error"
305 AlexSkip inp2 _ -> do
308 AlexToken inp2@(end,buf2) len t -> do
310 let span = mkSrcSpan loc1 end
311 span `seq` setLastToken span len
314 -- -----------------------------------------------------------------------------
317 -- Stuff that Alex needs to know about our input type:
318 type AlexInput = (SrcLoc,StringBuffer)
320 alexInputPrevChar :: AlexInput -> Char
321 alexInputPrevChar (_,s) = prevChar s '\n'
323 alexGetChar :: AlexInput -> Maybe (Char,AlexInput)
326 | otherwise = c `seq` loc' `seq` s' `seq` Just (c, (loc', s'))
327 where c = currentChar s
328 loc' = advanceSrcLoc loc c
331 getInput :: P AlexInput
332 getInput = P $ \s@PState{ loc=l, buffer=b } -> POk s (l,b)
334 setInput :: AlexInput -> P ()
335 setInput (l,b) = P $ \s -> POk s{ loc=l, buffer=b } ()