Don't restrict filenames in line pragmas to printable characters; fixes #4207
[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 {-# 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
19 -- for details
20
21 module CmmLex (
22    CmmToken(..), cmmlex,
23   ) where
24
25 import Cmm
26 import Lexer
27
28 import SrcLoc
29 import UniqFM
30 import StringBuffer
31 import FastString
32 import Ctype
33 import Util
34 --import TRACE
35 }
36
37 $whitechar   = [\ \t\n\r\f\v\xa0] -- \xa0 is Unicode no-break space
38 $white_no_nl = $whitechar # \n
39
40 $ascdigit  = 0-9
41 $unidigit  = \x01 -- Trick Alex into handling Unicode. See alexGetChar.
42 $digit     = [$ascdigit $unidigit]
43 $octit     = 0-7
44 $hexit     = [$digit A-F a-f]
45
46 $unilarge  = \x03 -- Trick Alex into handling Unicode. See alexGetChar.
47 $asclarge  = [A-Z \xc0-\xd6 \xd8-\xde]
48 $large     = [$asclarge $unilarge]
49
50 $unismall  = \x04 -- Trick Alex into handling Unicode. See alexGetChar.
51 $ascsmall  = [a-z \xdf-\xf6 \xf8-\xff]
52 $small     = [$ascsmall $unismall \_]
53
54 $namebegin = [$large $small \. \$ \@]
55 $namechar  = [$namebegin $digit]
56
57 @decimal     = $digit+
58 @octal       = $octit+
59 @hexadecimal = $hexit+
60 @exponent    = [eE] [\-\+]? @decimal
61
62 @floating_point = @decimal \. @decimal @exponent? | @decimal @exponent
63
64 @escape      = \\ ([abfnrt\\\'\"\?] | x $hexit{1,2} | $octit{1,3})
65 @strchar     = ($printable # [\"\\]) | @escape
66
67 cmm :-
68
69 $white_no_nl+           ;
70 ^\# pragma .* \n        ; -- Apple GCC 3.3 CPP generates pragmas in its output
71
72 ^\# (line)?             { begin line_prag }
73
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 }
79
80 <0> {
81   \n                    ;
82
83   [\:\;\{\}\[\]\(\)\=\`\~\/\*\%\-\+\&\^\|\>\<\,\!]      { special_char }
84   
85   ".."                  { kw CmmT_DotDot }
86   "::"                  { kw CmmT_DoubleColon }
87   ">>"                  { kw CmmT_Shr }
88   "<<"                  { kw CmmT_Shl }
89   ">="                  { kw CmmT_Ge }
90   "<="                  { kw CmmT_Le }
91   "=="                  { kw CmmT_Eq }
92   "!="                  { kw CmmT_Ne }
93   "&&"                  { kw CmmT_BoolAnd }
94   "||"                  { kw CmmT_BoolOr }
95   
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 }
101   Sp                    { global_reg Sp }
102   SpLim                 { global_reg SpLim }
103   Hp                    { global_reg Hp }
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 }
109   
110   $namebegin $namechar* { name }
111   
112   0 @octal              { tok_octal }
113   @decimal              { tok_decimal }
114   0[xX] @hexadecimal    { tok_hexadecimal }
115   @floating_point       { strtoken tok_float }
116   
117   \" @strchar* \"       { strtoken tok_string }
118 }
119
120 {
121 data CmmToken
122   = CmmT_SpecChar  Char
123   | CmmT_DotDot
124   | CmmT_DoubleColon
125   | CmmT_Shr
126   | CmmT_Shl
127   | CmmT_Ge
128   | CmmT_Le
129   | CmmT_Eq
130   | CmmT_Ne
131   | CmmT_BoolAnd
132   | CmmT_BoolOr
133   | CmmT_CLOSURE
134   | CmmT_INFO_TABLE
135   | CmmT_INFO_TABLE_RET
136   | CmmT_INFO_TABLE_FUN
137   | CmmT_INFO_TABLE_CONSTR
138   | CmmT_INFO_TABLE_SELECTOR
139   | CmmT_else
140   | CmmT_export
141   | CmmT_section
142   | CmmT_align
143   | CmmT_goto
144   | CmmT_if
145   | CmmT_jump
146   | CmmT_foreign
147   | CmmT_never
148   | CmmT_prim
149   | CmmT_return
150   | CmmT_returns
151   | CmmT_import
152   | CmmT_switch
153   | CmmT_case
154   | CmmT_default
155   | CmmT_bits8
156   | CmmT_bits16
157   | CmmT_bits32
158   | CmmT_bits64
159   | CmmT_float32
160   | CmmT_float64
161   | CmmT_gcptr
162   | CmmT_GlobalReg GlobalReg
163   | CmmT_Name      FastString
164   | CmmT_String    String
165   | CmmT_Int       Integer
166   | CmmT_Float     Rational
167   | CmmT_EOF
168 #ifdef DEBUG
169   deriving (Show)
170 #endif
171
172 -- -----------------------------------------------------------------------------
173 -- Lexer actions
174
175 type Action = SrcSpan -> StringBuffer -> Int -> P (Located CmmToken)
176
177 begin :: Int -> Action
178 begin code _span _str _len = do pushLexState code; lexToken
179
180 pop :: Action
181 pop _span _buf _len = do popLexState; lexToken
182
183 special_char :: Action
184 special_char span buf len = return (L span (CmmT_SpecChar (currentChar buf)))
185
186 kw :: CmmToken -> Action
187 kw tok span buf len = return (L span tok)
188
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
194
195 global_reg :: GlobalReg -> Action
196 global_reg r span buf len = return (L span (CmmT_GlobalReg r))
197
198 strtoken :: (String -> CmmToken) -> Action
199 strtoken f span buf len = 
200   return (L span $! (f $! lexemeToString buf len))
201
202 name :: Action
203 name span buf len = 
204   case lookupUFM reservedWordsFM fs of
205         Just tok -> return (L span tok)
206         Nothing  -> return (L span (CmmT_Name fs))
207   where
208         fs = lexemeToFastString buf len
209
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 ),
223         ( "if",                 CmmT_if ),
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 ),
240 -- New forms
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 )
248         ]
249
250 tok_decimal span buf len 
251   = return (L span (CmmT_Int  $! parseUnsignedInteger buf len 10 octDecDigit))
252
253 tok_octal span buf len 
254   = return (L span (CmmT_Int  $! parseUnsignedInteger (offsetBytes 1 buf) (len-1) 8 octDecDigit))
255
256 tok_hexadecimal span buf len 
257   = return (L span (CmmT_Int  $! parseUnsignedInteger (offsetBytes 2 buf) (len-2) 16 hexDigit))
258
259 tok_float str = CmmT_Float $! readRational str
260
261 tok_string str = CmmT_String (read str)
262                  -- urk, not quite right, but it'll do for now
263
264 -- -----------------------------------------------------------------------------
265 -- Line pragmas
266
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
273   popLexState
274   pushLexState code
275   lexToken
276
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))
281   popLexState
282   pushLexState code
283   lexToken
284
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.
288
289 cmmlex :: (Located CmmToken -> P a) -> P a
290 cmmlex cont = do
291   tok@(L _ tok__) <- lexToken
292   --trace ("token: " ++ show tok__) $ do
293   cont tok
294
295 lexToken :: P (Located CmmToken)
296 lexToken = do
297   inp@(loc1,buf) <- getInput
298   sc <- getLexState
299   case alexScan inp sc of
300     AlexEOF -> do let span = mkSrcSpan loc1 loc1
301                   setLastToken span 0
302                   return (L span CmmT_EOF)
303     AlexError (loc2,_) -> do failLocMsgP loc1 loc2 "lexical error"
304     AlexSkip inp2 _ -> do
305         setInput inp2
306         lexToken
307     AlexToken inp2@(end,buf2) len t -> do
308         setInput inp2
309         let span = mkSrcSpan loc1 end
310         span `seq` setLastToken span len
311         t span buf len
312
313 -- -----------------------------------------------------------------------------
314 -- Monad stuff
315
316 -- Stuff that Alex needs to know about our input type:
317 type AlexInput = (SrcLoc,StringBuffer)
318
319 alexInputPrevChar :: AlexInput -> Char
320 alexInputPrevChar (_,s) = prevChar s '\n'
321
322 alexGetChar :: AlexInput -> Maybe (Char,AlexInput)
323 alexGetChar (loc,s) 
324   | atEnd s   = Nothing
325   | otherwise = c `seq` loc' `seq` s' `seq` Just (c, (loc', s'))
326   where c = currentChar s
327         loc' = advanceSrcLoc loc c
328         s'   = stepOn s
329
330 getInput :: P AlexInput
331 getInput = P $ \s@PState{ loc=l, buffer=b } -> POk s (l,b)
332
333 setInput :: AlexInput -> P ()
334 setInput (l,b) = P $ \s -> POk s{ loc=l, buffer=b } ()
335 }