2 -- We always optimise this, otherwise performance of a non-optimised
3 -- compiler is severely affected
5 -- -----------------------------------------------------------------------------
7 -- (c) The University of Glasgow, 1997-2006
11 -- -----------------------------------------------------------------------------
30 #include "HsVersions.h"
32 import Data.Char ( ord, chr, isDigit, digitToInt, intToDigit,
34 import Numeric ( showIntAtBase )
36 import GHC.Ptr ( Ptr(..) )
39 -- -----------------------------------------------------------------------------
42 -- We can't write the decoder as efficiently as we'd like without
43 -- resorting to unboxed extensions, unfortunately. I tried to write
44 -- an IO version of this function, but GHC can't eliminate boxed
45 -- results from an IO-returning function.
47 -- We assume we can ignore overflow when parsing a multibyte character here.
48 -- To make this safe, we add extra sentinel bytes to unparsed UTF-8 sequences
49 -- before decoding them (see StringBuffer.hs).
51 {-# INLINE utf8DecodeChar# #-}
52 utf8DecodeChar# :: Addr# -> (# Char#, Addr# #)
54 let ch0 = word2Int# (indexWord8OffAddr# a# 0#) in
56 _ | ch0 <=# 0x7F# -> (# chr# ch0, a# `plusAddr#` 1# #)
58 | ch0 >=# 0xC0# && ch0 <=# 0xDF# ->
59 let ch1 = word2Int# (indexWord8OffAddr# a# 1#) in
60 if ch1 <# 0x80# || ch1 >=# 0xC0# then fail 1# else
61 (# chr# (((ch0 -# 0xC0#) `uncheckedIShiftL#` 6#) +#
65 | ch0 >=# 0xE0# && ch0 <=# 0xEF# ->
66 let ch1 = word2Int# (indexWord8OffAddr# a# 1#) in
67 if ch1 <# 0x80# || ch1 >=# 0xC0# then fail 1# else
68 let ch2 = word2Int# (indexWord8OffAddr# a# 2#) in
69 if ch2 <# 0x80# || ch2 >=# 0xC0# then fail 2# else
70 (# chr# (((ch0 -# 0xE0#) `uncheckedIShiftL#` 12#) +#
71 ((ch1 -# 0x80#) `uncheckedIShiftL#` 6#) +#
75 | ch0 >=# 0xF0# && ch0 <=# 0xF8# ->
76 let ch1 = word2Int# (indexWord8OffAddr# a# 1#) in
77 if ch1 <# 0x80# || ch1 >=# 0xC0# then fail 1# else
78 let ch2 = word2Int# (indexWord8OffAddr# a# 2#) in
79 if ch2 <# 0x80# || ch2 >=# 0xC0# then fail 2# else
80 let ch3 = word2Int# (indexWord8OffAddr# a# 3#) in
81 if ch3 <# 0x80# || ch3 >=# 0xC0# then fail 3# else
82 (# chr# (((ch0 -# 0xF0#) `uncheckedIShiftL#` 18#) +#
83 ((ch1 -# 0x80#) `uncheckedIShiftL#` 12#) +#
84 ((ch2 -# 0x80#) `uncheckedIShiftL#` 6#) +#
88 | otherwise -> fail 1#
90 -- all invalid sequences end up here:
91 fail n = (# '\0'#, a# `plusAddr#` n #)
92 -- '\xFFFD' would be the usual replacement character, but
93 -- that's a valid symbol in Haskell, so will result in a
94 -- confusing parse error later on. Instead we use '\0' which
95 -- will signal a lexer error immediately.
97 utf8DecodeChar :: Ptr Word8 -> (Char, Ptr Word8)
98 utf8DecodeChar (Ptr a#) =
99 case utf8DecodeChar# a# of (# c#, b# #) -> ( C# c#, Ptr b# )
101 -- UTF-8 is cleverly designed so that we can always figure out where
102 -- the start of the current character is, given any position in a
103 -- stream. This function finds the start of the previous character,
104 -- assuming there *is* a previous character.
105 utf8PrevChar :: Ptr Word8 -> IO (Ptr Word8)
106 utf8PrevChar p = utf8CharStart (p `plusPtr` (-1))
108 utf8CharStart :: Ptr Word8 -> IO (Ptr Word8)
109 utf8CharStart p = go p
110 where go p = do w <- peek p
111 if w >= 0x80 && w < 0xC0
112 then go (p `plusPtr` (-1))
115 utf8DecodeString :: Ptr Word8 -> Int -> IO [Char]
116 STRICT2(utf8DecodeString)
117 utf8DecodeString (Ptr a#) (I# len#)
120 end# = addr2Int# (a# `plusAddr#` len#)
123 | addr2Int# p# >=# end# = return []
125 case utf8DecodeChar# p# of
130 countUTF8Chars :: Ptr Word8 -> Int -> IO Int
131 countUTF8Chars ptr bytes = go ptr 0
133 end = ptr `plusPtr` bytes
137 | ptr >= end = return n
139 case utf8DecodeChar# (unPtr ptr) of
140 (# _, a #) -> go (Ptr a) (n+1)
142 unPtr :: Ptr a -> Addr#
145 utf8EncodeChar :: Char -> Ptr Word8 -> IO (Ptr Word8)
146 utf8EncodeChar c ptr =
149 _ | x > 0 && x <= 0x007f -> do
150 poke ptr (fromIntegral x)
151 return (ptr `plusPtr` 1)
152 -- NB. '\0' is encoded as '\xC0\x80', not '\0'. This is so that we
153 -- can have 0-terminated UTF-8 strings (see GHC.Base.unpackCStringUtf8).
155 poke ptr (fromIntegral (0xC0 .|. ((x `shiftR` 6) .&. 0x1F)))
156 pokeElemOff ptr 1 (fromIntegral (0x80 .|. (x .&. 0x3F)))
157 return (ptr `plusPtr` 2)
159 poke ptr (fromIntegral (0xE0 .|. (x `shiftR` 12) .&. 0x0F))
160 pokeElemOff ptr 1 (fromIntegral (0x80 .|. (x `shiftR` 6) .&. 0x3F))
161 pokeElemOff ptr 2 (fromIntegral (0x80 .|. (x .&. 0x3F)))
162 return (ptr `plusPtr` 3)
164 poke ptr (fromIntegral (0xF0 .|. (x `shiftR` 18)))
165 pokeElemOff ptr 1 (fromIntegral (0x80 .|. ((x `shiftR` 12) .&. 0x3F)))
166 pokeElemOff ptr 2 (fromIntegral (0x80 .|. ((x `shiftR` 6) .&. 0x3F)))
167 pokeElemOff ptr 3 (fromIntegral (0x80 .|. (x .&. 0x3F)))
168 return (ptr `plusPtr` 4)
170 utf8EncodeString :: Ptr Word8 -> String -> IO ()
171 utf8EncodeString ptr str = go ptr str
175 ptr' <- utf8EncodeChar c ptr
178 utf8EncodedLength :: String -> Int
179 utf8EncodedLength str = go 0 str
183 | ord c > 0 && ord c <= 0x007f = go (n+1) cs
184 | ord c <= 0x07ff = go (n+2) cs
185 | ord c <= 0xffff = go (n+3) cs
186 | otherwise = go (n+4) cs
188 -- -----------------------------------------------------------------------------
192 This is the main name-encoding and decoding function. It encodes any
193 string into a string that is acceptable as a C name. This is done
194 right before we emit a symbol name into the compiled C or asm code.
195 Z-encoding of strings is cached in the FastString interface, so we
196 never encode the same string more than once.
198 The basic encoding scheme is this.
200 * Tuples (,,,) are coded as Z3T
202 * Alphabetic characters (upper and lower) and digits
203 all translate to themselves;
204 except 'Z', which translates to 'ZZ'
205 and 'z', which translates to 'zz'
206 We need both so that we can preserve the variable/tycon distinction
208 * Most other printable characters translate to 'zx' or 'Zx' for some
209 alphabetic character x
211 * The others translate as 'znnnU' where 'nnn' is the decimal number
215 --------------------------
227 (# #) Z1H unboxed 1-tuple (note the space)
228 (#,,,,#) Z5H unboxed 5-tuple
229 (NB: There is no Z1T nor Z0H.)
232 type UserString = String -- As the user typed it
233 type EncodedString = String -- Encoded form
236 zEncodeString :: UserString -> EncodedString
237 zEncodeString cs = case maybe_tuple cs of
238 Just n -> n -- Tuples go to Z2T etc
242 go (c:cs) = encode_ch c ++ go cs
244 unencodedChar :: Char -> Bool -- True for chars that don't need encoding
245 unencodedChar 'Z' = False
246 unencodedChar 'z' = False
247 unencodedChar c = c >= 'a' && c <= 'z'
248 || c >= 'A' && c <= 'Z'
249 || c >= '0' && c <= '9'
251 encode_ch :: Char -> EncodedString
252 encode_ch c | unencodedChar c = [c] -- Common case first
255 encode_ch '(' = "ZL" -- Needed for things like (,), and (->)
256 encode_ch ')' = "ZR" -- For symmetry with (
276 encode_ch '\'' = "zq"
277 encode_ch '\\' = "zr"
282 encode_ch c = 'z' : if isDigit (head hex_str) then hex_str
284 where hex_str = showHex (ord c) "U"
285 -- ToDo: we could improve the encoding here in various ways.
286 -- eg. strings of unicode characters come out as 'z1234Uz5678U', we
287 -- could remove the 'U' in the middle (the 'z' works as a separator).
289 showHex = showIntAtBase 16 intToDigit
290 -- needed because prior to GHC 6.2, Numeric.showHex added a "0x" prefix
292 zDecodeString :: EncodedString -> UserString
293 zDecodeString [] = []
294 zDecodeString ('Z' : d : rest)
295 | isDigit d = decode_tuple d rest
296 | otherwise = decode_upper d : zDecodeString rest
297 zDecodeString ('z' : d : rest)
298 | isDigit d = decode_num_esc d rest
299 | otherwise = decode_lower d : zDecodeString rest
300 zDecodeString (c : rest) = c : zDecodeString rest
302 decode_upper, decode_lower :: Char -> Char
304 decode_upper 'L' = '('
305 decode_upper 'R' = ')'
306 decode_upper 'M' = '['
307 decode_upper 'N' = ']'
308 decode_upper 'C' = ':'
309 decode_upper 'Z' = 'Z'
310 decode_upper ch = {-pprTrace "decode_upper" (char ch)-} ch
312 decode_lower 'z' = 'z'
313 decode_lower 'a' = '&'
314 decode_lower 'b' = '|'
315 decode_lower 'c' = '^'
316 decode_lower 'd' = '$'
317 decode_lower 'e' = '='
318 decode_lower 'g' = '>'
319 decode_lower 'h' = '#'
320 decode_lower 'i' = '.'
321 decode_lower 'l' = '<'
322 decode_lower 'm' = '-'
323 decode_lower 'n' = '!'
324 decode_lower 'p' = '+'
325 decode_lower 'q' = '\''
326 decode_lower 'r' = '\\'
327 decode_lower 's' = '/'
328 decode_lower 't' = '*'
329 decode_lower 'u' = '_'
330 decode_lower 'v' = '%'
331 decode_lower ch = {-pprTrace "decode_lower" (char ch)-} ch
333 -- Characters not having a specific code are coded as z224U (in hex)
334 decode_num_esc :: Char -> EncodedString -> UserString
335 decode_num_esc d rest
336 = go (digitToInt d) rest
338 go n (c : rest) | isHexDigit c = go (16*n + digitToInt c) rest
339 go n ('U' : rest) = chr n : zDecodeString rest
340 go n other = error ("decode_num_esc: " ++ show n ++ ' ':other)
342 decode_tuple :: Char -> EncodedString -> UserString
344 = go (digitToInt d) rest
346 -- NB. recurse back to zDecodeString after decoding the tuple, because
347 -- the tuple might be embedded in a longer name.
348 go n (c : rest) | isDigit c = go (10*n + digitToInt c) rest
349 go 0 ('T':rest) = "()" ++ zDecodeString rest
350 go n ('T':rest) = '(' : replicate (n-1) ',' ++ ")" ++ zDecodeString rest
351 go 1 ('H':rest) = "(# #)" ++ zDecodeString rest
352 go n ('H':rest) = '(' : '#' : replicate (n-1) ',' ++ "#)" ++ zDecodeString rest
353 go n other = error ("decode_tuple: " ++ show n ++ ' ':other)
356 Tuples are encoded as
358 for 3-tuples or unboxed 3-tuples respectively. No other encoding starts
361 * "(# #)" is the tycon for an unboxed 1-tuple (not 0-tuple)
362 There are no unboxed 0-tuples.
364 * "()" is the tycon for a boxed 0-tuple.
365 There are no boxed 1-tuples.
368 maybe_tuple :: UserString -> Maybe EncodedString
370 maybe_tuple "(# #)" = Just("Z1H")
371 maybe_tuple ('(' : '#' : cs) = case count_commas (0::Int) cs of
372 (n, '#' : ')' : _) -> Just ('Z' : shows (n+1) "H")
374 maybe_tuple "()" = Just("Z0T")
375 maybe_tuple ('(' : cs) = case count_commas (0::Int) cs of
376 (n, ')' : _) -> Just ('Z' : shows (n+1) "T")
378 maybe_tuple _ = Nothing
380 count_commas :: Int -> String -> (Int, String)
381 count_commas n (',' : cs) = count_commas (n+1) cs
382 count_commas n cs = (n,cs)