Interpreter.icl 17.5 KB
Newer Older
1 2 3
implementation module ABC.Interpreter

import StdArray
4
import StdBool
5
import StdClass
6 7
import StdFile
import StdInt
8
import StdList
Camil Staps's avatar
Camil Staps committed
9
import StdMaybe
10
import StdMisc
11
import StdOrdList
12 13 14 15 16 17 18

import graph_copy_with_names
import symbols_in_program

import ABC.Interpreter.Internal
import ABC.Interpreter.Util

19 20 21 22 23 24
defaultDeserializationSettings :: DeserializationSettings
defaultDeserializationSettings =
	{ heap_size  = 2 << 20
	, stack_size = (512 << 10) * 2
	}

25 26
:: *SerializedGraph =
	{ graph    :: !*String
27 28
	, descinfo :: !{#DescInfo}
	, modules  :: !{#String}
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
	, bytecode :: !String
	}

// The arguments are:
// - Pointer to C function;
// - Argument for function (in our case, pointer to the interpret node)
// - Pointer to rest of the finalizers (dealt with in the RTS)
:: Finalizer = Finalizer !Int !Int !Int
:: InterpretedExpression :== Finalizer
:: *InterpretationEnvironment = E.a:
	{ ie_finalizer :: !Finalizer
	, ie_snode_ptr :: !Int
	, ie_snodes    :: !*{a}
	}

44 45
serialize :: a !String !*World -> *(!Maybe SerializedGraph, !*World)
serialize graph bcfile w
46
# (graph,descinfo,modules) = copy_to_string_with_names graph
47 48

# (bytecode,w) = readFile bcfile w
49 50
| isNothing bytecode = (Nothing, w)
# bytecode = fromJust bytecode
51

52
#! (len,bytecodep) = strip_bytecode bytecode {#symbol_name di modules \\ di <-: descinfo}
Camil Staps's avatar
Camil Staps committed
53
#! bytecode = derefCharArray bytecodep len
54
| free_to_false bytecodep = (Nothing, w)
Camil Staps's avatar
Camil Staps committed
55

56 57
# rec =
	{ graph    = graph
58 59
	, descinfo = descinfo
	, modules  = modules
60 61
	, bytecode = bytecode
	}
62
= (Just rec, w)
Camil Staps's avatar
Camil Staps committed
63 64 65 66 67 68
where
	symbol_name :: !DescInfo !{#String} -> String
	symbol_name {di_prefix_arity_and_mod,di_name} mod_a
	# prefix_n = di_prefix_arity_and_mod bitand 0xff
	# module_n = (di_prefix_arity_and_mod >> 8)-1
	# module_name = mod_a.[module_n]
69
	= make_symbol_name module_name di_name (min prefix_n PREFIX_D) +++ "\0"
Camil Staps's avatar
Camil Staps committed
70 71 72
	where
		PREFIX_D = 4

Camil Staps's avatar
Camil Staps committed
73
	strip_bytecode :: !String !{#String} -> (!Int, !Pointer)
Camil Staps's avatar
Camil Staps committed
74 75 76
	strip_bytecode bytecode descriptors = code {
		ccall strip_bytecode "sA:VIp"
	}
77

Camil Staps's avatar
Camil Staps committed
78 79
serialize_for_prelinked_interpretation :: a !String !String !*World -> *(!Maybe String, !*World)
serialize_for_prelinked_interpretation graph bcfile thisexe w
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
# (host_syms,w) = accFiles (read_symbols thisexe) w

# (graph,descinfo,modules) = copy_to_string_with_names graph

# (bytecode,w) = readFile bcfile w
| isNothing bytecode = (Nothing, w)
# bytecode = fromJust bytecode

# pgm = parse host_syms bytecode
| isNothing pgm = (Nothing, w)
# pgm = fromJust pgm
# code_start = get_code pgm

# int_syms = {#s \\ s <- getInterpreterSymbols pgm}
# int_syms = {#predef_or_lookup_symbol code_start d modules int_syms \\ d <-: descinfo}

# graph = replace_desc_numbers_by_descs 0 graph int_syms 0 code_start // relocate relative to beginning of code segment
= (Just graph, w)
where
	get_code :: !Pointer -> Pointer
	get_code pgm = code {
		ccall get_code "p:p"
	}

	predef_or_lookup_symbol :: !Int !DescInfo !{#String} !{#Symbol} -> Int
	predef_or_lookup_symbol code_start di mods syms = case di.di_name of
		"_ARRAY_"  -> code_start-1*8+2
		"_STRING_" -> code_start-2*8+2
		"BOOL"     -> code_start-3*8+2
		"CHAR"     -> code_start-4*8+2
		"REAL"     -> code_start-5*8+2
		"INT"      -> code_start-6*8+2
		"dINT"     -> code_start-6*8+2
		_          -> lookup_symbol_value di mods syms

	// This is like the function with the same name in GraphCopy's
	// graph_copy_with_names, but it assigns even negative descriptor numbers
	// to predefined symbols so that it matches predef_or_lookup_symbol above.
	replace_desc_numbers_by_descs :: !Int !*{#Char} !{#Int} !Int !Int -> *{#Char}
	replace_desc_numbers_by_descs i s symbol_a symbol_offset array_desc
	| i>=size s
		| i==size s = s
		| otherwise = abort "error in replace_desc_numbers_by_descs\n"
	#! desc=get_word_from_string s i
	| desc<0
		= replace_desc_numbers_by_descs (i+IF_INT_64_OR_32 8 4) s symbol_a symbol_offset array_desc
	# desc = symbol_a.[desc-1]
	# desc=desc+symbol_offset
	# s=store_int_in_string s i (desc-array_desc)
	| desc bitand 2==0
		# d = get_thunk_n_non_pointers desc
		= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 8 4)+(d<<(IF_INT_64_OR_32 3 2))) s symbol_a symbol_offset array_desc
	# (d,not_array) = get_descriptor_n_non_pointers_and_not_array desc
	| not_array
		= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 8 4)+(d<<(IF_INT_64_OR_32 3 2))) s symbol_a symbol_offset array_desc
	| d==0 // _STRING_
		#! l = get_word_from_string s (i+IF_INT_64_OR_32 8 4)
		# l = IF_INT_64_OR_32 ((l+7) bitand -8) ((l+3) bitand -4)
		= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 16 8)+l) s symbol_a symbol_offset array_desc
	| d==1 // _ARRAY_
		#! d = get_word_from_string s (i+IF_INT_64_OR_32 16 8)
		| d==0
			= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 24 12)) s symbol_a symbol_offset array_desc
		# d = symbol_a.[d-1]
		# d = d+symbol_offset
		# s=store_int_in_string s (i+IF_INT_64_OR_32 16 8) (d-array_desc)
		#! l = get_word_from_string s (i+IF_INT_64_OR_32 8 4)
