PmCleanSystem.icl 43.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,IF_ARM
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

:: 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

60 61
standardObjectFiles :: !Bool !Bool !Bool !Processor !Bool -> List String
standardObjectFiles stack_traces callgraph_profiling profiling _ use_64_bit_processor
John van Groningen's avatar
John van Groningen committed
62
	#! startup_file = if stack_traces "_startupTrace.o"
63 64
					  (if callgraph_profiling "_startupProfileGraph.o"
					  (if profiling "_startupProfile.o" "_startup.o"))
John van Groningen's avatar
John van Groningen committed
65 66 67 68
	= (startup_file :! "_system.o" :! Nil)

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

InitCompilingInfo :: *CompilingInfo
InitCompilingInfo = NoCompiler

Compile :: !String !Bool !Bool !(WindowFun *env) !(WindowFun *env) !CompileOrCheckSyntax !ModuleDirAndName !Pathname
75
	!(List Pathname) !ProjectCompilerOptions !CompilerOptions !Pathname !CompilerProcessIds !*env
John van Groningen's avatar
John van Groningen committed
76 77 78
	-> (!Pathname,!CompilerMsg,!CompilerProcessIds,!*env)
	| FileEnv env
Compile
79 80
	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
81 82 83 84 85 86 87
	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

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


	# 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
120
	!(List Pathname) !ProjectCompilerOptions !CompilerOptions !Pathname !*CompilingInfo !*env
John van Groningen's avatar
John van Groningen committed
121 122
	-> (!*CompilingInfo,!(!*env, !Pathname, !CompilerMsg))
	| FileEnv env
123 124
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
125 126 127
					startupdir compiling_info env
	# (cocl,cocl_dir,options1,options2) = get_path_name_and_options2 cocl startupdir

128 129
	# (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
130 131 132 133 134

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

135
	# args = makeCompilerOptionsArguments compileOrCheckSyntax write_module_times project_compiler_options co
John van Groningen's avatar
John van Groningen committed
136 137 138 139 140 141 142 143 144 145
	# 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
146
		= abort "write_failed\n"
John van Groningen's avatar
John van Groningen committed
147 148 149 150

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

	# exitcode = parse_result_number 0 result_string
154

John van Groningen's avatar
John van Groningen committed
155 156 157 158 159 160 161 162 163 164
	# 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
165
		= abort "parse_result_number failed\n"
166
where
John van Groningen's avatar
John van Groningen committed
167 168 169 170 171 172 173
	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
174 175
				= abort "parse_result_number2 failed\n"
			= abort "parse_result_number2 failed\n"
John van Groningen's avatar
John van Groningen committed
176 177 178

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

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

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

193
	# args = makeCompilerOptionsArguments compileOrCheckSyntax write_module_times project_compiler_options co
John van Groningen's avatar
John van Groningen committed
194 195 196 197 198 199 200 201 202 203
	# 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
204
		= abort "write_failed\n"
John van Groningen's avatar
John van Groningen committed
205 206 207 208 209 210 211
	= (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
212
		= abort "write_failed\n"
John van Groningen's avatar
John van Groningen committed
213 214
	# (w_pid,status) = wait_pid compiler_pid 0
	| w_pid <> -1 && w_pid<>compiler_pid
215
		= abort "waitpid failed\n"
John van Groningen's avatar
John van Groningen committed
216 217 218
	# result = (status bitand 0xff00) >> 8
	# wtermsig = status bitand 0x7f
	| wtermsig<>0
219
		= abort "compiler exited abnormally\n"
John van Groningen's avatar
John van Groningen committed
220 221
	# r=close commands_fd
	| r==(-1)
222
		= abort "close failed\n"
John van Groningen's avatar
John van Groningen committed
223 224
	# r=close results_fd
	| r==(-1)
225
		= abort "close failed\n"
John van Groningen's avatar
John van Groningen committed
226
	| unlink (commands_file_name+++"\0")<>0
227
		= abort "unlink failed\n"
John van Groningen's avatar
John van Groningen committed
228
	| unlink (results_file_name+++"\0")<>0
229
		= abort "unlink failed\n"
John van Groningen's avatar
John van Groningen committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243
	= 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
244
where
John van Groningen's avatar
John van Groningen committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
	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)
262
		= abort "creat failed\n"
