@@ -12,6 +12,7 @@ import Util.System
13import Numeric
13import Numeric
14import Data.Char
14import Data.Char
15import Data.List (intercalate)
16import Data.List (intercalate)
16import qualified Data.Vector.Unboxed as V
17import qualified Data.Vector.Unboxed as V
17import System.Process
18import System.Process
@@ -145,8 +146,26 @@ showCStr s = '"' : foldr ((++) . showChar) "\"" s
145 | ord c < 0x10 = "\"\"\\x0" ++ showHex (ord c) "\"\""
146 | ord c < 0x10 = "\"\"\\x0" ++ showHex (ord c) "\"\""
146 | ord c < 0x20 = "\"\"\\x" ++ showHex (ord c) "\"\""
147 | ord c < 0x20 = "\"\"\\x" ++ showHex (ord c) "\"\""
147 | ord c < 0x7f = [c] -- 0x7f = \DEL
148 | ord c < 0x7f = [c] -- 0x7f = \DEL
148− | ord c < 0x100 = "\"\"\\x" ++ showHex (ord c) "\"\""
149+ | otherwise = showHexes (utf8bytes (ord c))
149− | otherwise = error $ "non-8-bit character in string literal: " ++ show c
150+
151+ utf8bytes :: Int -> [Int]
152+ utf8bytes x = let (h : bytes) = split [] x in
153+ headHex h (length bytes) : map toHex bytes
156+ split acc x = let xbits = x .&. 0x3f
157+ xrest = shiftR x 6 in
158+ split (xbits : acc) xrest
160+ headHex h 1 = h + 0xc0
161+ headHex h 2 = h + 0xe0
162+ headHex h 3 = h + 0xf0
163+ headHex h n = error "Can't happen: Invalid UTF8 character"
167+ showHexes = foldr ((++) . showUTF8) ""
168+ showUTF8 c = "\"\"\\x" ++ showHex c "\"\""
151bcc :: Int -> BC -> String
170bcc :: Int -> BC -> String
152bcc i (ASSIGN l r) = indent i ++ creg l ++ " = " ++ creg r ++ ";\n"
171bcc i (ASSIGN l r) = indent i ++ creg l ++ " = " ++ creg r ++ ";\n"
@@ -424,11 +443,6 @@ doOp v (LSLe (ATInt ITBig)) [l, r] = v ++ "idris_bigLe(vm, " ++ creg l ++ ", " +
424doOp v (LSGt (ATInt ITBig)) [l, r] = v ++ "idris_bigGt(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
443doOp v (LSGt (ATInt ITBig)) [l, r] = v ++ "idris_bigGt(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
425doOp v (LSGe (ATInt ITBig)) [l, r] = v ++ "idris_bigGe(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
444doOp v (LSGe (ATInt ITBig)) [l, r] = v ++ "idris_bigGe(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
427−doOp v LStrConcat [l,r] = v ++ "idris_concat(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
428−doOp v LStrLt [l,r] = v ++ "idris_strlt(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
429−doOp v LStrEq [l,r] = v ++ "idris_streq(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
430−doOp v LStrLen [x] = v ++ "idris_strlen(vm, " ++ creg x ++ ")"
432doOp v (LIntFloat ITNative) [x] = v ++ "idris_castIntFloat(" ++ creg x ++ ")"
446doOp v (LIntFloat ITNative) [x] = v ++ "idris_castIntFloat(" ++ creg x ++ ")"
433doOp v (LFloatInt ITNative) [x] = v ++ "idris_castFloatInt(" ++ creg x ++ ")"
447doOp v (LFloatInt ITNative) [x] = v ++ "idris_castFloatInt(" ++ creg x ++ ")"
434doOp v (LSExt ITNative ITBig) [x] = v ++ "idris_castIntBig(vm, " ++ creg x ++ ")"
448doOp v (LSExt ITNative ITBig) [x] = v ++ "idris_castIntBig(vm, " ++ creg x ++ ")"
@@ -529,11 +543,18 @@ doOp v LFFloor [x] = v ++ flUnOp "floor" (creg x)
529doOp v LFCeil [x] = v ++ flUnOp "ceil" (creg x)
543doOp v LFCeil [x] = v ++ flUnOp "ceil" (creg x)
530doOp v LFNegate [x] = v ++ "MKFLOAT(vm, -GETFLOAT(" ++ (creg x) ++ "))"
544doOp v LFNegate [x] = v ++ "MKFLOAT(vm, -GETFLOAT(" ++ (creg x) ++ "))"
546+-- String functions which don't need to know we're UTF8
547+doOp v LStrConcat [l,r] = v ++ "idris_concat(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
548+doOp v LStrLt [l,r] = v ++ "idris_strlt(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
549+doOp v LStrEq [l,r] = v ++ "idris_streq(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
551+-- String functions which need to know we're UTF8
532doOp v LStrHead [x] = v ++ "idris_strHead(vm, " ++ creg x ++ ")"
552doOp v LStrHead [x] = v ++ "idris_strHead(vm, " ++ creg x ++ ")"
533doOp v LStrTail [x] = v ++ "idris_strTail(vm, " ++ creg x ++ ")"
553doOp v LStrTail [x] = v ++ "idris_strTail(vm, " ++ creg x ++ ")"
534doOp v LStrCons [x, y] = v ++ "idris_strCons(vm, " ++ creg x ++ "," ++ creg y ++ ")"
554doOp v LStrCons [x, y] = v ++ "idris_strCons(vm, " ++ creg x ++ "," ++ creg y ++ ")"
535doOp v LStrIndex [x, y] = v ++ "idris_strIndex(vm, " ++ creg x ++ "," ++ creg y ++ ")"
555doOp v LStrIndex [x, y] = v ++ "idris_strIndex(vm, " ++ creg x ++ "," ++ creg y ++ ")"
536doOp v LStrRev [x] = v ++ "idris_strRev(vm, " ++ creg x ++ ")"
556doOp v LStrRev [x] = v ++ "idris_strRev(vm, " ++ creg x ++ ")"
557+doOp v LStrLen [x] = v ++ "idris_strlen(vm, " ++ creg x ++ ")"
538doOp v LAllocate [x] = v ++ "idris_buffer_allocate(vm, " ++ creg x ++ ")"
559doOp v LAllocate [x] = v ++ "idris_buffer_allocate(vm, " ++ creg x ++ ")"
539doOp v LAppendBuffer [a, b, c, d, e, f] = v ++ "idris_appendBuffer(vm, " ++ creg a ++ "," ++ creg b ++ "," ++ creg c ++ "," ++ creg d ++ "," ++ creg e ++ "," ++ creg f ++ ")"
560doOp v LAppendBuffer [a, b, c, d, e, f] = v ++ "idris_appendBuffer(vm, " ++ creg a ++ "," ++ creg b ++ "," ++ creg c ++ "," ++ creg d ++ "," ++ creg e ++ "," ++ creg f ++ ")"