[project @ 2004-09-15 02:47:03 by wolfgang]
[ghc-hetmet.git] / ghc / compiler / cmm / CmmLex.x
1 -----------------------------------------------------------------------------
2 -- (c) The University of Glasgow, 2004
3 --
4 -- Lexer for concrete Cmm.  We try to stay close to the C-- spec, but there
5 -- are a few minor differences:
6 --
7 --   * extra keywords for our macros, and float32/float64 types
8 --   * global registers (Sp,Hp, etc.)
9 --
10 -----------------------------------------------------------------------------
11
12 {
13 module CmmLex (
14    CmmToken(..), cmmlex,
15   ) where
16
17 #include "HsVersions.h"
18
19 import Cmm
20 import Lexer
21
22 import SrcLoc
23 import UniqFM
24 import StringBuffer
25 import FastString
26 import Ctype
27 import Util             ( readRational )
28 --import TRACE
29 }
30
31 $whitechar   = [\ \t\n\r\f\v\xa0]
32 $white_no_nl = $whitechar # \n
33
34 $ascdigit  = 0-9
35 $unidigit  = \x01
36 $digit     = [$ascdigit $unidigit]
37 $octit     = 0-7
38 $hexit     = [$digit A-F a-f]
39
40 $unilarge  = \x03
41 $asclarge  = [A-Z \xc0-\xd6 \xd8-\xde]
42 $large     = [$asclarge $unilarge]
43
44 $unismall  = \x04
45 $ascsmall  = [a-z \xdf-\xf6 \xf8-\xff]
46 $small     = [$ascsmall $unismall \_]
47
48 $namebegin = [$large $small \_ \. \$ \@]
49 $namechar  = [$namebegin $digit]
50
51 @decimal     = $digit+
52 @octal       = $octit+
53 @hexadecimal = $hexit+
54 @exponent    = [eE] [\-\+]? @decimal
55
56 @floating_point = @decimal \. @decimal @exponent? | @decimal @exponent
57
58 @escape      = \\ ([abfnrt\\\'\"\?] | x @hexadecimal | @octal)
59 @strchar     = ($printable # [\"\\]) | @escape
60
61 cmm :-
62
63 $white_no_nl+           ;
64 ^\# pragma .* \n        ; -- Apple GCC 3.3 CPP generates pragmas in its output
65
66 ^\# (line)?             { begin line_prag }
67
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 }
73
74 <0> {
75   \n                    ;
76
77   [\:\;\{\}\[\]\(\)\=\`\~\/\*\%\-\+\&\^\|\>\<\,\!]      { special_char }
78   
79   ".."                  { kw CmmT_DotDot }
80   "::"                  { kw CmmT_DoubleColon }
81   ">>"                  { kw CmmT_Shr }
82   "<<"                  { kw CmmT_Shl }
83   ">="                  { kw CmmT_Ge }
84   "<="                  { kw CmmT_Le }
85   "=="                  { kw CmmT_Eq }
86   "!="                  { kw CmmT_Ne }
87   "&&"                  { kw CmmT_BoolAnd }
88   "||"                  { kw CmmT_BoolOr }
89   
90   R@decimal             { global_regN VanillaReg }
91   F@decimal             { global_regN FloatReg }
92   D@decimal             { global_regN DoubleReg }
93   L@decimal             { global_regN LongReg }
94   Sp                    { global_reg Sp }
95   SpLim                 { global_reg SpLim }
96   Hp                    { global_reg Hp }
97   HpLim                 { global_reg HpLim }
98   CurrentTSO            { global_reg CurrentTSO }
99   CurrentNursery        { global_reg CurrentNursery }
100   HpAlloc               { global_reg HpAlloc }
101   
102   $namebegin $namechar* { name }
103   
104   0 @octal              { tok_octal }
105   @decimal              { tok_decimal }
106   0[xX] @hexadecimal    { tok_hexadecimal }
107   @floating_point       { strtoken tok_float }
108   
109   \" @strchar* \"       { strtoken tok_string }
110 }
111
112 {
113 data CmmToken
114   = CmmT_SpecChar  Char
115   | CmmT_DotDot
116   | CmmT_DoubleColon
117   | CmmT_Shr
118   | CmmT_Shl
119   | CmmT_Ge
120   | CmmT_Le
121   | CmmT_Eq
122   | CmmT_Ne
123   | CmmT_BoolAnd
124   | CmmT_BoolOr
125   | CmmT_CLOSURE
126   | CmmT_INFO_TABLE
127   | CmmT_INFO_TABLE_RET
128   | CmmT_INFO_TABLE_FUN
129   | CmmT_INFO_TABLE_CONSTR
130   | CmmT_INFO_TABLE_SELECTOR
131   | CmmT_else
132   | CmmT_export
133   | CmmT_section
134   | CmmT_align
135   | CmmT_goto
136   | CmmT_if
137   | CmmT_jump
138   | CmmT_foreign
139   | CmmT_import
140   | CmmT_switch
141   | CmmT_case
142   | CmmT_default
143   | CmmT_bits8
144   | CmmT_bits16
145   | CmmT_bits32
146   | CmmT_bits64
147   | CmmT_float32
148   | CmmT_float64
149   | CmmT_GlobalReg GlobalReg
150   | CmmT_Name      FastString
151   | CmmT_String    String
152   | CmmT_Int       Integer
153   | CmmT_Float     Rational
154   | CmmT_EOF
155 #ifdef DEBUG
156   deriving (Show)
157 #endif
158
159 -- -----------------------------------------------------------------------------
160 -- Lexer actions
161
162 type Action = SrcSpan -> StringBuffer -> Int -> P (Located CmmToken)
163
164 begin :: Int -> Action
165 begin code _span _str _len = do pushLexState code; lexToken
166
167 pop :: Action
168 pop _span _buf _len = do popLexState; lexToken
169
170 special_char :: Action
171 special_char span buf len = return (L span (CmmT_SpecChar (currentChar buf)))
172
173 kw :: CmmToken -> Action
174 kw tok span buf len = return (L span tok)
175
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
181
182 global_reg :: GlobalReg -> Action
183 global_reg r span buf len = return (L span (CmmT_GlobalReg r))
184
185 strtoken :: (String -> CmmToken) -> Action
186 strtoken f span buf len = 
187   return (L span $! (f $! lexemeToString buf len))
188
189 name :: Action
190 name span buf len = 
191   case lookupUFM reservedWordsFM fs of
192         Just tok -> return (L span tok)
193         Nothing  -> return (L span (CmmT_Name fs))
194   where
195         fs = lexemeToFastString buf len
196
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 ),
210         ( "if",                 CmmT_if ),
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 )
223         ]
224
225 tok_decimal span buf len 
226   = return (L span (CmmT_Int  $! parseInteger buf len 10 octDecDigit))
227
228 tok_octal span buf len 
229   = return (L span (CmmT_Int  $! parseInteger (stepOn buf) (len-1) 8 octDecDigit))
230
231 tok_hexadecimal span buf len 
232   = return (L span (CmmT_Int  $! parseInteger (stepOnBy 2 buf) (len-2) 16 hexDigit))
233
234 tok_float str = CmmT_Float $! readRational str
235
236 tok_string str = CmmT_String (read str)
237                  -- urk, not quite right, but it'll do for now
238
239 -- -----------------------------------------------------------------------------
240 -- Line pragmas
241
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
248   popLexState
249   pushLexState code
250   lexToken
251
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))
256   popLexState
257   pushLexState code
258   lexToken
259
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.
263
264 cmmlex :: (Located CmmToken -> P a) -> P a
265 cmmlex cont = do
266   tok@(L _ tok__) <- lexToken
267   --trace ("token: " ++ show tok__) $ do
268   cont tok
269
270 lexToken :: P (Located CmmToken)
271 lexToken = do
272   inp@(loc1,buf) <- getInput
273   sc <- getLexState
274   case alexScan inp sc of
275     AlexEOF -> do let span = mkSrcSpan loc1 loc1
276                   setLastToken span 0
277                   return (L span CmmT_EOF)
278     AlexError (loc2,_) -> do failLocMsgP loc1 loc2 "lexical error"
279     AlexSkip inp2 _ -> do
280         setInput inp2
281         lexToken
282     AlexToken inp2@(end,buf2) len t -> do
283         setInput inp2
284         let span = mkSrcSpan loc1 end
285         span `seq` setLastToken span len
286         t span buf len
287
288 -- -----------------------------------------------------------------------------
289 -- Monad stuff
290
291 -- Stuff that Alex needs to know about our input type:
292 type AlexInput = (SrcLoc,StringBuffer)
293
294 alexInputPrevChar :: AlexInput -> Char
295 alexInputPrevChar (_,s) = prevChar s '\n'
296
297 alexGetChar :: AlexInput -> Maybe (Char,AlexInput)
298 alexGetChar (loc,s) 
299   | atEnd s   = Nothing
300   | otherwise = c `seq` loc' `seq` s' `seq` Just (c, (loc', s'))
301   where c = currentChar s
302         loc' = advanceSrcLoc loc c
303         s'   = stepOn s
304
305 getInput :: P AlexInput
306 getInput = P $ \s@PState{ loc=l, buffer=b } -> POk s (l,b)
307
308 setInput :: AlexInput -> P ()
309 setInput (l,b) = P $ \s -> POk s{ loc=l, buffer=b } ()
310 }