1 -----------------------------------------------------------------------------
2 -- (c) The University of Glasgow, 2004
4 -- Lexer for concrete Cmm. We try to stay close to the C-- spec, but there
5 -- are a few minor differences:
7 -- * extra keywords for our macros, and float32/float64 types
8 -- * global registers (Sp,Hp, etc.)
10 -----------------------------------------------------------------------------
17 #include "HsVersions.h"
27 import Util ( readRational )
31 $whitechar = [\ \t\n\r\f\v\xa0]
32 $white_no_nl = $whitechar # \n
36 $digit = [$ascdigit $unidigit]
38 $hexit = [$digit A-F a-f]
41 $asclarge = [A-Z \xc0-\xd6 \xd8-\xde]
42 $large = [$asclarge $unilarge]
45 $ascsmall = [a-z \xdf-\xf6 \xf8-\xff]
46 $small = [$ascsmall $unismall \_]
48 $namebegin = [$large $small \_ \. \$ \@]
49 $namechar = [$namebegin $digit]
53 @hexadecimal = $hexit+
54 @exponent = [eE] [\-\+]? @decimal
56 @floating_point = @decimal \. @decimal @exponent? | @decimal @exponent
58 @escape = \\ ([abfnrt\\\'\"\?] | x @hexadecimal | @octal)
59 @strchar = ($printable # [\"\\]) | @escape
64 ^\# pragma .* \n ; -- Apple GCC 3.3 CPP generates pragmas in its output
66 ^\# (line)? { begin line_prag }
68 -- single-line line pragmas, of the form
69 -- # <line> "<file>" <extra-stuff> \n
70 <line_prag> $digit+ { setLine line_prag1 }
71 <line_prag1> \" ($printable # \")* \" { setFile line_prag2 }
72 <line_prag2> .* { pop }
77 [\:\;\{\}\[\]\(\)\=\`\~\/\*\%\-\+\&\^\|\>\<\,\!] { special_char }
79 ".." { kw CmmT_DotDot }
80 "::" { kw CmmT_DoubleColon }
87 "&&" { kw CmmT_BoolAnd }
88 "||" { kw CmmT_BoolOr }
90 R@decimal { global_regN VanillaReg }
91 F@decimal { global_regN FloatReg }
92 D@decimal { global_regN DoubleReg }
93 L@decimal { global_regN LongReg }
95 SpLim { global_reg SpLim }
97 HpLim { global_reg HpLim }
98 CurrentTSO { global_reg CurrentTSO }
99 CurrentNursery { global_reg CurrentNursery }
100 HpAlloc { global_reg HpAlloc }
102 $namebegin $namechar* { name }
104 0 @octal { tok_octal }
105 @decimal { tok_decimal }
106 0[xX] @hexadecimal { tok_hexadecimal }
107 @floating_point { strtoken tok_float }
109 \" @strchar* \" { strtoken tok_string }
127 | CmmT_INFO_TABLE_RET
128 | CmmT_INFO_TABLE_FUN
129 | CmmT_INFO_TABLE_CONSTR
130 | CmmT_INFO_TABLE_SELECTOR
149 | CmmT_GlobalReg GlobalReg
150 | CmmT_Name FastString
153 | CmmT_Float Rational
159 -- -----------------------------------------------------------------------------
162 type Action = SrcSpan -> StringBuffer -> Int -> P (Located CmmToken)
164 begin :: Int -> Action
165 begin code _span _str _len = do pushLexState code; lexToken
168 pop _span _buf _len = do popLexState; lexToken
170 special_char :: Action
171 special_char span buf len = return (L span (CmmT_SpecChar (currentChar buf)))
173 kw :: CmmToken -> Action
174 kw tok span buf len = return (L span tok)
176 global_regN :: (Int -> GlobalReg) -> Action
177 global_regN con span buf len
178 = return (L span (CmmT_GlobalReg (con (fromIntegral n))))
179 where buf' = stepOn buf
180 n = parseInteger buf' (len-1) 10 octDecDigit
182 global_reg :: GlobalReg -> Action
183 global_reg r span buf len = return (L span (CmmT_GlobalReg r))
185 strtoken :: (String -> CmmToken) -> Action
186 strtoken f span buf len =
187 return (L span $! (f $! lexemeToString buf len))
191 case lookupUFM reservedWordsFM fs of
192 Just tok -> return (L span tok)
193 Nothing -> return (L span (CmmT_Name fs))
195 fs = lexemeToFastString buf len
197 reservedWordsFM = listToUFM $
198 map (\(x, y) -> (mkFastString x, y)) [
199 ( "CLOSURE", CmmT_CLOSURE ),
200 ( "INFO_TABLE", CmmT_INFO_TABLE ),
201 ( "INFO_TABLE_RET", CmmT_INFO_TABLE_RET ),
202 ( "INFO_TABLE_FUN", CmmT_INFO_TABLE_FUN ),
203 ( "INFO_TABLE_CONSTR", CmmT_INFO_TABLE_CONSTR ),
204 ( "INFO_TABLE_SELECTOR",CmmT_INFO_TABLE_SELECTOR ),
205 ( "else", CmmT_else ),
206 ( "export", CmmT_export ),
207 ( "section", CmmT_section ),
208 ( "align", CmmT_align ),
209 ( "goto", CmmT_goto ),
211 ( "jump", CmmT_jump ),
212 ( "foreign", CmmT_foreign ),
213 ( "import", CmmT_import ),
214 ( "switch", CmmT_switch ),
215 ( "case", CmmT_case ),
216 ( "default", CmmT_default ),
217 ( "bits8", CmmT_bits8 ),
218 ( "bits16", CmmT_bits16 ),
219 ( "bits32", CmmT_bits32 ),
220 ( "bits64", CmmT_bits64 ),
221 ( "float32", CmmT_float32 ),
222 ( "float64", CmmT_float64 )
225 tok_decimal span buf len
226 = return (L span (CmmT_Int $! parseInteger buf len 10 octDecDigit))
228 tok_octal span buf len
229 = return (L span (CmmT_Int $! parseInteger (stepOn buf) (len-1) 8 octDecDigit))
231 tok_hexadecimal span buf len
232 = return (L span (CmmT_Int $! parseInteger (stepOnBy 2 buf) (len-2) 16 hexDigit))
234 tok_float str = CmmT_Float $! readRational str
236 tok_string str = CmmT_String (read str)
237 -- urk, not quite right, but it'll do for now
239 -- -----------------------------------------------------------------------------
242 setLine :: Int -> Action
243 setLine code span buf len = do
244 let line = parseInteger buf len 10 octDecDigit
245 setSrcLoc (mkSrcLoc (srcSpanFile span) (fromIntegral line - 1) 0)
246 -- subtract one: the line number refers to the *following* line
247 -- trace ("setLine " ++ show line) $ do
252 setFile :: Int -> Action
253 setFile code span buf len = do
254 let file = lexemeToFastString (stepOn buf) (len-2)
255 setSrcLoc (mkSrcLoc file (srcSpanEndLine span) (srcSpanEndCol span))
260 -- -----------------------------------------------------------------------------
261 -- This is the top-level function: called from the parser each time a
262 -- new token is to be read from the input.
264 cmmlex :: (Located CmmToken -> P a) -> P a
266 tok@(L _ tok__) <- lexToken
267 --trace ("token: " ++ show tok__) $ do
270 lexToken :: P (Located CmmToken)
272 inp@(loc1,buf) <- getInput
274 case alexScan inp sc of
275 AlexEOF -> do let span = mkSrcSpan loc1 loc1
277 return (L span CmmT_EOF)
278 AlexError (loc2,_) -> do failLocMsgP loc1 loc2 "lexical error"
279 AlexSkip inp2 _ -> do
282 AlexToken inp2@(end,buf2) len t -> do
284 let span = mkSrcSpan loc1 end
285 span `seq` setLastToken span len
288 -- -----------------------------------------------------------------------------
291 -- Stuff that Alex needs to know about our input type:
292 type AlexInput = (SrcLoc,StringBuffer)
294 alexInputPrevChar :: AlexInput -> Char
295 alexInputPrevChar (_,s) = prevChar s '\n'
297 alexGetChar :: AlexInput -> Maybe (Char,AlexInput)
300 | otherwise = c `seq` loc' `seq` s' `seq` Just (c, (loc', s'))
301 where c = currentChar s
302 loc' = advanceSrcLoc loc c
305 getInput :: P AlexInput
306 getInput = P $ \s@PState{ loc=l, buffer=b } -> POk s (l,b)
308 setInput :: AlexInput -> P ()
309 setInput (l,b) = P $ \s -> POk s{ loc=l, buffer=b } ()