Camil Staps's avatar
Camil Staps committed
147
		| d==array_desc-5*8+2 // REAL
148 149
			# l = l << IF_INT_64_OR_32 3 2
			= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 24 12)+l) s symbol_a symbol_offset array_desc
Camil Staps's avatar
Camil Staps committed
150
		| d==array_desc-6*8+2 // INT
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
			# l = l << 3
			= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 24 12)+l) s symbol_a symbol_offset array_desc
		| d==array_desc-3*8+2 // BOOL
			# l = IF_INT_64_OR_32 ((l+7) bitand -8) ((l+3) bitand -4)
			= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 24 12)+l) s symbol_a symbol_offset array_desc
		# arity = get_D_node_arity d
		| arity>=256
			# record_a_arity = get_D_record_a_arity d
			# record_b_arity = arity-256-record_a_arity
			# l = (l * record_b_arity) << IF_INT_64_OR_32 3 2
			= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 24 12)+l) s symbol_a symbol_offset array_desc
		= abort (toString l+++" "+++toString d)
	where
		get_word_from_string :: !{#Char} !Int -> Int // get_D_from_string_64
		get_word_from_string s i = code inline {
			push_a_b 0
			pop_a 1
			addI
			load_i 16
		}

		store_int_in_string :: !*{#Char} !Int !Int -> *{#Char} // 64-bit variant
		store_int_in_string s i n =
			{s & [i]=toChar n,[i+1]=toChar (n>>8),[i+2]=toChar (n>>16),[i+3]=toChar (n>>24),
				 [i+4]=toChar (n >> 32),[i+5]=toChar (n>>40),[i+6]=toChar (n>>48),[i+7]=toChar (n>>56)}

		get_thunk_n_non_pointers:: !Int -> Int
		get_thunk_n_non_pointers d
		# arity = get_thunk_arity d
		| arity<256
			= 0
			# b_size = arity>>8
			= b_size
		where
			get_thunk_arity :: !Int -> Int // 64-bit version
			get_thunk_arity a = code {
				load_si32 -4
			}

		get_descriptor_n_non_pointers_and_not_array :: !Int -> (!Int,!Bool)
		get_descriptor_n_non_pointers_and_not_array d
		| d<array_desc
			| d==array_desc-1*8+2 = (1,False) // _ARRAY_
			| d==array_desc-2*8+2 = (0,False) // _STRING_
			| d==array_desc-3*8+2 = (1,True)  // BOOL
			| d==array_desc-4*8+2 = (1,True)  // CHAR
Camil Staps's avatar
Camil Staps committed
197
			| d==array_desc-5*8+2 = (IF_INT_64_OR_32 1 2,True) // REAL
198
			| d==array_desc-6*8+2 = (1,True)  // INT/dINT
Camil Staps's avatar
Camil Staps committed
199
			| otherwise = abort "internal error in serialize_for_prelinked_interpretation\n"
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
		# arity = get_D_node_arity d
		| arity<256 = (0,True)
		# record_a_arity = get_D_record_a_arity d
		# record_b_arity = arity-256-record_a_arity
		= (record_b_arity,True)

		get_D_node_arity :: !Int -> Int
		get_D_node_arity d = code inline {
			load_si16 -2
		}

		get_D_record_a_arity :: !Int -> Int
		get_D_record_a_arity d = code inline {
			load_si16 0
		}

216
deserialize :: !DeserializationSettings !SerializedGraph !String !*World -> *(!Maybe a, !*World)
217 218
deserialize dsets graph thisexe w = deserialize` False dsets graph thisexe w

219 220
deserialize_strict :: !DeserializationSettings !SerializedGraph !String !*World -> *(!DeserializedValue a, !*World)
deserialize_strict dsets graph thisexe w = case deserialize` True dsets graph thisexe w of
221 222 223 224 225
	(Nothing,w) -> (DV_ParseError,w)
	(Just v,w)  -> (v,w)

deserialize` :: !Bool !DeserializationSettings !SerializedGraph !String !*World -> *(Maybe a, !*World)
deserialize` strict dsets {graph,descinfo,modules,bytecode} thisexe w
226 227
# (host_syms,w) = accFiles (read_symbols thisexe) w

228
# pgm = parse host_syms bytecode
229
| isNothing pgm = (Nothing, w)
230
# pgm = fromJust pgm
231
# int_syms = {#s \\ s <- getInterpreterSymbols pgm}
232
# int_syms = {#lookup_symbol_value d modules int_syms \\ d <-: descinfo}
233

234
# stack = malloc (IF_INT_64_OR_32 8 4 * dsets.stack_size)
235
# asp = stack
236 237 238
# bsp = stack + IF_INT_64_OR_32 8 4 * (dsets.stack_size-1)
# csp = stack + IF_INT_64_OR_32 4 2 * dsets.stack_size
# heap = malloc (IF_INT_64_OR_32 8 4 * (dsets.heap_size+4))
239 240
# ie_settings = build_interpretation_environment
	pgm
241
	heap dsets.heap_size stack dsets.stack_size
242
	asp bsp csp heap
243
	strict
244 245
# graph = replace_desc_numbers_by_descs 0 graph int_syms 0 0
# graph_node = string_to_interpreter graph ie_settings
246
#! (ie,_) = make_finalizer ie_settings
Camil Staps's avatar
Camil Staps committed
247
# ie = {ie_finalizer=ie, ie_snode_ptr=0, ie_snodes=create_array_ 1}
248
= (Just (interpret ie (Finalizer 0 0 (graph_node + if strict 2 0))), w)
249
where
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
	string_to_interpreter :: !String !Pointer -> Pointer
	string_to_interpreter graph ie = code {
		ccall string_to_interpreter "Sp:p"
	}

getInterpreterSymbols :: !Pointer -> [Symbol]
getInterpreterSymbols pgm = takeWhile (\s -> size s.symbol_name <> 0)
	[getSymbol i \\ i <- [0..get_symbol_table_size pgm-1]]
where
	symbol_table = get_symbol_table pgm

	getSymbol :: !Int -> Symbol
	getSymbol i
	#! offset = symbol_table + i * IF_INT_64_OR_32 16 8 /* size of struct host_symbol */
	#! loc = derefInt offset
	#! name = derefString (derefInt (offset + IF_INT_64_OR_32 8 4))
	= {symbol_name=name, symbol_value=loc}

	get_symbol_table_size :: !Pointer -> Int
	get_symbol_table_size pgm = code {
		ccall get_symbol_table_size "p:I"
271
	}
272

273 274 275 276 277 278 279 280 281 282 283 284 285 286
	get_symbol_table :: !Pointer -> Pointer
	get_symbol_table pgm = code {
		ccall get_symbol_table "p:p"
	}

lookup_symbol_value :: !DescInfo !{#String} !{#Symbol} -> Int
lookup_symbol_value {di_prefix_arity_and_mod,di_name} mod_a symbols
	# prefix_n = di_prefix_arity_and_mod bitand 0xff
	# module_n = (di_prefix_arity_and_mod >> 8)-1
	# module_name = mod_a.[module_n]
	| prefix_n<PREFIX_D
		# symbol_name = make_symbol_name module_name di_name prefix_n
		# symbol_value = get_symbol_value symbol_name symbols
		| prefix_n<=1
287
			| symbol_value== -1
288
				= abort ("lookup_desc_info not found "+++symbol_name+++"\n")
289 290 291 292 293 294 295 296 297 298 299 300
				= symbol_value
			| symbol_value== -1
				= abort ("lookup_desc_info not found "+++symbol_name+++"\n")
				= symbol_value+2
		# symbol_name = make_symbol_name module_name di_name PREFIX_D
		# symbol_value = get_symbol_value symbol_name symbols
		| symbol_value== -1
			= abort ("lookup_desc_info not found "+++symbol_name+++"\n")
			# arity = prefix_n - PREFIX_D
			= symbol_value+(arity*8*2)+2
where
	PREFIX_D = 4
301

302
get_start_rule_as_expression :: !DeserializationSettings !String !String !*World -> *(Maybe a, !*World)
303
get_start_rule_as_expression dsets filename prog w
304 305
# (syms,w) = accFiles (read_symbols prog) w
# (bc,w) = readFile filename w
306 307
| isNothing bc = (Nothing, w)
# bc = fromJust bc
308
# pgm = parse syms bc
309
| isNothing pgm = (Nothing, w)
310
# pgm = fromJust pgm
311
# stack = malloc (IF_INT_64_OR_32 8 4 * dsets.stack_size)
312
# asp = stack
313 314 315
# bsp = stack + IF_INT_64_OR_32 8 4 * (dsets.stack_size-1)
# csp = stack + IF_INT_64_OR_32 4 2 * dsets.stack_size
# heap = malloc (IF_INT_64_OR_32 8 4 * dsets.heap_size)
316 317
# ie_settings = build_interpretation_environment
	pgm
318
	heap dsets.heap_size stack dsets.stack_size
319
	asp bsp csp heap
320
	False
321 322
# start_node = build_start_node ie_settings
#! (ie,_) = make_finalizer ie_settings
Camil Staps's avatar
Camil Staps committed
323
# ie = {ie_finalizer=ie, ie_snode_ptr=0, ie_snodes=create_array_ 1}
324
= (Just (interpret ie (Finalizer 0 0 start_node)), w)
325 326 327 328 329
	// Obviously, this is not a "valid" finalizer in the sense that it can be
	// called from the garbage collector. But that's okay, because we don't add
	// it to the finalizer_list anyway. This is just to ensure that the first
	// call to interpret gets the right argument.

330 331 332
build_interpretation_environment :: !Pointer !Pointer !Int !Pointer !Int !Pointer !Pointer !Pointer !Pointer !Bool -> Pointer
build_interpretation_environment pgm heap hsize stack ssize asp bsp csp hp strict = code {
	ccall build_interpretation_environment "ppIpIppppI:p"
333
}
334

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
build_start_node :: !Pointer -> Pointer
build_start_node ie = code {
	ccall build_start_node "p:p"
}

make_finalizer :: !Int -> (!.Finalizer,!Int)
make_finalizer ie_settings = code {
	push_finalizers
	ccall get_interpretation_environment_finalizer ":p"
	push_a_b 0
	pop_a 1
	build_r e__system_kFinalizer 0 3 0 0
	pop_b 3
	set_finalizers
	pushI 0
}
351

352 353
graph_to_string :: !*SerializedGraph -> *(!.String, !*SerializedGraph)
graph_to_string g=:{graph,descinfo,modules,bytecode}
354
# (graph_cpy,graph,graph_size) = copy graph
Camil Staps's avatar
Camil Staps committed
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
# g & graph = graph
# string_size = sum
	[ 4 + graph_size
	, 4 + sum [4 + size di.di_name + 1 \\ di <-: descinfo]
	, 4 + sum [size mod + 1 \\ mod <-: modules]
	, 4 + size bytecode
	]
# s = createArray string_size '\0'
# (i,s) = writeInt graph_size 0 s
# (i,s) = writeString {#c \\ c <- graph_cpy} i s
# (i,s) = writeInt (size descinfo) i s
# (i,s) = writeArray writeDescInfo 0 descinfo i s
# (i,s) = writeInt (size modules) i s
# (i,s) = writeArray writeTerminatedString 0 modules i s
# (i,s) = writeInt (size bytecode) i s
# (i,s) = writeString bytecode i s
| i <> string_size = abort "internal error in graphToString\n"
= (s,g)
373 374 375 376 377 378 379 380 381 382 383 384 385
where
	copy :: !*(b a) -> *(![a], !*b a, !Int) | Array b a
	copy arr
	# (s,arr) = usize arr
	# (cpy,arr) = copy (s-1) arr []
	= (cpy,arr,s)
	where
		copy :: !Int !*(b a) ![a] -> *(![a], !*b a) | Array b a
		copy -1 arr cpy = (cpy, arr)
		copy i  arr cpy
		# (x,arr) = arr![i]
		= copy (i-1) arr [x:cpy]

Camil Staps's avatar
Camil Staps committed
386 387
	writeInt :: !Int !Int !*String -> (!Int, !.String)
	writeInt n i s = (i+4, {s & [i]=toChar n, [i+1]=toChar (n>>8), [i+2]=toChar (n>>16), [i+3]=toChar (n>>24)})
388

Camil Staps's avatar
Camil Staps committed
389 390
	writeString :: !String !Int !*String -> (!Int, !.String)
	writeString src i s = (i+size src, {s & [j]=c \\ j <- [i..] & c <-: src})
391

Camil Staps's avatar
Camil Staps committed
392 393 394 395
	writeTerminatedString :: !String !Int !*String -> (!Int, !.String)
	writeTerminatedString src i s
	# (i,s) = writeString src i s
	= (i+1, {s & [i]='\0'})
396

Camil Staps's avatar
Camil Staps committed
397 398 399 400
	writeDescInfo :: !DescInfo !Int !*String -> (!Int, !.String)
	writeDescInfo di i s
	# (i,s) = writeInt di.di_prefix_arity_and_mod i s
	= writeTerminatedString di.di_name i s
401

Camil Staps's avatar
Camil Staps committed
402 403 404 405 406
	writeArray :: !(a Int *String -> (Int, *String)) !Int !{#a} !Int !*String -> (!Int, !.String) | Array {#} a
	writeArray write n arr i s
	| n >= size arr = (i,s)
	# (i,s) = write arr.[n] i s
	= writeArray write (n+1) arr i s
407

408 409
graph_from_string :: !String -> Maybe *SerializedGraph
graph_from_string s = read 0 s
410
where
Camil Staps's avatar
Camil Staps committed
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
	read :: !Int !String -> Maybe *SerializedGraph
	read i s
	# (i,graph_size) = readInt i s
	| i < 0 = Nothing
	# (i,graph) = readString i graph_size s
	| i < 0 = Nothing
	# graph = {c \\ c <-: graph}

	# (i,descinfo_size) = readInt i s
	| i < 0 = Nothing
	# (i,descinfo) = readArray readDescInfo descinfo_size i s
	| i < 0 = Nothing
	# descinfo = {di \\ di <- descinfo}

	# (i,modules_size) = readInt i s
	| i < 0 = Nothing
	# (i,modules) = readArray readTerminatedString modules_size i s
	| i < 0 = Nothing
	# modules = {mod \\ mod <- modules}

	# (i,bytecode_size) = readInt i s
	| i < 0 = Nothing
	# (i,bytecode) = readString i bytecode_size s
	| i < 0 = Nothing

	| i <> size s = Nothing
	= Just {graph=graph,descinfo=descinfo,modules=modules,bytecode=bytecode}

	readInt :: !Int !String -> (!Int, !Int)
	readInt i s
	| i >= size s-4 = (-1,0)
	| otherwise = (i+4, sum [toInt s.[i+j] << (8*j) \\ j <- [0,1,2,3]])

	readString :: !Int !Int !String -> (!Int, !String)
	readString i len s
	| i > size s - len = (-1,"")
	| otherwise = (i+len, s % (i,i+len-1))

	readTerminatedString :: !Int !String -> (!Int, !String)
	readTerminatedString i s
	# len = findNull i s - i
	| len < 0 = (-1, "")
	# (i,s`) = readString i len s
	| i < 0 = (i,s`)
	= (i+1,s`)
	where
		findNull :: !Int !String -> Int
		findNull i s
		| i >= size s   = -1
		| s.[i] == '\0' = i
		| otherwise     = findNull (i+1) s

	readDescInfo :: !Int !String -> (!Int, !DescInfo)
	readDescInfo i s
	| i >= size s = (-1, {di_prefix_arity_and_mod=0, di_name=""})
	# (i,arity) = readInt i s
	| i < 0 = (-1, {di_prefix_arity_and_mod=0, di_name=""})
	# (i,name) = readTerminatedString i s
	| i < 0 = (-1, {di_prefix_arity_and_mod=0, di_name=""})
	= (i, {di_prefix_arity_and_mod=arity, di_name=name})

	readArray :: !(Int String -> (Int, a)) !Int !Int !String -> (!Int, ![a])
	readArray read len i s
	| len <= 0 = (i,[])
	# (i,x) = read i s
	| i < 0 = (i,[])
	# (i,xs) = readArray read (len-1) i s
	= (i,[x:xs])

480 481 482
graph_to_file :: !*SerializedGraph !*File -> *(!*SerializedGraph, !*File)
graph_to_file g f
# (s,g) = graph_to_string g
Camil Staps's avatar
Camil Staps committed
483 484 485
# f = f <<< size s <<< s
= (g,f)

486 487
graph_from_file :: !*File -> *(!Maybe *SerializedGraph, !*File)
graph_from_file f
Camil Staps's avatar
Camil Staps committed
488 489
# (_,size,f) = freadi f
# (s,f) = freads f size
490
# g = graph_from_string s
Camil Staps's avatar
Camil Staps committed
491
= (g,f)
Camil Staps's avatar
Camil Staps committed
492 493 494 495 496

malloc :: !Int -> Pointer
malloc _ = code {
	ccall malloc "I:p"
}
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544

readFile :: !String !*World -> (!Maybe String, !*World)
readFile fname w
# (ok,f,w) = fopen fname FReadData w
| not ok = (Nothing, w)
# (ok,f) = fseek f 0 FSeekEnd
| not ok
	# (_,w) = fclose f w
	= (Nothing, w)
# (size,f) = fposition f
# (ok,f) = fseek f 0 FSeekSet
| not ok
	# (_,w) = fclose f w
	= (Nothing, w)
# (s,f) = freads f size
# (_,w) = fclose f w
= (Just s,w)

:: Pointer :== Int

derefInt :: !Pointer -> Int
derefInt ptr = code {
	load_i 0
}

derefChar :: !Pointer -> Char
derefChar ptr = code inline {
	load_ui8 0
}

derefCharArray :: !Pointer !Int -> {#Char}
derefCharArray ptr len = copy 0 (createArray len '\0')
where
	copy :: !Int *{#Char} -> *{#Char}
	copy i arr
	| i == len = arr
	# c = derefChar (ptr+i)
	= copy (i+1) {arr & [i]=c}

derefString :: !Pointer -> String
derefString ptr
# len = findNull ptr - ptr
= derefCharArray ptr len
where
	findNull :: !Pointer -> Pointer
	findNull ptr = case derefChar ptr of
		'\0' -> ptr
		_    -> findNull (ptr+1)