[project @ 2004-08-13 13:04:50 by simonmar]
[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
65 ^\# (line)?             { begin line_prag }
66
67 -- single-line line pragmas, of the form
68 --    # <line> "<file>" <extra-stuff> \n
69 <line_prag> $digit+                     { setLine line_prag1 }
70 <line_prag1> \" ($printable # \")* \"   { setFile line_prag2 }
71 <line_prag2> .*                         { pop }
72
73 <0> {
74   \n                    ;
75
76   [\:\;\{\}\[\]\(\)\=\`\~\/\*\%\-\+\&\^\|\>\<\,\!]      { special_char }
77   
78   ".."                  { kw CmmT_DotDot }
79   "::"                  { kw CmmT_DoubleColon }
80   ">>"                  { kw CmmT_Shr }
81   "<<"                  { kw CmmT_Shl }
82   ">="                  { kw CmmT_Ge }
83   "<="                  { kw CmmT_Le }
84   "=="                  { kw CmmT_Eq }
85   "!="                  { kw CmmT_Ne }
86   "&&"                  { kw CmmT_BoolAnd }
87   "||"                  { kw CmmT_BoolOr }
88   
89   R@decimal             { global_regN VanillaReg }
90   F@decimal             { global_regN FloatReg }
91   D@decimal             { global_regN DoubleReg }
92   L@decimal             { global_regN LongReg }
93   Sp                    { global_reg Sp }
94   SpLim                 { global_reg SpLim }
95   Hp                    { global_reg Hp }
96   HpLim                 { global_reg HpLim }
97   CurrentTSO            { global_reg CurrentTSO }
98   CurrentNursery        { global_reg CurrentNursery }
99   HpAlloc               { global_reg HpAlloc }
100   
101   $namebegin $namechar* { name }
102   
103   0 @octal              { tok_octal }
104   @decimal              { tok_decimal }
105   0[xX] @hexadecimal    { tok_hexadecimal }
106   @floating_point       { strtoken tok_float }
107   
108   \" @strchar* \"       { strtoken tok_string }
109 }
110
111 {
112 data CmmToken
113   = CmmT_SpecChar  Char
114   | CmmT_DotDot
115   | CmmT_DoubleColon
116   | CmmT_Shr
117   | CmmT_Shl
118   | CmmT_Ge
119   | CmmT_Le
120   | CmmT_Eq
121   | CmmT_Ne
122   | CmmT_BoolAnd
123   | CmmT_BoolOr
124   | CmmT_CLOSURE
125   | CmmT_INFO_TABLE
126   | CmmT_INFO_TABLE_RET
127   | CmmT_INFO_TABLE_FUN
128   | CmmT_INFO_TABLE_CONSTR
129   | CmmT_INFO_TABLE_SELECTOR
130   | CmmT_else
131   | CmmT_export
132   | CmmT_section
133   | CmmT_align
134   | CmmT_goto
135   | CmmT_if
136   | CmmT_jump
137   | CmmT_foreign
138   | CmmT_import
139   | CmmT_switch
140   | CmmT_case
141   | CmmT_default
142   | CmmT_bits8
143   | CmmT_bits16
144   | CmmT_bits32
145   | CmmT_bits64
146   | CmmT_float32
147   | CmmT_float64
148   | CmmT_GlobalReg GlobalReg
149   | CmmT_Name      FastString
150   | CmmT_String    String
151   | CmmT_Int       Integer
152   | CmmT_Float     Rational
153   | CmmT_EOF
154 #ifdef DEBUG
155   deriving (Show)
156 #endif
157
158 -- -----------------------------------------------------------------------------
159 -- Lexer actions
160
161 type Action = SrcSpan -> StringBuffer -> Int -> P (Located CmmToken)
162
163 begin :: Int -> Action
164 begin code _span _str _len = do pushLexState code; lexToken
165
166 pop :: Action
167 pop _span _buf _len = do popLexState; lexToken
168
169 special_char :: Action
170 special_char span buf len = return (L span (CmmT_SpecChar (currentChar buf)))
171
172 kw :: CmmToken -> Action
173 kw tok span buf len = return (L span tok)
174
175 global_regN :: (Int -> GlobalReg) -> Action
176 global_regN con span buf len 
177   = return (L span (CmmT_GlobalReg (con (fromIntegral n))))
178   where buf' = stepOn buf
179         n = parseInteger buf' (len-1) 10 octDecDigit
180
181 global_reg :: GlobalReg -> Action
182 global_reg r span buf len = return (L span (CmmT_GlobalReg r))
183
184 strtoken :: (String -> CmmToken) -> Action
185 strtoken f span buf len = 
186   return (L span $! (f $! lexemeToString buf len))
187
188 name :: Action
189 name span buf len = 
190   case lookupUFM reservedWordsFM fs of
191         Just tok -> return (L span tok)
192         Nothing  -> return (L span (CmmT_Name fs))
193   where
194         fs = lexemeToFastString buf len
195
196 reservedWordsFM = listToUFM $
197         map (\(x, y) -> (mkFastString x, y)) [
198         ( "CLOSURE",            CmmT_CLOSURE ),
199         ( "INFO_TABLE",         CmmT_INFO_TABLE ),
200         ( "INFO_TABLE_RET",     CmmT_INFO_TABLE_RET ),
201         ( "INFO_TABLE_FUN",     CmmT_INFO_TABLE_FUN ),
202         ( "INFO_TABLE_CONSTR",  CmmT_INFO_TABLE_CONSTR ),
203         ( "INFO_TABLE_SELECTOR",CmmT_INFO_TABLE_SELECTOR ),
204         ( "else",               CmmT_else ),
205         ( "export",             CmmT_export ),
206         ( "section",            CmmT_section ),
207         ( "align",              CmmT_align ),
208         ( "goto",               CmmT_goto ),
209         ( "if",                 CmmT_if ),
210         ( "jump",               CmmT_jump ),
211         ( "foreign",            CmmT_foreign ),
212         ( "import",             CmmT_import ),
213         ( "switch",             CmmT_switch ),
214         ( "case",               CmmT_case ),
215         ( "default",            CmmT_default ),
216         ( "bits8",              CmmT_bits8 ),
217         ( "bits16",             CmmT_bits16 ),
218         ( "bits32",             CmmT_bits32 ),
219         ( "bits64",             CmmT_bits64 ),
220         ( "float32",            CmmT_float32 ),
221         ( "float64",            CmmT_float64 )
222         ]
223
224 tok_decimal span buf len 
225   = return (L span (CmmT_Int  $! parseInteger buf len 10 octDecDigit))
226
227 tok_octal span buf len 
228   = return (L span (CmmT_Int  $! parseInteger (stepOn buf) (len-1) 8 octDecDigit))
229
230 tok_hexadecimal span buf len 
231   = return (L span (CmmT_Int  $! parseInteger (stepOnBy 2 buf) (len-2) 16 hexDigit))
232
233 tok_float str = CmmT_Float $! readRational str
234
235 tok_string str = CmmT_String (read str)
236                  -- urk, not quite right, but it'll do for now
237
238 -- -----------------------------------------------------------------------------
239 -- Line pragmas
240
241 setLine :: Int -> Action
242 setLine code span buf len = do
243   let line = parseInteger buf len 10 octDecDigit
244   setSrcLoc (mkSrcLoc (srcSpanFile span) (fromIntegral line - 1) 0)
245         -- subtract one: the line number refers to the *following* line
246   -- trace ("setLine "  ++ show line) $ do
247   popLexState
248   pushLexState code
249   lexToken
250
251 setFile :: Int -> Action
252 setFile code span buf len = do
253   let file = lexemeToFastString (stepOn buf) (len-2)
254   setSrcLoc (mkSrcLoc file (srcSpanEndLine span) (srcSpanEndCol span))
255   popLexState
256   pushLexState code
257   lexToken
258
259 -- -----------------------------------------------------------------------------
260 -- This is the top-level function: called from the parser each time a
261 -- new token is to be read from the input.
262
263 cmmlex :: (Located CmmToken -> P a) -> P a
264 cmmlex cont = do
265   tok@(L _ tok__) <- lexToken
266   --trace ("token: " ++ show tok__) $ do
267   cont tok
268
269 lexToken :: P (Located CmmToken)
270 lexToken = do
271   inp@(loc1,buf) <- getInput
272   sc <- getLexState
273   case alexScan inp sc of
274     AlexEOF -> do let span = mkSrcSpan loc1 loc1
275                   setLastToken span 0
276                   return (L span CmmT_EOF)
277     AlexError (loc2,_) -> do failLocMsgP loc1 loc2 "lexical error"
278     AlexSkip inp2 _ -> do
279         setInput inp2
280         lexToken
281     AlexToken inp2@(end,buf2) len t -> do
282         setInput inp2
283         let span = mkSrcSpan loc1 end
284         span `seq` setLastToken span len
285         t span buf len
286
287 -- -----------------------------------------------------------------------------
288 -- Monad stuff
289
290 -- Stuff that Alex needs to know about our input type:
291 type AlexInput = (SrcLoc,StringBuffer)
292
293 alexInputPrevChar :: AlexInput -> Char
294 alexInputPrevChar (_,s) = prevChar s '\n'
295
296 alexGetChar :: AlexInput -> Maybe (Char,AlexInput)
297 alexGetChar (loc,s) 
298   | atEnd s   = Nothing
299   | otherwise = c `seq` loc' `seq` s' `seq` Just (c, (loc', s'))
300   where c = currentChar s
301         loc' = advanceSrcLoc loc c
302         s'   = stepOn s
303
304 getInput :: P AlexInput
305 getInput = P $ \s@PState{ loc=l, buffer=b } -> POk s (l,b)
306
307 setInput :: AlexInput -> P ()
308 setInput (l,b) = P $ \s -> POk s{ loc=l, buffer=b } ()
309 }