John van Groningen's avatar
John van Groningen committed
263 264 265 266 267

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

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

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

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

	# ((_, errors_not_empty, error_text),ps) = accFiles (readErrorsAndWarnings errors_file_name) ps
	  ps = if errors_not_empty
295
				(wf (StrictListToList error_text) ps)
John van Groningen's avatar
John van Groningen committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
				(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)
320
		= abort "creat failed\n"
John van Groningen's avatar
John van Groningen committed
321 322 323 324 325

	# (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
326
		= abort "fork failed\n"
John van Groningen's avatar
John van Groningen committed
327 328 329
	| code_generator_pid==0
		# r=dup2 stderr_fd 2
		| r== (-1)
330
			= abort "dup2 failed\n"
John van Groningen's avatar
John van Groningen committed
331
		| execv (cgen+++"\0") argv<0
332 333
			= abort "execv failed\n"
			= abort "execution continued after execv\n"
John van Groningen's avatar
John van Groningen committed
334
	| free args_memory<0
335
		= abort "free failed\n"
John van Groningen's avatar
John van Groningen committed
336 337 338 339 340 341 342 343
	# 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
344
		= abort "waitpid failed\n"
John van Groningen's avatar
John van Groningen committed
345 346 347 348
	# process_n = find_process_id_index 0 w_pid process_ids
	# result = (status bitand 0xff00) >> 8
	# wtermsig = status bitand 0x7f
	| wtermsig<>0
349
		= abort "code generator exited abnormally\n"
John van Groningen's avatar
John van Groningen committed
350
	= (process_n,result,ps)
351
where
John van Groningen's avatar
John van Groningen committed
352 353 354 355 356 357 358 359 360 361
	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)
362
		= abort "close failed\n"
John van Groningen's avatar
John van Groningen committed
363 364
	# ((_, errors_not_empty, error_text),ps) = accFiles (readErrorsAndWarnings scg_errors_file_name) ps
	  ps = if errors_not_empty
365
				(wf (StrictListToList error_text) ps)
John van Groningen's avatar
John van Groningen committed
366 367 368 369 370 371
				(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
Jurrien Stutterheim's avatar
Jurrien Stutterheim committed
372
		!Pathname !(List Pathname) !(List Pathname) !(List Pathname) !Bool !Bool !Bool !Bool !Bool !String
John van Groningen's avatar
John van Groningen committed
373 374 375 376
		!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}
Jurrien Stutterheim's avatar
Jurrien Stutterheim committed
377
		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
378
		link_resources resource_path gen_dll dll_syms startupdir dynlstr _ use_64_bit_processor ps
379
	# (calling_convention,linker) = get_optional_calling_convention linker
380 381
	# (linker,linker_dir,options12) = get_path_name_and_options linker startupdir
	# (options1,options2) = splitOptions options12
John van Groningen's avatar
John van Groningen committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395
	# 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
396
	# object_paths = [optionspathname : StrictListToList (RemoveDup object_file_names)]
397
	| size calling_convention==0
398 399 400 401
		# 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"
402
			= linkGCC linker object_paths options2 ps
403 404
			= linkOther linker object_paths ps
	| calling_convention=="gcc"
405
		= linkGCC linker object_paths options2 ps
406
	| calling_convention=="linker"
Haye Böhm's avatar
Haye Böhm committed
407
		= linkOther linker object_paths ps
408 409 410
	| calling_convention=="linker+gcc"
		= linkOptimizingGCC linker object_paths options1 options2 ps
		= (winfun ["Linker error: Unknown calling convention : "+++calling_convention] ps,False)
Haye Böhm's avatar
Haye Böhm committed
411 412 413
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
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
		# 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
448
		#! (ps, link_ok) = linkGCC "/usr/bin/gcc" [temp_file_name] options2 ps
449 450 451
		| unlink (temp_file_name+++"\0")<>0
			= (ps,link_ok)
			= (ps,link_ok)
Haye Böhm's avatar
Haye Böhm committed
452 453

	//Just link with gcc
454
	linkGCC gcc_file_name object_paths gccOptions ps
455 456
		# ld_args = add_options_string_to_args 0 gccOptions (StrictListToList (Append library_file_names "-lm"))
		# ld_args = ["-o", path : object_paths ] ++ ld_args
457 458
		  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]
459
		# (argv,args_memory) = make_argv [gcc_file_name:ld_args]
