PmCleanSystem.icl 42.4 KB
Newer Older
John van Groningen's avatar
John van Groningen committed
1 2
implementation module PmCleanSystem

3
import StdEnv,StdMisc,StdMaybe
John van Groningen's avatar
John van Groningen committed
4 5
from Directory import pd_StringToPath,getFileInfo,createDirectory,::Path,::FileInfo{pi_fileInfo},::PI_FileInfo{isDirectory},::DirError(..)
import UtilStrictLists
6
from Platform import DirSeparatorString,IF_MACOSX
John van Groningen's avatar
John van Groningen committed
7
import StdPathname
8
import PmTypes
John van Groningen's avatar
John van Groningen committed
9 10 11 12
from PmCompilerOptions import ::CompilerOptions(..),::ListTypes(..),instance == ListTypes
from PmPath import ModuleDirAndNameToABCSystemPathname,ModuleDirAndNameToObjSystemPathname
import PmCallBack
from PmParse import IsTypeSpec,IsImportError20
13 14 15 16
from linkargs import ReadLinkErrors,WriteLinkOpts,:: LinkInfo`(..),:: LPathname

from Platform import TempDir
tooltempdir =: TempDir
John van Groningen's avatar
John van Groningen committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

:: CompileOrCheckSyntax = SyntaxCheck | Compilation

:: CompilePollCompletedResult = NoFinishedCompiler | UnknownFinishedCompiler | FinishedCompiler !Int !Int
:: CodeGenerateAsmOrCode = AsmGeneration | CodeGeneration

:: CompilingInfo = NoCompiler | CompilerProcess !CompilerProcess

:: CompilerProcess = {
	commands_file_name :: !{#Char},
	results_file_name :: !{#Char},
	compiler_pid :: !Int,
	commands_fd :: !Int,
	results_fd :: !Int
  }

:: CompilerProcessIds :== [CompilerProcess]

::	CompilerMsg
	= 	CompilerOK
	| 	SyntaxError
	| 	Patherror Pathname

::	WindowFun env :== ([String]) -> env -> env

instance == CompileOrCheckSyntax where
	(==) SyntaxCheck SyntaxCheck = True
	(==) SyntaxCheck _ = False
	(==) Compilation Compilation = True
	(==) Compilation _ = False

instance == CodeGenerateAsmOrCode where
	(==) CodeGeneration CodeGeneration = True
	(==) AsmGeneration AsmGeneration = True
	(==) _ _ = False

NoCompilerProcessIds :: CompilerProcessIds
NoCompilerProcessIds = []

standardStaticLibraries :: !Processor !LinkMethod -> List String
standardStaticLibraries _ method
	= Nil

standardObjectFiles :: !Bool !Bool !Processor !Bool -> List String
standardObjectFiles stack_traces profiling _ use_64_bit_processor
	#! startup_file = if stack_traces "_startupTrace.o"
					  (if profiling "_startupProfile.o" "_startup.o")
	= (startup_file :! "_system.o" :! Nil)

getLibs :: ![String] !*Files -> (!(![String],![String]),!*Files)
getLibs [] files = (([],[]),files)
68
getLibs libs files = abort "getLibs\n"
John van Groningen's avatar
John van Groningen committed
69 70 71 72 73

InitCompilingInfo :: *CompilingInfo
InitCompilingInfo = NoCompiler

Compile :: !String !Bool !Bool !(WindowFun *env) !(WindowFun *env) !CompileOrCheckSyntax !ModuleDirAndName !Pathname
74
	!(List Pathname) !ProjectCompilerOptions !CompilerOptions !Pathname !CompilerProcessIds !*env
John van Groningen's avatar
John van Groningen committed
75 76 77
	-> (!Pathname,!CompilerMsg,!CompilerProcessIds,!*env)
	| FileEnv env
Compile
78 79
	cocl use_compiler_process_ids write_module_times errwin typewin compileOrCheckSyntax mdn path paths project_compiler_options
	co=:{CompilerOptions | listTypes}
John van Groningen's avatar
John van Groningen committed
80 81 82 83 84 85 86
	startupdir compiler_process_ids env
	# (cocl,cocl_dir,options1,options2) = get_path_name_and_options2 cocl startupdir

	# temp_dir = temp_dir_path startupdir
	  out_file_name = out_file_path temp_dir 0
	  errors_file_name = errors_file_path temp_dir 0

87
	# cocl_arguments = compiler_arguments out_file_name errors_file_name compileOrCheckSyntax path paths write_module_times project_compiler_options co
John van Groningen's avatar
John van Groningen committed
88 89 90 91 92 93 94
	  cocl_arguments = add_options_string_to_args 0 options2 cocl_arguments
	  cocl_arguments = add_options_string_to_args 0 options1 cocl_arguments

	# (argv,args_memory) = make_argv [cocl:cocl_arguments]

	# compiler_pid = fork
	| compiler_pid<0
95
		= abort "fork failed\n"
John van Groningen's avatar
John van Groningen committed
96 97
	| compiler_pid==0
		| execv (cocl+++"\0") argv<0
98 99
			= abort "execv failed\n"
			= abort "execution continued after execv\n"
John van Groningen's avatar
John van Groningen committed
100
	| free args_memory<0
101
		= abort "free failed\n"
John van Groningen's avatar
John van Groningen committed
102 103
	# (w_pid,status) = wait_pid compiler_pid 0
	| w_pid <> -1 && w_pid<>compiler_pid
104
		= abort "waitpid failed\n"
John van Groningen's avatar
John van Groningen committed
105 106 107
	# result = (status bitand 0xff00) >> 8
	# wtermsig = status bitand 0x7f
	| wtermsig<>0
108
		= abort "compiler exited abnormally\n"
John van Groningen's avatar
John van Groningen committed
109 110 111 112 113 114 115 116 117 118


	# exitcode = if (result==0) 0 1

	# dummy_slot = 0
	# (path,mess,env) =	CompileHandleExitCode exitcode cocl startupdir dummy_slot errwin typewin mdn listTypes env
	= (path,mess,compiler_process_ids,env)

CompilePersistent ::
	!String !Bool !(WindowFun *env) !(WindowFun *env) !CompileOrCheckSyntax !ModuleDirAndName
119
	!(List Pathname) !ProjectCompilerOptions !CompilerOptions !Pathname !*CompilingInfo !*env
John van Groningen's avatar
John van Groningen committed
120 121
	-> (!*CompilingInfo,!(!*env, !Pathname, !CompilerMsg))
	| FileEnv env
122 123
CompilePersistent cocl write_module_times errwin typewin compileOrCheckSyntax mdn paths project_compiler_options
					co=:{CompilerOptions | listTypes}
John van Groningen's avatar
John van Groningen committed
124 125 126
					startupdir compiling_info env
	# (cocl,cocl_dir,options1,options2) = get_path_name_and_options2 cocl startupdir

127 128
	# (compiling_info=:CompilerProcess {commands_fd,results_fd})
		= start_compiler_if_not_yet_started cocl options1 compiling_info
John van Groningen's avatar
John van Groningen committed
129 130 131 132 133

	# temp_dir = temp_dir_path startupdir
	  out_file_name = out_file_path temp_dir 0
	  errors_file_name = errors_file_path temp_dir 0

134
	# args = makeCompilerOptionsArguments compileOrCheckSyntax write_module_times project_compiler_options co
John van Groningen's avatar
John van Groningen committed
135 136 137 138 139 140 141 142 143 144
	# args_string = concat_args args+++
		" "+++mdn.mdn_name+++
		" -P "+++"\""+++concatenate_paths paths+++"\""+++
		" -RE "+++"\""+++errors_file_name+++"\""+++
		" -RO "+++"\""+++out_file_name+++"\""
	# args_string = if (size options2==0) args_string (options2+++" "+++args_string)
	# args_string = "cocl "+++args_string+++"\n"
	  n_chars = size args_string
	# r = write commands_fd args_string n_chars
	| r<>n_chars
145
		= abort "write_failed\n"
John van Groningen's avatar
John van Groningen committed
146 147 148 149

	# result_string = createArray 6 '\0'
	# r = read results_fd result_string 6;
	| r<=1
150
		= abort ("read failed "+++toString r +++ "\n")
John van Groningen's avatar
John van Groningen committed
151 152

	# exitcode = parse_result_number 0 result_string
153

John van Groningen's avatar
John van Groningen committed
154 155 156 157 158 159 160 161 162 163
	# dummy_slot = 0
	# (path,mess,env) =	CompileHandleExitCode exitcode cocl startupdir dummy_slot errwin typewin mdn listTypes env
	= (compiling_info,(env,path,mess))

parse_result_number i result_string
	# c=result_string.[i]
	| c>='0' && c<='9'
		= parse_result_number2 (i+1) (toInt c-48) result_string
	| c=='-'
		= 0 - parse_result_number (i+1) result_string
164
		= abort "parse_result_number failed\n"
165
where
John van Groningen's avatar
John van Groningen committed
166 167 168 169 170 171 172
	parse_result_number2 i n result_string
		| i<size result_string
			# c=result_string.[i]
			| c>='0' && c<='9'
				= parse_result_number2 (i+1) (n*10+(toInt c-48)) result_string
			| c=='\n'
				= n
173 174
				= abort "parse_result_number2 failed\n"
			= abort "parse_result_number2 failed\n"
John van Groningen's avatar
John van Groningen committed
175 176 177

CompileStartCommand ::
	!String !Bool !(WindowFun *env) !CompileOrCheckSyntax !Pathname
178
	!(List Pathname) !Int !ProjectCompilerOptions !CompilerOptions !Pathname !CompilerProcessIds !*env
John van Groningen's avatar
John van Groningen committed
179 180 181
	-> (!Bool,!CompilerProcessIds,!*env)
	| FileEnv env
CompileStartCommand cocl write_module_times errwin compileOrCheckSyntax path
182
		paths slot project_compiler_options co startupdir compiler_process_ids ps
John van Groningen's avatar
John van Groningen committed
183 184 185
	# (cocl,cocl_dir,options1,options2) = get_path_name_and_options2 cocl startupdir

	# ({commands_fd,results_fd},compiler_process_ids)
186
		= start_a_compiler_if_not_yet_started cocl slot compiler_process_ids options1
John van Groningen's avatar
John van Groningen committed
187 188 189 190 191

	# temp_dir = temp_dir_path startupdir
	  out_file_name = out_file_path temp_dir slot
	  errors_file_name = errors_file_path temp_dir slot

192
	# args = makeCompilerOptionsArguments compileOrCheckSyntax write_module_times project_compiler_options co
John van Groningen's avatar
John van Groningen committed
193 194 195 196 197 198 199 200 201 202
	# args_string = concat_args args+++
		" "+++path+++
		" -P "+++concatenate_paths paths+++
		" -RE "+++errors_file_name+++
		" -RO "+++out_file_name
	# args_string = if (size options2==0) args_string (options2+++" "+++args_string)
	# args_string = "cocl "+++args_string+++"\n"
	  n_chars = size args_string
	# r = write commands_fd args_string n_chars
	| r<>n_chars
203
		= abort "write_failed\n"
John van Groningen's avatar
John van Groningen committed
204 205 206 207 208 209 210
	= (True,compiler_process_ids,ps)

exit_clean_compiler {compiler_pid,commands_fd,results_fd,commands_file_name,results_file_name} env
	# args_string = "quit\n"
	  n_chars = size args_string
	# r = write commands_fd args_string n_chars
	| r<>n_chars
211
		= abort "write_failed\n"
John van Groningen's avatar
John van Groningen committed
212 213
	# (w_pid,status) = wait_pid compiler_pid 0
	| w_pid <> -1 && w_pid<>compiler_pid
214
		= abort "waitpid failed\n"
John van Groningen's avatar
John van Groningen committed
215 216 217
	# result = (status bitand 0xff00) >> 8
	# wtermsig = status bitand 0x7f
	| wtermsig<>0
218
		= abort "compiler exited abnormally\n"
John van Groningen's avatar
John van Groningen committed
219 220
	# r=close commands_fd
	| r==(-1)
221
		= abort "close failed\n"
John van Groningen's avatar
John van Groningen committed
222 223
	# r=close results_fd
	| r==(-1)
224
		= abort "close failed\n"
John van Groningen's avatar
John van Groningen committed
225
	| unlink (commands_file_name+++"\0")<>0
226
		= abort "unlink failed\n"
John van Groningen's avatar
John van Groningen committed
227
	| unlink (results_file_name+++"\0")<>0
228
		= abort "unlink failed\n"
John van Groningen's avatar
John van Groningen committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242
	= env

ExitCleanCompiler :: !*(!*CompilingInfo,*env) -> *(!*CompilingInfo,*env)
ExitCleanCompiler (NoCompiler,env)
	= (NoCompiler,env)
ExitCleanCompiler (CompilerProcess compiler,env)
	#! env = exit_clean_compiler compiler env
	= (NoCompiler,env)

QuitCleanCompiler :: !Bool !CompilerProcessIds !*World -> *World
QuitCleanCompiler async compiler_process_ids world
	| async
		= exit_compilers compiler_process_ids world
		= world
243
where
John van Groningen's avatar
John van Groningen committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	exit_compilers [compiler:compilers] world
		= exit_compilers compilers (exit_clean_compiler compiler world)
	exit_compilers [] world
		= world

CodeGen	::	!String !Bool !(WindowFun *GeneralSt) !CodeGenerateAsmOrCode !Pathname !Pathname !Bool
			!CodeGenOptions !Processor !ApplicationOptions !Pathname !CompilerProcessIds !*GeneralSt
			-> (!Pathname,!Bool,!CompilerProcessIds,!*GeneralSt)
CodeGen cgen used_compiler_process_ids wf genAsmOrCode abc_path obj_path timeprofile cgo tp ao startupdir compiler_process_ids ps
	# (cgen,cgendir,options) = get_path_name_and_options cgen startupdir
	# path_without_suffix = RemoveSuffix abc_path
	# cg_arguments = make_code_generator_arguments genAsmOrCode cgo

	# temp_dir = temp_dir_path startupdir
	  errors_file_name = errors_file_path temp_dir 0
	# stderr_fd = creat (errors_file_name+++"\0") 0777;
	| stderr_fd== (-1)
261
		= abort "creat failed\n"
John van Groningen's avatar
John van Groningen committed
262 263 264 265 266

	# (argv,args_memory) = make_argv [cgen : add_options_string_to_args 0 options (cg_arguments++[path_without_suffix])]

	# code_generator_pid = fork
	| code_generator_pid<0
267
		= abort "fork failed\n"
John van Groningen's avatar
John van Groningen committed
268 269 270
	| code_generator_pid==0
		# r=dup2 stderr_fd 2
		| r== (-1)
271
			= abort "dup2 failed\n"
John van Groningen's avatar
John van Groningen committed
272
		| execv (cgen+++"\0") argv<0
273 274
			= abort "execv failed\n"
			= abort "execution continued after execv\n"
John van Groningen's avatar
John van Groningen committed
275
	| free args_memory<0
276
		= abort "free failed\n"
John van Groningen's avatar
John van Groningen committed
277 278 279

	# (w_pid,status) = wait_pid code_generator_pid 0
	| w_pid <> -1 && w_pid<>code_generator_pid
280
		= abort "waitpid failed\n"
John van Groningen's avatar
John van Groningen committed
281 282 283
	# result = (status bitand 0xff00) >> 8
	# wtermsig = status bitand 0x7f
	| wtermsig<>0
284
		= abort "code generator exited abnormally\n"
John van Groningen's avatar
John van Groningen committed
285 286 287

	# r=close stderr_fd
	| r==(-1)
288
		= abort "close failed\n"
John van Groningen's avatar
John van Groningen committed
289 290 291 292 293

	# exit_code = if (result==0) 0 1

	# ((_, errors_not_empty, error_text),ps) = accFiles (readErrorsAndWarnings errors_file_name) ps
	  ps = if errors_not_empty
294
				(wf (StrictListToList error_text) ps)
John van Groningen's avatar
John van Groningen committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
				(if (exit_code <> 0)
					(wf ["Error: Code generator failed for '"+++abc_path+++"' with exit code: "+++toString exit_code,(quoted_string path_without_suffix)] ps)
					ps
					)
	= (obj_path,exit_code==0,compiler_process_ids,ps)

:: StartedCodeGenerator = !{
	scg_abc_path :: !{#Char},
	scg_path_without_suffix :: !{#Char},
	scg_std_error_fd :: !Int,
	scg_errors_file_name :: !{#Char}
  }

start_code_generator ::	!String !(WindowFun *GeneralSt) !Pathname !Int !Bool !CodeGenOptions !Processor !Pathname !*GeneralSt
						-> (!Bool,!Int/*HANDLE*/,!StartedCodeGenerator,!*GeneralSt)
start_code_generator cgen wf path slot timeprofile cgo tp startupdir ps
	# (cgen,cgendir,options) = get_path_name_and_options cgen startupdir
	# path_without_suffix = RemoveSuffix path
	# cg_arguments = make_code_generator_arguments CodeGeneration cgo

	# temp_dir = temp_dir_path startupdir
	  errors_file_name = errors_file_path temp_dir slot
	# stderr_fd = creat (errors_file_name+++"\0") 0777;
	| stderr_fd== (-1)
319
		= abort "creat failed\n"
John van Groningen's avatar
John van Groningen committed
320 321 322 323 324

	# (argv,args_memory) = make_argv [cgen : add_options_string_to_args 0 options (cg_arguments++[path_without_suffix])]

	# code_generator_pid = fork
	| code_generator_pid<0
325
		= abort "fork failed\n"
John van Groningen's avatar
John van Groningen committed
326 327 328
	| code_generator_pid==0
		# r=dup2 stderr_fd 2
		| r== (-1)
329
			= abort "dup2 failed\n"
John van Groningen's avatar
John van Groningen committed
330
		| execv (cgen+++"\0") argv<0
331 332
			= abort "execv failed\n"
			= abort "execution continued after execv\n"
John van Groningen's avatar
John van Groningen committed
333
	| free args_memory<0
334
		= abort "free failed\n"
John van Groningen's avatar
John van Groningen committed
335 336 337 338 339 340 341 342
	# scg = {scg_abc_path=path,scg_path_without_suffix=path_without_suffix,
			 scg_std_error_fd=stderr_fd,scg_errors_file_name=errors_file_name}
	= (True,code_generator_pid,scg,ps)

wait_for_finished_code_generator :: !{#Int} !*GeneralSt -> (!Int,!Int,!*GeneralSt)
wait_for_finished_code_generator process_ids ps
	# (w_pid,status) = wait_pid -1 0
	| w_pid<0
343
		= abort "waitpid failed\n"
John van Groningen's avatar
John van Groningen committed
344 345 346 347
	# process_n = find_process_id_index 0 w_pid process_ids
	# result = (status bitand 0xff00) >> 8
	# wtermsig = status bitand 0x7f
	| wtermsig<>0
348
		= abort "code generator exited abnormally\n"
John van Groningen's avatar
John van Groningen committed
349
	= (process_n,result,ps)
350
where
John van Groningen's avatar
John van Groningen committed
351 352 353 354 355 356 357 358 359 360
	find_process_id_index i w_pid pids
		| i<size pids
			| pids.[i]==w_pid
				= i
				= find_process_id_index (i+1) w_pid pids

finish_code_generator :: !Int/*HANDLE*/ !StartedCodeGenerator !Int !(WindowFun *GeneralSt) !*GeneralSt -> (!Bool,!*GeneralSt)
finish_code_generator process_handle {scg_abc_path,scg_path_without_suffix,scg_std_error_fd,scg_errors_file_name} exit_code wf ps
	# r=close scg_std_error_fd
	| r==(-1)
361
		= abort "close failed\n"
John van Groningen's avatar
John van Groningen committed
362 363
	# ((_, errors_not_empty, error_text),ps) = accFiles (readErrorsAndWarnings scg_errors_file_name) ps
	  ps = if errors_not_empty
364
				(wf (StrictListToList error_text) ps)
John van Groningen's avatar
John van Groningen committed
365 366 367 368 369 370
				(if (exit_code <> 0)
					(wf ["Error: Code generator failed for '" +++ scg_abc_path +++ "' with exit code: "+++toString exit_code,(quoted_string scg_path_without_suffix)] ps)
					ps)
	= (exit_code==0, ps)

Link ::	!String !(WindowFun *GeneralSt) !Pathname !ApplicationOptions
371
		!Pathname !(List Pathname) !(List Pathname) !(List Pathname) !Bool !Bool !Bool !Bool !Bool !String
John van Groningen's avatar
John van Groningen committed
372 373 374 375
		!Bool !String !Pathname !String !Processor !Bool !*GeneralSt
		 -> (!*GeneralSt,!Bool)
Link linker winfun path
		applicationOptions=:{ss,hs,initial_heap_size,profiling,heap_size_multiple,o,memoryProfilingMinimumHeapSize=minheap}
376
		optionspathname library_file_names object_file_names static_libraries static gen_relocs gen_symbol_table gen_linkmap
John van Groningen's avatar
John van Groningen committed
377
		link_resources resource_path gen_dll dll_syms startupdir dynlstr _ use_64_bit_processor ps
378
	# (linker,linker_dir,options1,options2) = get_path_name_and_options2 linker startupdir
John van Groningen's avatar
John van Groningen committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392
	# flags = ApplicationOptionsToFlags applicationOptions
	  optdirpath = RemoveFilename optionspathname
	  ((ok,pd_optdirpath),ps) = pd_StringToPath optdirpath ps
	| not ok
		= (winfun ["Linker error: Unable to understand path: "+++optdirpath] ps,False)
	# ((err,{pi_fileInfo={isDirectory}}),ps) = getFileInfo pd_optdirpath ps
	  (err,ps) = if (case err of DoesntExist -> True; _ -> not isDirectory)
					(createDirectory pd_optdirpath ps)
					(err,ps)
	| case err of NoDirError -> False; _ -> True
		= (winfun ["Linker error: Unable to access or create: "+++optdirpath] ps,False)
	# (options_file_ok,ps) = accFiles (write_options_file optionspathname flags hs ss initial_heap_size heap_size_multiple minheap use_64_bit_processor) ps
	| not options_file_ok
		= (winfun ["Linker error: Could not write the options object file: "+++optionspathname] ps,False)
Haye Böhm's avatar
Haye Böhm committed
393 394 395 396 397 398 399 400 401 402
	# object_paths = [optionspathname : StrictListToList (RemoveDup object_file_names)]
	# len = size linker
	| len >= 14 && linker % (len - 14, len) == "lib/exe/linker"
		= linkOptimizingGCC linker object_paths options1 options2 ps
	| len >= 12 && linker % (len - 12, len) == "/usr/bin/gcc"
		= linkGCC object_paths options2 ps
		= linkOther linker object_paths ps
where
	//First call the optimizing linker to generate an intermediate optimized object file which is then passed to gcc
	linkOptimizingGCC linker object_paths options1 options2 ps
403 404 405 406 407 408 409 410 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
		# temp_file_name = "/tmp/linkerXXXXXX" +++ "\0" // +++ because modified by mkstemp
		# fd = mkstemp temp_file_name
		| fd== -1
			= abort "mkstemp failed\n"
		# r = close fd
		| r== -1
			= abort "close failed\n"
		//Call optimized linker
		# linker_args = add_options_string_to_args 0 options1 [temp_file_name:object_paths]
		# (argv,args_memory) = make_argv [linker:linker_args]

		# linker_pid = fork
		| linker_pid<0
			= abort "fork failed\n"
		| linker_pid==0
			| execv (linker+++"\0") argv<0
				= abort "execv failed\n"
				= abort "execution continued after execv\n"
		# (w_pid,status) = wait_pid linker_pid 0
		| w_pid <> -1 && w_pid<>linker_pid
			= abort "waitpid failed\n"
		# exit_code = (status >> 8) bitand 0xff
		# wtermsig = status bitand 0x7f
		| wtermsig<>0
			= abort "linker exited abnormally\n"
		| free args_memory<0
			= abort "free failed\n"

		# link_ok = exit_code==0
		| not link_ok
			| unlink (temp_file_name+++"\0")<>0
				=  (ps,link_ok)
				=  (ps,link_ok)
		//Call gcc
437 438 439 440
		#! (ps, link_ok) = linkGCC [temp_file_name] options2 ps
		| unlink (temp_file_name+++"\0")<>0
			= (ps,link_ok)
			= (ps,link_ok)
Haye Böhm's avatar
Haye Böhm committed
441 442 443

	//Just link with gcc
	linkGCC object_paths gccOptions ps
444 445
		# ld_args = add_options_string_to_args 0 gccOptions (StrictListToList (Append library_file_names "-lm"))
		# ld_args = ["-o", path : object_paths ] ++ ld_args
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
		  ld_args = IF_MACOSX ld_args (IF_INT_64_OR_32 ["-no-pie":ld_args] ld_args)
		  ld_args = if gen_symbol_table ld_args ["-s":ld_args]
		# ld = "/usr/bin/gcc"
		# (argv,args_memory) = make_argv [ld:ld_args]
		# ld_pid = fork
		| ld_pid<0
			= abort "fork failed\n"
		| ld_pid==0
			| execv (ld+++"\0") argv<0
				= abort "execv failed\n"
				= abort "execution continued after execv\n"
		| free args_memory<0
			= abort "free failed\n"
		# (w_pid,status) = wait_pid ld_pid 0
		| w_pid <> -1 && w_pid<>ld_pid
			= abort "waitpid failed\n"
		# wtermsig = status bitand 0x7f
		# exit_code = (status >> 8) bitand 0xff;
		| wtermsig<>0
			= abort "ld exited abnormally\n"
		# link_ok = exit_code==0
		= (ps,link_ok)
Haye Böhm's avatar
Haye Böhm committed
468 469 470

	//Call other/additional linker
	linkOther linker object_paths ps
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
		# linkopts  =
			{ exe_path                  = path
			, res_path                  = resource_path
			, open_console              = o <> NoConsole
			, static_link               = static
			, gen_relocs                = gen_relocs
			, gen_symbol_table          = gen_symbol_table
			, gen_linkmap               = gen_linkmap
			, link_resources            = link_resources
			, object_paths              = ListToStrictList object_paths
			, dynamic_libs              = RemoveDup library_file_names
			, static_libs               = RemoveDup static_libraries
			, stack_size                = ss
			, gen_dll                   = gen_dll
			, dll_names                 = dll_syms
			, dynamics_path             = startupdir +++. DirSeparatorString +++. dynlstr
			, lib_name_obj_path         = MakeFullPathname tooltempdir "lib_name.o"
			}
		# linkoptspath = MakeFullPathname tooltempdir "linkopts"
		# linkerrspath = MakeFullPathname tooltempdir "linkerrs"
		# (err,ps) = accFiles (WriteLinkOpts linkoptspath linkopts) ps
		| isJust err
			= (winfun (fromJust err) ps,False)
		# linker_args = ["-I",linkoptspath,"-O",linkerrspath]
		# (argv,args_memory) = make_argv [linker:linker_args]
		# linker_pid = fork
		| linker_pid<0
			= abort "fork failed\n"
		| linker_pid==0
			| execv (linker+++"\0") argv<0
				= abort "execv failed\n"
				= abort "execution continued after execv\n"
		# (w_pid,status) = wait_pid linker_pid 0
		| w_pid <> -1 && w_pid<>linker_pid
			= abort "waitpid failed\n"
		# exit_code = (status >> 8) bitand 0xff
		# wtermsig = status bitand 0x7f
		| wtermsig<>0
			= abort "linker exited abnormally\n"
		| free args_memory<0
			= abort "free failed\n"
		# link_ok = exit_code==0
		= (ps,link_ok)
514

John van Groningen's avatar
John van Groningen committed
515 516 517 518 519 520 521 522 523 524 525 526 527
DelayEventLoop :: !.ps -> .ps
DelayEventLoop ps
	= ps

POLLIN :== 1

CompilePollCompleted :: !CompilerProcessIds !*env -> (!CompilePollCompletedResult, !*env) | FileEnv env
CompilePollCompleted compiler_process_ids ps
	# n_processes = length compiler_process_ids
	  fda = createArray (IF_INT_64_OR_32 n_processes (n_processes<<1)) 0
	  fda = fill_fda compiler_process_ids 0 fda
	  r = poll fda n_processes -1
	| r<0
528
		= abort "poll failed\n"
John van Groningen's avatar
John van Groningen committed
529 530 531 532
	| r==0
		= (NoFinishedCompiler,ps)
		# compiler_id = find_fd_index 0 fda
		# results_fd = (compiler_process_ids !! compiler_id).results_fd
533

John van Groningen's avatar
John van Groningen committed
534 535 536
		# result_string = createArray 6 '\0'
		# r = read results_fd result_string 6;
		| r<=1
537
			= abort ("read failed "+++toString r+++ "\n")
John van Groningen's avatar
John van Groningen committed
538
		# exit_code = parse_result_number 0 result_string
539

John van Groningen's avatar
John van Groningen committed
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
		= (FinishedCompiler compiler_id exit_code,ps)
where
	fill_fda :: !CompilerProcessIds !Int !*{#Int} -> *{#Int}
	fill_fda [{results_fd}:process_ids] i fda
		# fda = IF_INT_64_OR_32
					{fda & [i] = results_fd + (POLLIN<<32)}
					{fda & [i] = results_fd, [i+1] = POLLIN}
		= fill_fda process_ids (i+(IF_INT_64_OR_32 1 2)) fda
	fill_fda [] i fda
		= fda

	find_fd_index i fda
		| i<size fda
			#! r_events = (fda.[IF_INT_64_OR_32 i ((i<<1)+1)] >> (IF_INT_64_OR_32 48 16)) bitand 0xffff;
			| r_events==0
				= find_fd_index (i+1) fda
			| r_events==POLLIN
				= i
558
				= abort ("polling file descriptors failed with event"+++toString r_events+++"\n")
John van Groningen's avatar
John van Groningen committed
559 560 561 562 563

O_RDONLY:==0;
O_WRONLY:==1;
O_RDWR:==2;

564 565
start_compiler :: !{#Char} !{#Char} -> *CompilerProcess
start_compiler compiler_file_name initial_args
John van Groningen's avatar
John van Groningen committed
566 567 568
	# commands_file_name = "/tmp/comXXXXXX" +++ "\0" // +++ because modified by mkstemp
	# fd = mkstemp commands_file_name
	| fd== -1
569
		= abort "mkstemp failed\n"
John van Groningen's avatar
John van Groningen committed
570 571
	# r = close fd
	| r== -1
572
		= abort "close failed\n"
John van Groningen's avatar
John van Groningen committed
573 574
	# r = unlink commands_file_name
	| r== -1
575
		= abort "unlink failed\n"
John van Groningen's avatar
John van Groningen committed
576 577
	# r = mkfifo commands_file_name (S_IRUSR bitor S_IWUSR)
	| r== -1
578
		= abort "mkfifo failed\n"
John van Groningen's avatar
John van Groningen committed
579 580 581 582

	# results_file_name = "/tmp/resXXXXXX" +++ "\0" // +++ because modified by mkstemp
	# fd = mkstemp results_file_name
	| fd== -1
583
		= abort "mkstemp failed\n"
John van Groningen's avatar
John van Groningen committed
584 585
	# r = close fd
	| r== -1
586
		= abort "close failed\n"
John van Groningen's avatar
John van Groningen committed
587 588
	# r = unlink results_file_name
	| r== -1
589
		= abort "unlink failed\n"
John van Groningen's avatar
John van Groningen committed
590 591
	# r = mkfifo results_file_name (S_IRUSR bitor S_IWUSR)
	| r== -1
592
		= abort "mkfifo failed\n"
John van Groningen's avatar
John van Groningen committed
593

594
	# cocl_arguments = add_options_string_to_args 0 initial_args ["--pipe",commands_file_name,results_file_name]
John van Groningen's avatar
John van Groningen committed
595 596 597 598 599

	# (argv,args_memory) = make_argv [compiler_file_name:cocl_arguments]

	# compiler_pid = fork
	| compiler_pid<0
600
		= abort "fork failed\n"
John van Groningen's avatar
John van Groningen committed
601 602
	| compiler_pid==0
		| execv (compiler_file_name+++"\0") argv<0
603 604
			= abort "execv failed\n"
			= abort "execution continued after execv\n"
John van Groningen's avatar
John van Groningen committed
605
	| free args_memory<0
606
		= abort "free failed\n"
John van Groningen's avatar
John van Groningen committed
607 608 609

	# commands_fd = open (commands_file_name+++"\0") O_WRONLY 0
	| commands_fd == -1
610
		= abort "open failed\n"
John van Groningen's avatar
John van Groningen committed
611 612
	# results_fd = open (results_file_name+++"\0") O_RDONLY 0
	| results_fd == -1
613
		= abort "open failed\n"
John van Groningen's avatar
John van Groningen committed
614 615 616 617

	= {	commands_file_name=commands_file_name, results_file_name=results_file_name,
		compiler_pid=compiler_pid, commands_fd=commands_fd, results_fd=results_fd}

618 619 620 621
start_compiler_if_not_yet_started :: !{#Char} !{#Char} !*CompilingInfo -> *CompilingInfo
start_compiler_if_not_yet_started compiler_file_name initial_args NoCompiler
	= CompilerProcess (start_compiler compiler_file_name initial_args)
start_compiler_if_not_yet_started compiler_file_name initial_args compiling_info
John van Groningen's avatar
John van Groningen committed
622 623
	= compiling_info

624
start_a_compiler_if_not_yet_started compiler_file_name slot compiler_process_ids initial_args
John van Groningen's avatar
John van Groningen committed
625 626
	| slot<length compiler_process_ids
		= (compiler_process_ids !! slot,compiler_process_ids)
627
		# compiler_process = start_compiler compiler_file_name initial_args
John van Groningen's avatar
John van Groningen committed
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
		# compiler_process_ids = compiler_process_ids++[compiler_process]
		= (compiler_process,compiler_process_ids)

CompileHandleExitCode :: !Int !String !String !Int !(WindowFun *env) !(WindowFun *env) !ModuleDirAndName
				!ListTypes !*env -> (!Pathname,!CompilerMsg,!*env) | FileEnv env
CompileHandleExitCode exitcode cocl startupdir slot errwin typewin mdn listTypes ps
	# temp_dir = temp_dir_path startupdir
	  out_file_name = out_file_path temp_dir slot
	  errors_file_name = errors_file_path temp_dir slot
	#	((type_text_not_empty,type_text),ps)
			= accFiles (readTypesInfo (listTypes<>NoTypes) out_file_name) ps
		((errors,errors_and_messages_not_empty,errors_and_messages),ps)
			= accFiles (readErrorsAndWarnings errors_file_name) ps
	| exitcode <> 0 && not errors_and_messages_not_empty
	  = ( ""
		, SyntaxError
		, errwin [  "Error: Compiler crashed: "+++cocl
				 : (case errors of CompilerOK -> ["Unable to open Errors file"] ; _ -> [])
				 ] ps
		)
	#	abcpath			= ModuleDirAndNameToABCSystemPathname mdn
		ps				= (if type_text_not_empty (typewin (StrictListToList type_text)) id) ps
		ps				= (if errors_and_messages_not_empty (errwin (StrictListToList errors_and_messages)) id) ps
		errors			= case exitcode of
							0	-> CompilerOK
							_	-> errors
     = (abcpath,errors,ps)
where
	readTypesInfo :: !Bool !Pathname !*Files -> ((!Bool,!(List String)),!*Files)
	readTypesInfo readtypes	path env
		| not readtypes
			= ((False,Nil),env)
		# (opened,file,env) = fopen path FReadText env
		| not opened
			= ((False,Nil),env)
		# (typelist,types_read,file`) = readTypeMsg file
		  (_,env) = fclose file` env
		= ((types_read,typelist),env)

	readTypeMsg :: !*File -> (!List String,!Bool,!*File)
	readTypeMsg file
		#	(string,file)					= freadline file
			(eof,file)						= fend file
		| eof && IsTypeSpec string
			= (strip_newlines string :! Nil,True,file)
		| eof
			= (Nil,False,file)
		#	(typeslist,types_read,file)	= readTypeMsg file
		= (strip_newlines string :! typeslist,types_read,file)

readErrorsAndWarnings :: !Pathname !*Files -> ((!CompilerMsg, !Bool, !(List String)), !*Files)
readErrorsAndWarnings path env
	# (opened,file,env) = fopen path FReadText env
	| not opened
		= ((CompilerOK,False,Nil),env)
	# (errors,errors_and_warnings_read,errlist,file`) = readErrorAndWarningMessages file
	  (_,env) = fclose file` env
	= ((errors,errors_and_warnings_read,errlist),env)
where
	readErrorAndWarningMessages :: !*File -> (!CompilerMsg,!Bool,!List String,!*File)
	readErrorAndWarningMessages file
		#!	(string, file1)					= freadline file
			(eof,file2)						= fend file1
			(is_import_error,path)			= IsImportError20 string
		| eof
			#!	not_empty_or_newline 		= (size string)<>0 && string.[0]<>'\n'
			= (if is_import_error (Patherror path) SyntaxError,not_empty_or_newline,strip_newlines string :! Nil,file2)
		#	(path_error,_,errlist,file3) = readErrorAndWarningMessages file2
		= (if is_import_error (Patherror path) path_error,True,strip_newlines string:!errlist,file3)

strip_newlines :: !{#Char} -> {#Char}
strip_newlines s
	| size s==0
		= s
		# last = dec (size s)
		  char = s.[last]
		| char == '\n' || char == '\r'
			= strip_newlines (s % (0,dec last))
		= s

temp_dir_path startupdir
	= startupdir +++ DirSeparatorString +++ "Temp"

out_file_path temp_dir slot
	| slot==0
		= temp_dir +++ DirSeparatorString +++ "out"
		= temp_dir +++ DirSeparatorString +++ "out"+++toString slot

errors_file_path temp_dir slot
	| slot==0
		= temp_dir +++ DirSeparatorString +++ "errors"
		= temp_dir +++ DirSeparatorString +++ "errors"+++toString slot

get_path_name_and_options2 ccstring startupdir
	# (ccstring,rem) = splitOptions ccstring
	  (opts,opts`) = splitOptions rem
	  cocl = startupdir +++ "/" +++ ccstring
	  cocldir = RemoveFilename cocl
	= (cocl,cocldir,opts,opts`)

get_path_name_and_options codegen_or_linker startupdir
	# (codegen_or_linker,opts) = splitOptions codegen_or_linker
	| size codegen_or_linker>0 && codegen_or_linker.[0]=='/'
		# codegen_or_linker_dir = RemoveFilename codegen_or_linker
		= (codegen_or_linker,codegen_or_linker_dir,opts)
		# codegen_or_linker = startupdir +++ "/" +++ codegen_or_linker
		# codegen_or_linker_dir = RemoveFilename codegen_or_linker
		= (codegen_or_linker,codegen_or_linker_dir,opts)

splitOptions :: !{#Char} -> (!{#Char},!{#Char})
splitOptions str
	# len_str = size str
	  first_q = FindQuoteChar str len_str 0
	| first_q >= len_str
		= (str,"")
		# first_str = str % (0,dec first_q)
		  last_str = str % (first_q+1, len_str)
		= (first_str,last_str)
where
	FindQuoteChar str len pos	= FindChar ':' str len pos;

	FindChar	:: !Char !.String !.Int !Int -> Int;
	FindChar c line linelen pos
		| pos >= linelen		=  pos;
		| c ==  line.[pos]		=  pos;
								=  FindChar c line linelen (pos+1);

755
add_options_string_to_args :: !Int !{#Char} ![{#Char}] -> [{#Char}]
John van Groningen's avatar
John van Groningen committed
756 757 758 759 760 761
add_options_string_to_args i s args
	# first_i = skip_spaces_and_tabs i s
	| first_i>=size s
		= args
		# end_i = skip_to_space_or_tab (i+1) s
		= [s % (first_i,end_i-1) : add_options_string_to_args end_i s args]
762
where
John van Groningen's avatar
John van Groningen committed
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
	skip_spaces_and_tabs i s
		| i<size s
			# c=s.[i]
			| c==' ' || c=='\t'
				= skip_spaces_and_tabs (i+1) s
				= i
			= i

	skip_to_space_or_tab i s
		| i<size s
			# c=s.[i]
			| c==' ' || c=='\t'
				= i
				= skip_to_space_or_tab (i+1) s
			= i

779
compiler_arguments :: !String !String !CompileOrCheckSyntax !Pathname !(List Pathname) !Bool !ProjectCompilerOptions !CompilerOptions -> [String]
John van Groningen's avatar
John van Groningen committed
780
compiler_arguments out_file_name errors_file_name compileOrCheckSyntax path paths
781 782
					write_module_times project_compiler_options co
	# args = makeCompilerOptionsArguments compileOrCheckSyntax write_module_times project_compiler_options co
John van Groningen's avatar
John van Groningen committed
783 784 785 786 787 788
	= args ++ [path,"-P",concatenate_paths paths,"-RE",errors_file_name,"-RO",out_file_name];

concat_args [] = ""
concat_args [arg] = arg
concat_args [arg:args] = arg+++" "+++concat_args args

789
makeCompilerOptionsArguments :: !CompileOrCheckSyntax !Bool !ProjectCompilerOptions !CompilerOptions -> [String]
790 791 792
makeCompilerOptionsArguments compileOrCheckSyntax write_module_times
		{pco_memory_profiling,pco_time_profiling,pco_desc_exl,pco_dynamics,pco_generic_fusion,pco_link_dynamic}
		{neverMemoryProfile,neverTimeProfile,sa,gw,gc,listTypes,attr,reuseUniqueNodes,fusion}
793
	= write_module_times_arg ++ checksyntax ++ timeProfileSwitch ++ memoryProfileSwitch ++ descExlSwitch ++ dynamicsSwitch
John van Groningen's avatar
John van Groningen committed
794 795 796 797 798 799 800
		++ strictness ++ warnings ++ comments ++ listtypes ++ show_attr ++ reuse ++ fusion_arg
where
	write_module_times_arg
		| write_module_times
			= ["-wmt"]
			= []
	memoryProfileSwitch
801
		| (not neverMemoryProfile && pco_memory_profiling) || pco_desc_exl || pco_link_dynamic
John van Groningen's avatar
John van Groningen committed
802 803 804
			= ["-desc"]
			= []
	timeProfileSwitch
805
		| not neverTimeProfile && pco_time_profiling
John van Groningen's avatar
John van Groningen committed
806 807
			= ["-pt"]
			= []
808 809
	descExlSwitch = if (pco_desc_exl || pco_link_dynamic) ["-exl"] []
	dynamicsSwitch = if (pco_dynamics || pco_link_dynamic) ["-dynamics"] []
John van Groningen's avatar
John van Groningen committed
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
	strictness
		| sa
			= []
			= ["-sa"]
	warnings
		| gw
			= []
			= ["-w"]
	comments
		| gc
			= ["-d"]
			= []
	listtypes
		| listTypes == InferredTypes
			= ["-lt"]
		| listTypes == AllTypes
			= ["-lat"]
		| listTypes == StrictExportTypes
			= ["-lset"]
			= []
	show_attr
		| attr
			= []
			= ["-lattr"]
	checksyntax
		| compileOrCheckSyntax == SyntaxCheck
			= ["-c"]
			= []
	reuse
		| reuseUniqueNodes
			= ["-ou"]
			= []
	fusion_arg
843
		= case fusion of FusionOn -> ["-fusion"]; FusionDefault | pco_generic_fusion -> ["-generic_fusion"]; _ -> []
John van Groningen's avatar
John van Groningen committed
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861

make_code_generator_arguments genAsmOrCode {ci,cs}
	= checkindex++checkstack++genasm
where
	checkindex	| ci = ["-ci"]; = []
	checkstack	| cs = ["-os"]; = []
	genasm		| genAsmOrCode == AsmGeneration
								= ["-a"]
								= []

concatenate_paths :: (List Pathname) -> String
concatenate_paths ss
	# s = createArray (sSize ss) ':'
	= sUpdate 0 s ss
where
	sSize Nil = 0
	sSize (string :! Nil) = size string
	sSize (string :! rest) = size string + 1 + sSize rest
862

John van Groningen's avatar
John van Groningen committed
863 864 865 866 867 868 869 870
	sUpdate i s Nil = s
	sUpdate i s (string :! Nil)
		# (_,s) = sU (size string) i 0 s string
		= s
	sUpdate i s (string :! rest)
		# (i,s) = sU (size string) i 0 s string
		# i = inc i
		= sUpdate i s rest
871

John van Groningen's avatar
John van Groningen committed
872 873 874 875 876 877 878 879 880 881
	sU l i j s h
		| j >= l = (i,s)
		# s = update s i h.[j]
		= sU l (inc i) (inc j) s h

make_argv argv_list
	# args_size = argv_length argv_list 0
	  args_string = create_args_string args_size argv_list
	  args_memory = malloc args_size
	| args_memory==0
882
		= abort "malloc failed\n"
John van Groningen's avatar
John van Groningen committed
883 884 885 886 887 888 889 890 891 892 893 894
	# args_memory = memcpy_string_to_pointer args_memory args_string args_size
	  argv = create_argv argv_list args_memory
	= (argv,args_memory)
where
	argv_length [a:as] l
		= argv_length as (l+((size a+4) bitand -4))
	argv_length [] l
		= l

	create_args_string args_size argv_list
		# s = createArray args_size '\0'
		= copy_args argv_list 0 s
895 896 897 898
	where
		copy_args [a:as] i s
			# s = copy_chars 0 a i s
			= copy_args as (i+((size a+4) bitand -4)) s
John van Groningen's avatar
John van Groningen committed
899 900
		copy_args [] i s
			= s
901

John van Groningen's avatar
John van Groningen committed
902 903 904 905 906 907
		copy_chars :: !Int !{#Char} !Int !*{#Char} -> *{#Char}
		copy_chars ai a si s
			| ai<size a
				# s = {s & [si]=a.[ai]}
				= copy_chars (ai+1) a (si+1) s
				= s
908

John van Groningen's avatar
John van Groningen committed
909 910 911
	create_argv argv_list args_memory
		# n_args = length argv_list
		# argv = createArray (n_args+1) 0;
912 913 914
		= fill_argv 0 argv_list argv args_memory
	where
		fill_argv :: !Int ![{#Char}] !*{#Int} !Int -> *{#Int}
John van Groningen's avatar
John van Groningen committed
915 916 917 918 919 920 921 922
		fill_argv arg_n [a:as] argv args_memory
			# argv = {argv & [arg_n]=args_memory}
			  args_memory = args_memory + ((size a+4) bitand -4)
			= fill_argv (arg_n+1) as argv args_memory
		fill_argv arg_n [] argv args_memory
			= {argv & [arg_n]=0}

ApplicationOptionsToFlags :: !ApplicationOptions -> Int
923
ApplicationOptionsToFlags {sgc,pss,marking_collection,set,o,memoryProfiling,write_stderr_to_file,disable_rts_flags}
924
	= showgc+printstacksize+showexectime+cons+marking_collection_mask+memory_profiling_mask+write_stderr_to_file_mask+disable_rts_flags_mask
John van Groningen's avatar
John van Groningen committed
925
where
926
	showgc
John van Groningen's avatar
John van Groningen committed
927 928
		| sgc	= 2
				= 0
929
	printstacksize
John van Groningen's avatar
John van Groningen committed
930 931
		| pss	= 4
				= 0
932 933
	showexectime
		| set	= 8
John van Groningen's avatar
John van Groningen committed
934 935
				= 0
	write_stderr_to_file_mask
936 937 938 939
		| write_stderr_to_file	= 128
								= 0
	marking_collection_mask
		| marking_collection 	= 64
John van Groningen's avatar
John van Groningen committed
940 941
								= 0
	memory_profiling_mask
942
		| memoryProfiling	= 32
John van Groningen's avatar
John van Groningen committed
943 944 945 946 947 948 949 950
							= 0
	cons
		= case o of
			BasicValuesOnly		-> 1
			ShowConstructors	-> 0
			NoReturnType		-> 16
			NoConsole			-> 16

951 952 953 954
	disable_rts_flags_mask
		| disable_rts_flags	= 8192
							= 0

John van Groningen's avatar
John van Groningen committed
955 956 957 958 959
(FWI) infixl
(FWI) f i :== fwritei i f

write_options_file :: !{#.Char} !.Int !.Int !.Int !.Int !.Int !.Int !Bool !*Files -> *(!Bool,!*Files)
write_options_file options_file_name flags heap_size stack_size initial_heap_size heap_size_multiple min_write_heap_size use_64_bit_processor files
960
	# (opened,file,files)
John van Groningen's avatar
John van Groningen committed
961 962 963 964 965 966 967 968 969 970
		= fopen options_file_name FWriteData files
	| not opened
		= (False,files)
	#! file = IF_INT_64_OR_32
			(file FWI
			0x464c457f FWI 0x00010102 FWI 0x00000000 FWI 0x00000000 FWI
			0x003e0001 FWI 0x00000001 FWI 0x00000000 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000094 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000040 FWI 0x00400000 FWI 0x00040007 FWI

971
			heap_size FWI (heap_size>>32) FWI stack_size FWI (stack_size>>32) FWI
John van Groningen's avatar
John van Groningen committed
972
			flags      FWI 0x00000000 FWI heap_size_multiple FWI 00 FWI
973
			initial_heap_size FWI (initial_heap_size>>32) FWI
John van Groningen's avatar
John van Groningen committed
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
										  0x79732e00 FWI 0x6261746d FWI
			0x74732e00 FWI 0x62617472 FWI 0x68732e00 FWI 0x74727473 FWI
			0x2e006261 FWI 0x74786574 FWI 0x61642e00 FWI 0x2e006174 FWI
			0x00737362 FWI
						   0x00000000 FWI 0x00000000 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI 0x00000000 FWI
			0x00000000 FWI
						   0x0000001b FWI 0x00000001 FWI 0x00000006 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI 0x00000040 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000004 FWI 0x00000000 FWI 0x00000000 FWI
			0x00000000 FWI
						   0x00000021 FWI 0x00000001 FWI 0x00000003 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI 0x00000040 FWI
			0x00000000 FWI 0x00000028 FWI 0x00000000 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000004 FWI 0x00000000 FWI 0x00000000 FWI
			0x00000000 FWI
						   0x00000027 FWI 0x00000008 FWI 0x00000003 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI 0x00000068 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000004 FWI 0x00000000 FWI 0x00000000 FWI
997
			0x00000000 FWI
John van Groningen's avatar
John van Groningen committed
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
						   0x00000011 FWI 0x00000003 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI 0x00000068 FWI
			0x00000000 FWI 0x0000002c FWI 0x00000000 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000001 FWI 0x00000000 FWI 0x00000000 FWI
			0x00000000 FWI
						   0x00000001 FWI 0x00000002 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI 0x00000254 FWI
			0x00000000 FWI 0x000000d8 FWI 0x00000000 FWI 0x00000006 FWI
			0x00000004 FWI 0x00000004 FWI 0x00000000 FWI 0x00000018 FWI
			0x00000000 FWI
						   0x00000009 FWI 0x00000003 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI 0x0000032c FWI
			0x00000000 FWI 0x00000044 FWI 0x00000000 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000001 FWI 0x00000000 FWI 0x00000000 FWI
1012
			0x00000000 FWI
John van Groningen's avatar
John van Groningen committed
1013 1014 1015 1016
						   0x00000000 FWI 0x00000000 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI
														 0x00000000 FWI
			0x00010003 FWI 0x00000000 FWI 0x00000000 FWI 0x00000000 FWI
1017
			0x00000000 FWI
John van Groningen's avatar
John van Groningen committed
1018 1019
				           0x00000000 FWI 0x00020003 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI
1020
														 0x00000000 FWI
John van Groningen's avatar
John van Groningen committed
1021 1022 1023 1024 1025 1026
			0x00030003 FWI 0x00000000 FWI 0x00000000 FWI 0x00000000 FWI
			0x00000000 FWI
				           0x00000001 FWI 0x00020011 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI
														 0x0000000b FWI
			0x00020011 FWI 0x00000008 FWI 0x00000000 FWI 0x00000000 FWI
1027
			0x00000000 FWI
John van Groningen's avatar
John van Groningen committed
1028 1029
				           0x00000019 FWI 0x00020011 FWI 0x00000010 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI
1030
														 0x0000001f FWI
John van Groningen's avatar
John van Groningen committed
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
			0x00020011 FWI 0x00000018 FWI 0x00000000 FWI 0x00000000 FWI
			0x00000000 FWI
				           0x00000032 FWI 0x00020011 FWI 0x00000020 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI
														 0x61656800 FWI
			0x69735f70 FWI 0x6100657a FWI 0x74735f62 FWI 0x5f6b6361 FWI
			0x657a6973 FWI 0x616c6600 FWI 0x68007367 FWI 0x5f706165 FWI
			0x657a6973 FWI 0x6c756d5f FWI 0x6c706974 FWI 0x6e690065 FWI
			0x61697469 FWI 0x65685f6c FWI 0x735f7061 FWI 0x00657a69)

			(file FWI
			0x464c457f FWI 0x00010101 FWI 0x00000000 FWI 0x00000000 FWI
			0x00030001 FWI 0x00000001 FWI 0x00000000 FWI 0x00000000 FWI
			0x00000074 FWI 0x00000000 FWI 0x00000034 FWI 0x00280000 FWI
			0x00040007 FWI heap_size FWI stack_size FWI flags FWI
			heap_size_multiple FWI initial_heap_size FWI 0x79732e00 FWI 0x6261746d FWI
			0x74732e00 FWI 0x62617472 FWI 0x68732e00 FWI 0x74727473 FWI
			0x2e006261 FWI 0x74786574 FWI 0x61642e00 FWI 0x2e006174 FWI
			0x00737362 FWI 0x00000000 FWI 0x00000000 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI 0x0000001b FWI
			0x00000001 FWI 0x00000006 FWI 0x00000000 FWI 0x00000034 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI 0x00000004 FWI
			0x00000000 FWI 0x00000021 FWI 0x00000001 FWI 0x00000003 FWI
			0x00000000 FWI 0x00000034 FWI 0x00000014 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000004 FWI 0x00000000 FWI 0x00000027 FWI
			0x00000008 FWI 0x00000003 FWI 0x00000000 FWI 0x00000048 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI 0x00000004 FWI
			0x00000000 FWI 0x00000011 FWI 0x00000003 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000048 FWI 0x0000002c FWI 0x00000000 FWI
			0x00000000 FWI 0x00000001 FWI 0x00000000 FWI 0x00000001 FWI
			0x00000002 FWI 0x00000000 FWI 0x00000000 FWI 0x0000018c FWI
			0x00000090 FWI 0x00000006 FWI 0x00000004 FWI 0x00000004 FWI
			0x00000010 FWI 0x00000009 FWI 0x00000003 FWI 0x00000000 FWI
			0x00000000 FWI 0x0000021c FWI 0x00000044 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000001 FWI 0x00000000 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000000 FWI 0x00010003 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000000 FWI 0x00020003 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000000 FWI 0x00030003 FWI 0x00000001 FWI
			0x00000000 FWI 0x00000000 FWI 0x00020211 FWI 0x0000000b FWI
			0x00000004 FWI 0x00000000 FWI 0x00020211 FWI 0x00000019 FWI
			0x00000008 FWI 0x00000000 FWI 0x00020211 FWI 0x0000001f FWI
			0x0000000c FWI 0x00000000 FWI 0x00020211 FWI 0x00000032 FWI
			0x00000010 FWI 0x00000000 FWI 0x00020211 FWI 0x61656800 FWI
			0x69735f70 FWI 0x6100657a FWI 0x74735f62 FWI 0x5f6b6361 FWI
			0x657a6973 FWI 0x616c6600 FWI 0x68007367 FWI 0x5f706165 FWI
			0x657a6973 FWI 0x6c756d5f FWI 0x6c706974 FWI 0x6e690065 FWI
			0x61697469 FWI 0x65685f6c FWI 0x735f7061 FWI 0x00657a69)
1080
	# (close_ok,files)
John van Groningen's avatar
John van Groningen committed
1081 1082 1083 1084
		= fclose file files
	= (close_ok,files)

ClearCompilerCache :: .a
1085
ClearCompilerCache = abort "ClearCompilerCache\n"
John van Groningen's avatar
John van Groningen committed
1086 1087

ClearCompilerCaches :: .a
1088
ClearCompilerCaches = abort "ClearCompilerCaches\n"
John van Groningen's avatar
John van Groningen committed
1089 1090

SendRepeatResult :: .a
1091
SendRepeatResult = abort "SendRepeatResult\n"
John van Groningen's avatar
John van Groningen committed
1092 1093

StartCodeGenerator :: .a
1094
StartCodeGenerator = abort "StartCodeGenerator\n"
John van Groningen's avatar
John van Groningen committed
1095 1096

Execute` :: .a
1097
Execute` = abort "Execute`\n"
John van Groningen's avatar
John van Groningen committed
1098

1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
RunExternalCommand :: !String ![String] !String !*GeneralSt -> *(!Int, ![String], !*GeneralSt)
RunExternalCommand cmd args startupdir ps
	# temp_dir = temp_dir_path startupdir
	  errors_file_name = errors_file_path temp_dir 0
	# stderr_fd = creat (errors_file_name+++"\0") 0644
	| stderr_fd== (-1)
		= abort "creat failed\n"

	# (argv,args_memory) = make_argv [cmd : args]

	# pid = fork
	| pid<0
		= abort "fork failed\n"
	| pid==0
		# r=dup2 stderr_fd 2
		| r== (-1)
			= abort "dup2 failed\n"
		| execv (cmd+++"\0") argv<0
			= abort "execv failed\n"
			= abort "execution continued after execv\n"
	| free args_memory<0
		= abort "free failed\n"

	# (w_pid,status) = wait_pid pid 0
	| w_pid <> -1 && w_pid<>pid
		= abort "waitpid failed\n"
	# result = (status bitand 0xff00) >> 8
	# wtermsig = status bitand 0x7f
	| wtermsig<>0
		= abort "external command exited abnormally\n"

	# r=close stderr_fd
	| r==(-1)
		= abort "close failed\n"

	# (out,ps) = accFiles (readFileLines errors_file_name) ps

	= (result,out,ps)
where
	readFileLines :: !String !*Files -> *(![String], !*Files)
	readFileLines path env
	# (ok,f,env) = fopen path FReadText env
	| not ok = ([], env)
	# (out,f) = read [] f
	# (_,env) = fclose f env
	= (out,env)
	where
		read :: ![String] !*File -> *(![String], !*File)
		read acc f
		# (e,f) = fend f
		| e = (reverse acc,f)
		# (line,f) = freadline f
		#! line = strip_newlines line
		= read [line:acc] f

John van Groningen's avatar
John van Groningen committed
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
wait_pid :: !Int !Int -> (!Int,!Int)
wait_pid pid options
	# status_a = createArray 1 0
	# w_pid = waitpid pid status_a 0
	| w_pid==w_pid
		# status = status_a.[0]
		= (w_pid,status)
		# status = status_a.[0]
		= (w_pid,status)

S_IRUSR:==0x100
S_IWUSR:==0x080

mkstemp :: !{#Char} -> Int;
mkstemp temp_file_name = code {
	ccall mkstemp "s:I"
}

creat :: !{#Char} !Int -> Int;
creat string0 mode = code {
	ccall creat "sI:I"
}

open :: !{#Char} !Int !Int -> Int;
open path_name flags mode = code {
	ccall open "sII:I"
}

write :: !Int !{#Char} !Int -> Int;
write fd buffer count = code {
	ccall write "Isp:p"
}

read :: !Int !{#Char} !Int -> Int;
read fd buffer count = code {
	ccall read "Isp:p"
}

close :: !Int -> Int;
close fd = code {
	ccall close "I:I"
}

unlink :: !{#Char} -> Int;
unlink file_name = code {
	ccall unlink "s:I"
}

mkfifo :: !{#Char} !Int -> Int;
mkfifo name flags = code {
	ccall mkfifo "sI:I"
}

dup2 :: !Int !Int -> Int
dup2 oldfd newfd = code {
	ccall dup2 "II:I"
}

fork :: Int
fork = code {
	ccall fork ":I"
}

execv :: !{#Char} !{#Int} -> Int
execv program argv = code {
	ccall execv "sA:I"
}

waitpid :: !Int !{#Int} !Int -> Int
waitpid pid status_p options = code {
	ccall waitpid "IAI:I"
}

poll :: !{#Int} !Int !Int -> Int;
poll fds nfds timeout = code {
	ccall poll "AII:I"
}

malloc :: !Int -> Int
malloc s = code {
	ccall malloc "p:p"
}

free :: !Int -> Int
free p = code {
	ccall free "p:I"
}

memcpy_string_to_pointer :: !Int !{#Char} !Int -> Int
memcpy_string_to_pointer p s n = code {
	ccall memcpy "psp:p"
}