460 461 462 463
		# ld_pid = fork
		| ld_pid<0
			= abort "fork failed\n"
		| ld_pid==0
464
			| execv (gcc_file_name+++"\0") argv<0
465 466 467 468 469 470 471 472 473 474 475 476 477
				= 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
478 479 480

	//Call other/additional linker
	linkOther linker object_paths ps
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 514 515 516 517 518 519 520 521 522 523
		# 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)
524

John van Groningen's avatar
John van Groningen committed
525 526 527 528 529 530 531 532 533 534 535 536 537
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
538
		= abort "poll failed\n"
John van Groningen's avatar
John van Groningen committed
539 540 541 542
	| r==0
		= (NoFinishedCompiler,ps)
		# compiler_id = find_fd_index 0 fda
		# results_fd = (compiler_process_ids !! compiler_id).results_fd
543

John van Groningen's avatar
John van Groningen committed
544 545 546
		# result_string = createArray 6 '\0'
		# r = read results_fd result_string 6;
		| r<=1
547
			= abort ("read failed "+++toString r+++ "\n")
John van Groningen's avatar
John van Groningen committed
548
		# exit_code = parse_result_number 0 result_string
549

John van Groningen's avatar
John van Groningen committed
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
		= (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
568
				= abort ("polling file descriptors failed with event"+++toString r_events+++"\n")
John van Groningen's avatar
John van Groningen committed
569 570 571 572 573

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

574 575
start_compiler :: !{#Char} !{#Char} -> *CompilerProcess
start_compiler compiler_file_name initial_args
John van Groningen's avatar
John van Groningen committed
576 577 578
	# commands_file_name = "/tmp/comXXXXXX" +++ "\0" // +++ because modified by mkstemp
	# fd = mkstemp commands_file_name
	| fd== -1
579
		= abort "mkstemp failed\n"
John van Groningen's avatar
John van Groningen committed
580 581
	# r = close fd
	| r== -1
582
		= abort "close failed\n"
John van Groningen's avatar
John van Groningen committed
583 584
	# r = unlink commands_file_name
	| r== -1
585
		= abort "unlink failed\n"
John van Groningen's avatar
John van Groningen committed
586 587
	# r = mkfifo commands_file_name (S_IRUSR bitor S_IWUSR)
	| r== -1
588
		= abort "mkfifo failed\n"
John van Groningen's avatar
John van Groningen committed
589 590 591 592

	# results_file_name = "/tmp/resXXXXXX" +++ "\0" // +++ because modified by mkstemp
	# fd = mkstemp results_file_name
	| fd== -1
593
		= abort "mkstemp failed\n"
John van Groningen's avatar
John van Groningen committed
594 595
	# r = close fd
	| r== -1
596
		= abort "close failed\n"
John van Groningen's avatar
John van Groningen committed
597 598
	# r = unlink results_file_name
	| r== -1
599
		= abort "unlink failed\n"
John van Groningen's avatar
John van Groningen committed
600 601
	# r = mkfifo results_file_name (S_IRUSR bitor S_IWUSR)
	| r== -1
602
		= abort "mkfifo failed\n"
John van Groningen's avatar
John van Groningen committed
603

604
	# 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
605 606 607 608 609

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

	# compiler_pid = fork
	| compiler_pid<0
610
		= abort "fork failed\n"
John van Groningen's avatar
John van Groningen committed
611 612
	| compiler_pid==0
		| execv (compiler_file_name+++"\0") argv<0
613 614
			= abort "execv failed\n"
			= abort "execution continued after execv\n"
John van Groningen's avatar
John van Groningen committed
615
	| free args_memory<0
616
		= abort "free failed\n"
John van Groningen's avatar
John van Groningen committed
617 618 619

	# commands_fd = open (commands_file_name+++"\0") O_WRONLY 0
	| commands_fd == -1
620
		= abort "open failed\n"
John van Groningen's avatar
John van Groningen committed
621 622
	# results_fd = open (results_file_name+++"\0") O_RDONLY 0
	| results_fd == -1
623
		= abort "open failed\n"
John van Groningen's avatar
John van Groningen committed
624 625 626 627

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

628 629 630 631
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
632 633
	= compiling_info

634
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
635 636
	| slot<length compiler_process_ids
		= (compiler_process_ids !! slot,compiler_process_ids)
637
		# compiler_process = start_compiler compiler_file_name initial_args
John van Groningen's avatar
John van Groningen committed
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
		# 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

731 732 733 734 735 736 737 738 739 740
get_optional_calling_convention :: !{#Char} -> (!{#Char},!{#Char})
get_optional_calling_convention s
	# s_size = size s;
	| s_size>0 && s.[0]==':'
		# next_c = FindColonChar 1 s s_size
		| next_c<s_size
			= (s % (1,next_c-1),s % (next_c+1,s_size));
			= ("",s);
		= ("",s);

John van Groningen's avatar
John van Groningen committed
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
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
760 761
	  first_c = FindColonChar 0 str len_str
	| first_c >= len_str
John van Groningen's avatar
John van Groningen committed
762
		= (str,"")
763 764
		# first_str = str % (0,dec first_c)
		  last_str = str % (first_c+1, len_str)
John van Groningen's avatar
John van Groningen committed
765 766
		= (first_str,last_str)

767 768
FindColonChar :: !Int !String !Int -> Int;
FindColonChar pos line linelen
769
	| pos<linelen && line.[pos]<>':'
770 771
		= FindColonChar (pos+1) line linelen;
		= pos;
John van Groningen's avatar
John van Groningen committed
772

773
add_options_string_to_args :: !Int !{#Char} ![{#Char}] -> [{#Char}]
John van Groningen's avatar
John van Groningen committed
774 775 776 777 778 779
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]
780
where
John van Groningen's avatar
John van Groningen committed
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
	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

797
compiler_arguments :: !String !String !CompileOrCheckSyntax !Pathname !(List Pathname) !Bool !ProjectCompilerOptions !CompilerOptions -> [String]
John van Groningen's avatar
John van Groningen committed
798
compiler_arguments out_file_name errors_file_name compileOrCheckSyntax path paths
799 800
					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
801 802 803 804 805 806
	= 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

807
makeCompilerOptionsArguments :: !CompileOrCheckSyntax !Bool !ProjectCompilerOptions !CompilerOptions -> [String]
808
makeCompilerOptionsArguments compileOrCheckSyntax write_module_times
809
		{pco_memory_profiling,pco_time_profiling,pco_callgraph_profiling,pco_desc_exl,pco_dynamics,pco_generic_fusion,pco_link_dynamic}
810
		{neverMemoryProfile,neverTimeProfile,sa,gw,gc,listTypes,attr,reuseUniqueNodes,fusion}
811
	= write_module_times_arg ++ checksyntax ++ timeProfileSwitch ++ callgraphProfileSwitch ++ memoryProfileSwitch ++ descExlSwitch ++ dynamicsSwitch
John van Groningen's avatar
John van Groningen committed
812 813 814 815 816 817 818
		++ strictness ++ warnings ++ comments ++ listtypes ++ show_attr ++ reuse ++ fusion_arg
where
	write_module_times_arg
		| write_module_times
			= ["-wmt"]
			= []
	memoryProfileSwitch
819
		| (not neverMemoryProfile && pco_memory_profiling) || pco_desc_exl || pco_link_dynamic
John van Groningen's avatar
John van Groningen committed
820 821 822
			= ["-desc"]
			= []
	timeProfileSwitch
823
		| not neverTimeProfile && pco_time_profiling
John van Groningen's avatar
John van Groningen committed
824 825
			= ["-pt"]
			= []
826 827 828 829
	callgraphProfileSwitch
		| not neverTimeProfile && pco_callgraph_profiling
			= ["-pg"]
			= []
830 831
	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
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
	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
865
		= case fusion of FusionOn -> ["-fusion"]; FusionDefault | pco_generic_fusion -> ["-generic_fusion"]; _ -> []
John van Groningen's avatar
John van Groningen committed
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883

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
884

John van Groningen's avatar
John van Groningen committed
885 886 887 888 889 890 891 892
	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
893

John van Groningen's avatar
John van Groningen committed
894 895 896 897 898 899 900 901 902 903
	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
904
		= abort "malloc failed\n"
John van Groningen's avatar
John van Groningen committed
905 906 907 908 909 910 911 912 913 914 915 916
	# 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
917 918 919 920
	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
921 922
		copy_args [] i s
			= s
923

John van Groningen's avatar
John van Groningen committed
924 925 926 927 928 929
		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
930

John van Groningen's avatar
John van Groningen committed
931 932 933
	create_argv argv_list args_memory
		# n_args = length argv_list
		# argv = createArray (n_args+1) 0;
934 935 936
		= 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
937 938 939 940 941 942 943 944
		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
945
ApplicationOptionsToFlags {sgc,pss,marking_collection,set,o,memoryProfiling,write_stderr_to_file,disable_rts_flags}
946
	= 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
947
where
948
	showgc
John van Groningen's avatar
John van Groningen committed
949 950
		| sgc	= 2
				= 0
951
	printstacksize
John van Groningen's avatar
John van Groningen committed
952 953
		| pss	= 4
				= 0
954 955
	showexectime
		| set	= 8
John van Groningen's avatar
John van Groningen committed
956 957
				= 0
	write_stderr_to_file_mask
958 959 960 961
		| write_stderr_to_file	= 128
								= 0
	marking_collection_mask
		| marking_collection 	= 64
John van Groningen's avatar
John van Groningen committed
962 963
								= 0
	memory_profiling_mask
964
		| memoryProfiling	= 32
John van Groningen's avatar
John van Groningen committed
965 966 967 968 969 970 971 972
							= 0
	cons
		= case o of
			BasicValuesOnly		-> 1
			ShowConstructors	-> 0
			NoReturnType		-> 16
			NoConsole			-> 16

973 974 975 976
	disable_rts_flags_mask
		| disable_rts_flags	= 8192
							= 0

John van Groningen's avatar
John van Groningen committed
977 978 979 980 981
(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
982
	# (opened,file,files)
John van Groningen's avatar
John van Groningen committed
983 984 985 986 987 988
		= 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
989
			(IF_ARM 0x00b70001 0x003e0001) FWI 0x00000001 FWI 0x00000000 FWI 0x00000000 FWI
John van Groningen's avatar
John van Groningen committed
990 991 992
			0x00000000 FWI 0x00000000 FWI 0x00000094 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000040 FWI 0x00400000 FWI 0x00040007 FWI

993
			heap_size FWI (heap_size>>32) FWI stack_size FWI (stack_size>>32) FWI
John van Groningen's avatar
John van Groningen committed
994
			flags      FWI 0x00000000 FWI heap_size_multiple FWI 00 FWI
995
			initial_heap_size FWI (initial_heap_size>>32) FWI
John van Groningen's avatar
John van Groningen committed
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
										  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
1019
			0x00000000 FWI
John van Groningen's avatar
John van Groningen committed
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
						   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
1034
			0x00000000 FWI
John van Groningen's avatar
John van Groningen committed
1035 1036 1037 1038
						   0x00000000 FWI 0x00000000 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI
														 0x00000000 FWI
			0x00010003 FWI 0x00000000 FWI 0x00000000 FWI 0x00000000 FWI
1039
			0x00000000 FWI
John van Groningen's avatar
John van Groningen committed
1040 1041
				           0x00000000 FWI 0x00020003 FWI 0x00000000 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI
1042
														 0x00000000 FWI
John van Groningen's avatar
John van Groningen committed
1043 1044 1045 1046 1047 1048
			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
1049
			0x00000000 FWI
John van Groningen's avatar
John van Groningen committed
1050 1051
				           0x00000019 FWI 0x00020011 FWI 0x00000010 FWI
			0x00000000 FWI 0x00000000 FWI 0x00000000 FWI
1052
														 0x0000001f FWI
John van Groningen's avatar
John van Groningen committed
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 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
			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)
1102
	# (close_ok,files)
John van Groningen's avatar
John van Groningen committed
1103 1104 1105 1106
		= fclose file files
	= (close_ok,files)

ClearCompilerCache :: .a
1107
ClearCompilerCache = abort "ClearCompilerCache\n"
John van Groningen's avatar
John van Groningen committed
1108 1109

ClearCompilerCaches :: .a
1110
ClearCompilerCaches = abort "ClearCompilerCaches\n"
John van Groningen's avatar
John van Groningen committed
1111 1112

SendRepeatResult :: .a
1113
SendRepeatResult = abort "SendRepeatResult\n"
John van Groningen's avatar
John van Groningen committed
1114 1115

StartCodeGenerator :: .a
1116
StartCodeGenerator = abort "StartCodeGenerator\n"
John van Groningen's avatar
John van Groningen committed
1117 1118

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

Camil Staps's avatar
Camil Staps committed
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 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
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
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 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
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"
}