-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcompile.pro
More file actions
executable file
·979 lines (869 loc) · 33.7 KB
/
compile.pro
File metadata and controls
executable file
·979 lines (869 loc) · 33.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
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
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
%-*-Prolog-*-
% compile indented on 12/23/2001 by 'JOLI' 1.0.
%------------------------------------------------------------------------
%
% compile.pro -- control flow for procedure compiler
%
% Copyright (c) 1992-2012 Amzi! inc. All Rights Reserved.
%
%------------------------------------------------------------------------
:- module(amzi_compiler).
:- import(amzi_register).
:- end_module(amzi_compiler).
:- body(amzi_compiler).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% compile one file %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% InFile is the source file, Outfile is the listing file (if any) and
% CodeFile is the generated object file
% Note that this can be called from another program, not
% cmain.pro, in which case the listing option is turned off
% by making OutFile = null.
% NOTE This requires aosutils to be loaded. THIS IS AN ERROR.
% This is only used by Eclipse and is bad form and should be rewritten.
xcompile(Dir, InFile, CodeFile, OutFile) :-
curdir(CurDir),
(chdir(Dir) ->
true
;
throw(comperr(`Unable to change directory`, Dir)) ),
xcompile(InFile, CodeFile, OutFile).
% This throw does not work. Wraps error in an error structure instead of
% maintaining its original pieces that can be retrieved via ls.GetLineno, etc.
% catch( xcompile(InFile, CodeFile, OutFile), Err, (chdir(CurDir), throw(Err)) ),
% chdir(CurDir).
xcompile(InFile, CodeFile, OutFile) :-
%buginit,
(OutFile = null -> retractall(list_on) ; assert(list_on)),
%retractall(wamout(_)),
abolish(wamout/1),
compile(InFile, CodeFile, OutFile),
clean_house.
compile(InFile, CodeFile, OutFile) :-
clean_house,
% abolish(input_clause/1),
% retractall(last_fa(_)),
read_input_file(InFile),
compile2(CodeFile, OutFile).
clean_house :-
abolish(input_clause/1),
abolish(last_fa/1),
abolish(is$dynamic/1),
abolish(ops_defined/0).
% NOTE This requires aosutils to be loaded. THIS IS AN ERROR.
% This is only used by Eclipse and is bad form and should be rewritten.
debug_compile(Dir, InFile, CodeFile, OutFile) :-
curdir(CurDir),
(chdir(Dir) ->
true
;
throw(comperr(`Unable to change directory`, Dir)) ),
debug_compile(InFile, CodeFile, OutFile).
% This throw does not work. Wraps error in an error structure instead of
% maintaining its original pieces that can be retrieved via ls.GetLineno, etc.
% catch( debug_compile(InFile, CodeFile, OutFile), Err, (chdir(CurDir), throw(Err)) ),
% chdir(CurDir).
debug_compile(InFile, CodeFile, OutFile) :-
abolish(input_clause/1),
retractall(last_fa(_)),
amzi_system:debug_compile_read(InFile),
compile2(CodeFile, OutFile).
%------------------------------------------------------------------
% First read all of the clauses and store them in input_clause/1
% clauses. These are then backtracked through for the main compile
% loop. This allows us to use alternate readers for processing the
% input file.
%
/*
read_input_file(InFile) :-
(open(InFile, read, InHndl) ; file_cannot_open(InFile)),
repeat,
read(InHndl, Clause),
( Clause = (:-(include(File2a))) ->
tilt_slashes(File2a,File2),
read_input_file(File2)
;
true ),
amzi_system:check$term(Clause),
add_clause(Clause),
Clause == end_of_file,
!,
close(InHndl).
*/
read_input_file(InFile) :-
(open(InFile, read, InHndl) ; file_cannot_open(InFile)),
repeat,
read(InHndl, Clause),
( Clause = (:-(include(File2a))) ->
tilt_slashes(File2a,File2),
read_input_file(File2)
;
amzi_system:check$term(Clause),
add_clause(Clause) ),
Clause == end_of_file,
!,
close(InHndl).
/*
read_input_file(InFile) :-
(open(InFile, read, InHndl) ; file_cannot_open(InFile)),
repeat,
read(InHndl, Clause),
( Clause = (:-(include(File2a))) ->
tilt_slashes(File2a,File2),
read_input_file(File2)
;
true ),
(Clause == end_of_file ->
true
;
amzi_system:check$term(Clause),
add_clause(Clause),
fail ),
!,
close(InHndl).
*/
add_clause(Clause) :-
get_fa(Clause, FA),
( FA == directive -> reader_directive(Clause); true),
( (predicate_end(FA, OLD_FA), OLD_FA \= end_of_file/0) ->
assert( input_clause(end_of_predicate(OLD_FA)) )
;
true ),
assert( input_clause(Clause) ).
% directives that need to be dealt with now because they
% affect the reader
reader_directive(:-(op(A, B, C))) :-
!,
op(A, B, C).
reader_directive(:-(set_prolog_flag(F, V))) :-
!,
set_prolog_flag(F, V).
reader_directive(_).
get_fa( (:- _), directive) :-
!.
get_fa( (Head :- _), F/A ) :-
!,
functor(Head, F, A).
get_fa( (Head --> _), F/A) :-
!,
functor(Head, F, A1),
A is A1 + 2.
get_fa( Head, F/A ) :-
functor(Head, F, A).
% not a new one, same as last one
predicate_end(FA, _) :-
last_fa(FA),
!,
fail.
% there was an old one and its different
predicate_end(FA, OLD_FA) :-
retract(last_fa(OLD_FA)),
!,
assert(last_fa(FA)),
OLD_FA \= directive.
% there wasn't an old one, so this is first clause
predicate_end(FA, _) :-
assert(last_fa(FA)),
!,
fail.
%--------------------------------------------------
%compile(InFile, CodeFile, OutFile) :-
compile2(CodeFile, OutFile) :-
(list_on -> (tell(OutFile) ; file_cannot_open(OutFile)) ; true),
%(see(InFile) ; file_cannot_open(InFile)),
%(fopen(InHndl, InFile, r) ; file_cannot_open(InFile)),
% (open(InFile, read, InHndl) ; file_cannot_open(InFile)),
retractall(dyn$fact(_, _)),
retractall(is$discontiguous(_:_/_)),
retractall(is$dynamic(_:_/_)),
retractall(ops_defined), !,
%(fleopen(CodeHndl, CodeFile, wb) ; file_cannot_open(CodeFile)),
(
open(CodeFile, write, CodeHndl, [type(binary)]) ;
file_cannot_open(CodeFile)
),
reserve$(AssemblePoint), % to stash code for assembling
reserve$(ClausePoint), % to stash cross clause info
reserve$(CurrentProc), % to stash current proc info
write_code_header(CodeHndl),
Time1 is cputime,
process_clauses(OutFile, CodeHndl, InHndl, AssemblePoint, ClausePoint,
CurrentProc),
Time2 is cputime, !,
fseek(CodeHndl, 0, 1, NumBytes), % seek to EOF for size
write_l($\n[CodeSize $),
write_l(NumBytes),
write_l($ Bytes, Compile time $),
Time is Time2 - Time1,
write_l(Time),
write_l($ seconds.]\n$),
(list_on -> told ; true),
fseek(CodeHndl, 0, 2, _), % add two 0s at end for VMS bug
flewrite(CodeHndl, 0, 1), %fclose(CodeHndl),
close(CodeHndl),
stash$free(AssemblePoint),
stash$free(ClausePoint),
stash$free(CurrentProc),
seen,
!.
compile2(_, _) :-
report(`Compiler failed\n`).
% The compiler proper. In order to compile VERY large procedures
% (potentially hundreds of clauses) we take a different approach from
% that in versions 1.0X and 1.1X.
%
% We compile a single clause at a time and write it out. We then go
% back at the end of the procedure (procedures must still be contiguous)
% And add any hash tables required. This means that we have to compile
% in some redundancies that we can let the linker and loader resolve:
%
% The first clause of a procedure has to begin with
% switch A, B, C
% try_me_else
%
% And succeeding clauses have to begin with
% retry_me_else
%
% We then CLOSE the procedure at the end. This means we
% Fill in all the correct addresses for the inserted
% (re)try_me_else
%
% Add any hash tables required and fix up the initial
% switch A, B, C
%
% In order to acomplish this we need to track certain information
% for each clause in the procedure - this information has to be available
% to the procedure closer, so we call this cross clause info. In particular
% we need
%
% The physical disk address of the first code bye in each clause
% of the procedure - so we can get back during the link up phase
%
% The number of the clause to be used as a label for branching
%
% The first argument of each clause (if there is one) for hash table
% generation
%
% The inserted switch and first try_me_else are initially written out
% as 0s. Then if there is more than one clause (and the procedure closer
% decides that the switch is needed) the 0 are overwritten with proper code.
%
% This means that the loader and linker can decide at load time where the real
% code starts (it will either be at the switch, at the try_me_else or at the
% next op code if there is only one clause. Moreover since this possible
% trimming takes place at the prefix to the code sgment, a simple linear
% translation on all the op codes with a code address in them will suffice.
% Once the code has been loade in this fashion it is indistinguishable from
% any other code
process_clauses(OutFile, CodeHndl, InHndl, AP, CP, CurrProc) :-
cntr_set(1, 1), % counter 1 is used to track clauses
cntr_set(3, -1), % counter 3 tracks labels, negatives during compile
% repeat,
% read(InHndl, Clause),
input_clause(Clause),
% write( input_clause(Clause) ), nl,
amzi_system:check$term(Clause),
/*
( Clause == end_of_file -> % Link up the last procedure
close_procedure(CodeHndl, CP, CurrProc)
;
( Clause = (:-(include(File2a))) ->
tilt_slashes(File2a, File2),
( open(File2, read, InHndl2) ->
process_clauses(OutFile, CodeHndl, InHndl2, AP, CP, CurrProc),
close(InHndl2),
fail
;
file_cannot_open(InFile) )
;
*/
compile_a_clause(Clause, OutFile, InHndl, CodeHndl, AP, CP, CurrProc),
!.
process_clauses(_,_,_,_,_,_).
compile_a_clause(end_of_file, OutFile, InHndl, CodeHndl, AP, CP, CurrProc) :-
!, fail.
compile_a_clause(end_of_predicate(N/A), OutFile, InHndl, CodeHndl, AP, CP, CurrProc) :-
check_dynamic(N/A), % was really a latent in that case
!, fail.
compile_a_clause(end_of_predicate(N/A), OutFile, InHndl, CodeHndl, AP, CP, CurrProc) :-
( (loading_module(M), clause(is$discontiguous(M:N/A), true)) ->
functor(F, N, A),
compile_clause(N/A, _, (F :- fail), OutFile, CodeHndl, AP, CP, CurrProc)
;
true
),
% write(closing_predicate(N/A)), nl,
close_procedure(CodeHndl, CP, CurrProc),
!,
fail.
compile_a_clause(Clause, OutFile, InHndl, CodeHndl, AP, CP, CurrProc) :-
filter_clause(Clause, FC, N/A, FArg),
/* peek$(CurrProc, CPI),
( (CPI == []; CPI == [latent_exp/0]; CPI == [latent_opdef/0]) -> % First clause of predicate
stash$(CurrProc, (N/A))
;
/.*
% If compiling a new proc or proc is a latent
% expression then close and Link the old proc
( (CPI \== [N/A]; CPI == [latent_exp/0]; CPI == [latent_opdef/0]) ->
close_procedure(CodeHndl, CP, CurrProc),
X = newProc,
stash$(CurrProc, (N/A)) % And assert new current proc
;
*./
true % not a new proc
),
*/
((loading_module(M), clause(is$discontiguous(M:N/A), true)) -> % check directives
/*
%fseek(InHndl, 0, 1, BookMark), % discontiguous or multifile
stream_property(InHndl, position(BookMark)),
read(InHndl, NextClause), % look ahead
( NextClause \= end_of_file ->
%fseek(InHndl, BookMark, 0, BookMark)
set_stream_position(InHndl, BookMark)
;
true
), % go back
filter_clause(NextClause, _, N1/A1, _),
( (N1/A1 \= N/A) -> % is it same proc?
functor(F, N, A), % no, this is last, compile extra clause
compile_clause(N/A, _, (F :- fail), OutFile, CodeHndl, AP, CP, CurrProc)
;
*/
((cntr_get(1,ClauseN), ClauseN > 1000) ->
functor(F, N, A), % no, this is last, compile extra clause
compile_clause(N/A, _, (F :- fail), OutFile, CodeHndl, AP, CP, CurrProc),
close_procedure(CodeHndl, CP, CurrProc),
cntr_set(1,1)
;
true )
;
true ),
compile_clause(N/A, FArg, FC, OutFile, CodeHndl, AP, CP, CurrProc),
((N/A == latent_exp/0; N/A == latent_opdef/0) ->
close_procedure(CodeHndl, CP, CurrProc)
;
true ),
!, fail.
% Compile clauses - stash code at AP
compile_clause(N/A, FArg, Clause, OutFile, _, AP, CP, CurrProc) :-
% Set current proc if first clause of proc
(peek$(CurrProc, [(N/A)]) -> true ; stash$(CurrProc, (N/A))),
compileclause(Clause, Code - L),
L = [],
stash$(AP, Code),
fail. % Now assemble and we're done
compile_clause(N/A, FArg, _, _, CodeHndl, AP, CP, _) :-
peek$(CP, CPInfo),
is_it_first(CPInfo, FirstClause),
get$(AP, [Code]),
output(CodeHndl, Code, N/A, FirstClause, PhysicalP),
% Now update cross clause info
cntr_inc(1, Label), % Get clause # and increment
stash$(CP, cpinfo(FArg, Label, PhysicalP)), !.
is_it_first([], yes).
is_it_first([_|_], no).
/*********************************************************************
** filter a clause -- source - source pre translations
*********************************************************************/
filter_clause(?-(X), _, _, _) :-
(call(X) ; true), !, % ?- is a compile time directive, it is not compiled
fail.
/* :- compiles to a latent expression.
** it is not executed at compile time, except when the body is:
** an op() directive,
** a dynamic directive,
** import/export
*/
filter_clause(:-(nonterminal(NonTerminalList)), _, _, _) :-
add_nonterminal_list(NonTerminalList), !,
fail.
filter_clause(:-(dynamic(DynamicList)), _, _, _) :-
add_dynamic_list(DynamicList),
!,
fail.
filter_clause(:-(noNonTerminals), _, _, _) :-
sys$assertz('{sys}no$nt'), !,
fail.
filter_clause(:-(discontiguous(DL)), (latent_exp :- amzi_system:set$discontiguous(DL)),
latent_exp/0, _) :-
add_discontiguous_list(DL), !.
filter_clause(:-(multifile(DL)), (latent_exp :- amzi_system:set$discontiguous(DL)),
latent_exp/0, _) :-
add_discontiguous_list(DL), !.
filter_clause(:-(sorted(DL)), (latent_exp :- amzi_system:set$sorted(DL)), latent_exp/0,
_) :-
add_dynamic_list(DL), !.
filter_clause(:-(indexed(DL)), (latent_exp :- amzi_system:set$$indexed(DL)),
latent_exp/0, _) :-
add_dynamic_list(DL), !.
filter_clause(:-(op(A, B, C)), (latent_opdef :- op(A, B, C)),
latent_opdef/0, _) :- !,
op(A, B, C),
(clause(ops_defined) -> true ; assert(ops_defined)).
filter_clause(:-(set_prolog_flag(F, V)), (latent_exp :- set_prolog_flag(F, V)),
latent_exp/0, _) :- !,
set_prolog_flag(F, V).
% Need to set up the module stuff as we go, so that the pretranslate
% routines for metapredicates will work, see cclause, xpretrans.
filter_clause(:-(module(M)), (latent_exp :- module$(M)), latent_exp/0, _) :-
module$(M), !.
filter_clause(:-(end_module(M)), (latent_exp :- end_module$(M)),
latent_exp/0, _) :-
end_module$(M), !.
filter_clause(:-(body(M)), (latent_exp :- module$(M)), latent_exp/0, _) :-
module$(M), !.
filter_clause(:-(end_body(M)), (latent_exp :- end_module$(M)),
latent_exp/0, _) :-
end_module$(M), !.
filter_clause(:-(metapredicate(MI)), (latent_exp :- amzi_system:meta$assert(MI)),
latent_exp/0, _) :-
amzi_system:meta$assert(MI), !.
filter_clause(:-(Body), (latent_exp :- Body), latent_exp/0, _) :- !.
filter_clause((A --> B), DCG, Name/Ar, FArg) :- % use DCG compiler
expand_term((A --> B), DCG),
functor(A, Name, Arity),
first_arg(A, FArg),
Ar is Arity + 2, !.
filter_clause((H :- B), (latent_exp :- Body), latent_exp/0, _) :- % reeves
functor(H, N, A),
check_dynamic(N/A),
%clause(is$dynamic(N/A), true),
Body = assertz((H :- B)), !.
filter_clause((H :- B), (H :- B), N/A, FArg) :-
functor(H, N, A),
first_arg(H, FArg), !.
filter_clause(H, (latent_exp :- Body), latent_exp/0, _) :- % reeves
functor(H, N, A),
check_dynamic(N/A),
%clause(is$dynamic(N/A), true),
Body = assertz(H), !.
filter_clause(H, H, N/A, FArg) :-
functor(H, N, A),
first_arg(H, FArg).
% first_arg extracts the first argument from the clause - atomic
% terms and variables are themselves, functors are represented as
% N/A where a list is '.'/2
first_arg(Head, FArg) :-
Head =.. [_, Arg1|_],
farg(Arg1, FArg), !.
first_arg(_, _).
farg(Arg1, _) :-
var(Arg1), !.
farg(Arg1, Arg1) :-
atomic(Arg1), !.
farg([_|_], '.'/2) :- !.
farg(Arg1, Struc/Arity) :-
functor(Arg1, Struc, Arity).
add_discontiguous_list(N/A) :-
atom(N),
integer(A), !,
loading_module(M),
( clause(is$discontiguous(M:N/A), true) ->
true
;
assert(is$discontiguous(M:N/A)) ).
add_discontiguous_list([]).
add_discontiguous_list([H|T]) :-
add_discontiguous_list(H),
add_discontiguous_list(T).
add_discontiguous_list((A, B)) :-
add_discontiguous_list(A),
add_discontiguous_list(B).
add_discontiguous_list(X) :-
throw(comperr(badglobal, X)).
check_discontiguous(N/A) :-
loading_module(M),
clause(is$discontiguous(M:N/A), true),
!.
add_dynamic_list([]).
add_dynamic_list(N/A) :-
atom(N),
integer(A), !,
loading_module(M),
assert(is$dynamic(M:N/A)).
add_dynamic_list((A, B)) :-
add_dynamic_list(A),
add_dynamic_list(B).
add_dynamic_list([H|T]) :-
add_dynamic_list(H),
add_dynamic_list(T).
add_dynamic_list(P) :-
functor(P, N, A), !,
loading_module(M),
assert(is$dynamic(M:N/A)).
add_dynamic_list(X) :-
throw(comperr(badglobal, X)).
check_dynamic(N/A) :-
loading_module(M),
clause(is$dynamic(M:N/A), true),
!.
add_nonterminal_list([]).
add_nonterminal_list(N/A) :-
atom(N),
integer(A), !,
assert(is$nonterminal(N/A)).
add_nonterminal_list((A, B)) :-
add_nonterminal_list(A),
add_nonterminal_list(B).
add_nonterminal_list([H|T]) :-
add_nonterminal_list(H),
add_nonterminal_list(T).
add_nonterminal_list(X) :-
throw(comperr(badglobal, X)).
/* write a header block to the code file --
** 8 bytes as follows
**
** byte 1 set to 0xff -- so we can sense if the file is not ascii
** byte 2 set to 0x03 -- the new multilinked object file tag - indicating
** .plm file. Anded with 0x10 to indicate Unicode.
** byte 3 -- version
** byte 4 -- build
** bytes 5-8 set to 0
*/
write_code_header(CodeHndl) :-
flewrite(CodeHndl, -1, 0), % byte 1
(
is_unicode ->
flewrite(CodeHndl, 0x13, 0) ;
flewrite(CodeHndl, 0x03, 0)
),
version_build(V, B, _),
flewrite(CodeHndl, V, 0), % byte 3 version
flewrite(CodeHndl, B, 0), % byte 4 build
flewrite(CodeHndl, 0, 0),
flewrite(CodeHndl, 0, 0),
flewrite(CodeHndl, 0, 0),
flewrite(CodeHndl, 0, 0), !. % byte 8
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Code to string clauses together
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
close_procedure(CodeHndl, ClausePoint, CurrProc) :-
get$(ClausePoint, CP),
get$(CurrProc, [N/A]),
% see whether we need to string the clauses together along the var block
(
CP = [cpinfo(_, _, PP)] -> % Only one clause
fseek(CodeHndl, 0, 1, EOF),
PP1 is PP - 6,
fseek(CodeHndl, PP1, 0, _), % Seek to clause info int
flewrite(CodeHndl, 1, 1), % set to last cls 0x0001
fseek(CodeHndl, PP, 0, _), % Get to the switch
flewrite(CodeHndl, 52, 0), % set to no_switch op code
fseek(CodeHndl, 6, 1, _), % Get to the try_me_else
flewrite(CodeHndl, 53, 0), % set to no_try op code
fseek(CodeHndl, EOF, 0, _) ;
% Else more than one clause so start the string process
string_clauses(CodeHndl, N, A, CP)
),
cntr_set(1, 1), % clause numbers and labels are relative to procedures
cntr_set(3, -1).
close_procedure(_, _, _) :-
write_l($Error: Unable to close procedure\n$).
% String clauses links up the clauses along the variable block
% (by writing the label fields of the try_me_elses) and then
% generates any branch tables at the end of the procedure
% For 3.0, the relative offsets are no longer stored in the try instructions,
% instead, simply the clause number of where to jump to is stored. This way
% the loader can decide what the right offset is for the target machine.
% This quick fix here should be improved by removing the other code in the
% system that is designed to support the relative offset concept.
% Branch tables are added by just writing another clause for the procedure
%
string_clauses(Hndl, N, A, [cpinfo(FArg, Label, PP)|T]) :-
% String together clauses along their variable blocks
% Special case first clause
fseek(Hndl, 0, 1, EOF), % where we will build hashes
FirstTry is PP + 8, % skip switch, try opcode
fseek(Hndl, FirstTry, 0, _), % move to label of try
NextLabel is Label + 1,
flewrite(Hndl, NextLabel, 1), % simply write # of next clause
string_v_clauses(Hndl, T, LastPP), % Now chain rest of clauses
fseek(Hndl, EOF, 0, _), % Move back to EOF
build_branch(Hndl, N, A, [cpinfo(FArg, Label, PP)|T], LastPP). %
string_v_clauses(Hndl, [cpinfo(_, _, PP)], PP1) :- !, % We are last clause
fseek(Hndl, PP, 0, _), % Get to retry_me_else op code
flewrite(Hndl, 33, 0), % Chng to trust_me_2_else_fail
PP1 is PP - 6,
fseek(Hndl, PP1, 0, _), % Get to info word in clause
flewrite(Hndl, 1, 1). % Change to 1 ("last clause")
%
string_v_clauses(Hndl, [cpinfo(FArg, Label, PP)|T], LastPP) :-
NextTry is PP + 1, % Get to label of try_me op
fseek(Hndl, NextTry, 0, _),
NextLabel is Label + 1,
flewrite(Hndl, NextLabel, 1), % simply write # of next clause
string_v_clauses(Hndl, T, LastPP). % Now chain rest of clauses
% Build the branch tables starting at current posn in codefile
build_branch(H, _, 0, CCI, _) :- % Arity 0 -> no branch tables
kill_switch(H, CCI), !.
build_branch(H, N, A, CCI, _) :- % All first args are variable
all_var(CCI), % -> no branch tables
kill_switch(H, CCI), !.
build_branch(H, N, A, CCI, LastPP) :-
% Otherwise generate hash tables and wire the initial switch
do_blocks(CCI, A, [C, L, S], Code),
% Some or all of Const, List, Struc may currently be variables
% (Branch tables whose code exists but which has not yet been allocated).
% So, first we write the code, then we fix the switch.
(
Code \= [] ->
output(H, Code, N/A, no, PP),
% LastPP has LastClause marker of what was the last clause
% since it is no longer the last clause (Code is)
% we reset to no last clause and set Code to last clause
fseek(H, 0, 1, EOF),
fseek(H, LastPP, 0, _),
flewrite(H, 0, 1),
PP1 is PP - 6,
fseek(H, PP1, 0, _),
flewrite(H, 1, 1) ;
fseek(H, 0, 1, EOF)
),
CCI = [cpinfo(_, _, StartCode)|_], % Get to beginning of procedure
fseek(H, StartCode, 0, _), % Make the switch real
% Compute relative jmps for switch
switch_rel_jmp(C, C1),
switch_rel_jmp(L, L1),
switch_rel_jmp(S, S1),
flewrite(H, 27, 0), % Switch op code
flewrite(H, C1, 1), % And the labels
flewrite(H, L1, 1),
flewrite(H, S1, 1),
fseek(H, EOF, 0, _), !.
switch_rel_jmp(0, 0) :- !.
switch_rel_jmp(fail, 0) :- !.
switch_rel_jmp(B, B).
kill_switch(H, [cpinfo(_, _, S)|_]) :-
fseek(H, 0, 1, EOF),
fseek(H, S, 0, _),
flewrite(H, 52, 0), % write the no_switch op code
fseek(H, EOF, 0, _).
/*
do_blocks( CCI, Arity, [Const, List, Struc], Code)
CCI is our cross clause info
[Const, List, STruc] are the corresponding labels for the switch
Code is a list of branch codes (may be empty)
*/
% only vars and one other kind in first args -
% if X1 is of the other kind it can only match with the vars
do_blocks(CCI, Arity, CLS, TryCode) :-
same_or_var(CCI, Kind), !, % fails or sets Kind to one other kind of arg
filterv(CCI, VarC), % VarC is subset of CCI with all variable clauses
try_block(VarC, Arity, TryLbl, TryCode, TryLink), % Brnch table for Kind
block_code((Kind, CCI), Arity, TryLbl, 1, CLS, TryLink, []).
/* else generate all blocks
note that the vars, ..Lbl, are bound to vars
in Code of the form label(..Lbl)
*/
do_blocks(CCI, Arity, [ConstLbl, ListLbl, StrucLbl], Code) :-
filterlcs(CCI, ListC, ConstC, StrucC),
try_block(ListC, Arity, ListLbl, Code, LLink),
cs_block(ConstC, Arity, ConstLbl, LLink, CLink, _),
cs_block(StrucC, Arity, StrucLbl, CLink, [], _).
% 1st arg is list or var
block_code((list, _), _, TryLbl, VarLbl, [TryLbl, VarLbl, TryLbl], L, L) :- !.
% VarLbl is top of the var block chain - always after the switch.
% else 1st arg is struct or const
block_code((Kind, CompC), Arity, TryLbl, VarLbl, CLS, Code, Link) :-
cs_block(CompC, Arity, BlkLbl, BlkCode, BlkLink, Hashed),
( % no_hash => no separate try_block
Hashed = no_hash ->
CSLbl = VarLbl, % no hash
Code = Link ;
CSLbl = BlkLbl, % hash
Code = BlkCode,
Link = BlkLink
),
(
Kind = constant ->
CLS = [CSLbl, TryLbl, TryLbl] ; % constant
CLS = [TryLbl, TryLbl, CSLbl] % not constant
), !.
same_or_var([cpinfo(FArg, _, _)|Rest], Kind) :- % true if 1st args are all
kind(FArg, K), % variable and 1 other kind
(K = variable ; K = Kind), !,
same_or_var(Rest, Kind).
same_or_var([], _).
all_var(CompC) :- % true if 1st args all vars
same_or_var(CompC, variable). %
/*
** Filter clauses which could match with a list, const or struc as first
** argument. Note that a variable as first arg matches with all of them.
*/
filterlcs([], [], [], []).
filterlcs([X|Rest], [X|ListLbls], [X|ConstLbls], [X|StrucLbls]) :-
X = cpinfo(FArg, _, _),
var(FArg), !,
filterlcs(Rest, ListLbls, ConstLbls, StrucLbls).
filterlcs([X|Rest], [X|ListLbls], ConstLbls, StrucLbls) :-
X = cpinfo('.'/2, _, _),
filterlcs(Rest, ListLbls, ConstLbls, StrucLbls).
filterlcs([X|Rest], ListLbls, [X|ConstLbls], StrucLbls) :-
X = cpinfo(FArg, _, _),
atomic(FArg), !,
filterlcs(Rest, ListLbls, ConstLbls, StrucLbls).
filterlcs([X|Rest], ListLbls, ConstLbls, [X|StrucLbls]) :-
filterlcs(Rest, ListLbls, ConstLbls, StrucLbls).
filterv([], []).
filterv([X|Rest], [X|VarLbls]) :- % Filter clauses with vars as 1st args
X = cpinfo(FArg, _, _),
var(FArg), !,
filterv(Rest, VarLbls).
filterv([_|Rest], VarLbls) :-
filterv(Rest, VarLbls).
/*
** Try block -- generic try-block to try all clauses in the given list.
** Optimizes only if 0 or 1 clauses are given
**
** try_block(ListOfCrossClauses, NumberTempVars, Label, Code, Link)
**
** Try_block produces a list of try/retry codes to branch to the
** clauses specified in the ListOfCrossClauses.
** This code is contained at Label and is the code Code.
** The switch will branch to Label to try the clauses sequentially.
**
** If there is no code in the CCI then our label for the branch header
** is simply 0 (the fail)
** If there is exactly one clause in the CCI then our branch header label
** itself. This means the switch will go directly to the clauses rather
** than generating a try list with only one element
*/
try_block([], _, 0, L, L). % /5 empty
try_block([cpinfo(_, Lbl, _)], _, Lbl, L, L) :- !. % singleton
try_block([cpinfo(_, Lbl, _)|Clauses], NTV, Label, Code, Link) :- % general
try_block(Clauses, LCode, Link), % go to try_block/3
Code = [label(Label), try(NTV, Lbl)|LCode]. % 'try'
try_block([cpinfo(_, Lbl, _)], [trust(Lbl)|L], L) :- !. % /3 base 'trust'
try_block([cpinfo(_, Lbl, _)|Clauses], [retry(Lbl)|LCode], Link) :- % 'retry'
try_block(Clauses, LCode, Link). % recurse
/*
** Const and structure block: First arg is const or structure.
** This routine works for both consts and structs.
** Difference with try_block: generates hash tables if needed
** Variable Hashed indicates whether hash tables were generated -
** It is either no_hash or yes_hash
*/
cs_block([], _, fail, Link, Link, no_hash).
cs_block([cpinfo(_, Lbl, _)], _, Lbl, Link, Link, no_hash).
cs_block(Clauses, Arity, Lbl, [label(Lbl)|Code], Link, Hashed) :-
cs_gather(Clauses, [], Gather, [], Hashed),
(var(Hashed) -> Hashed = no_hash ; true),
cs_link(try, Arity, Gather, Code, Link).
/*
** Gather contiguous arguments which are not variables together.
** The other arguments are left separate
*/
cs_gather([X|Rest], Collect, Gather, Link, H) :-
X = cpinfo(FArg, _, _),
var(FArg), !,
dump(Collect, Gather - G, H),
G = [X|G2],
cs_gather(Rest, [], G2, Link, H).
cs_gather([X|Rest], Collect, Gather, Link, H) :-
X = cpinfo(FArg, _, _),
member(cpinfo(FArg, _, _), Collect), !,
dump(Collect, Gather - G, H),
cs_gather(Rest, [X], G, Link, H).
cs_gather([X|Rest], Collect, G, L, H) :-
cs_gather(Rest, [X|Collect], G, L, H).
cs_gather([], Collect, Gather, Link, H) :-
dump(Collect, Gather - Link, H).
/*
** Convert a collection of clauses to a member of Gather
** If Collect is > 1, it (as list) is a member.
** Else just its element clause is member
*/
dump([], L - L, _).
dump([X], [X|L] - L, _) :-
X = cpinfo(_, _, _).
dump(Collect, [Collect|L] - L, yes_hash).
cs_link(Type, Arity, [Gr], Code, Link) :-
( % Link all elements of Gather together with try, retry, trust
Gr = cpinfo(_, Lbl, _) ->
Code = [trust(Lbl)|Link] ;
hash(Gr, Hash - Link),
(Type = try -> Code = Hash ; Code = [trust(else, fail)|Hash])
).
cs_link(try, NTV, [Gr|Rest], Code, Link) :- !,
(
Gr = cpinfo(_, Lbl, _) ->
(Instr =.. [try, NTV, Lbl], Code = [Instr|L]) ;
(
Gr = [cpinfo(_, _, _)|_],
hash(Gr, Hash - [label(ElseLbl)|L]),
Code = [Instr|Hash],
Instr =.. [try, NTV, else, ElseLbl]
)
),
cs_link(retry, NTV, Rest, L, Link).
cs_link(Type, NTV, [Gr|Rest], Code, Link) :-
(
Gr = cpinfo(_, Lbl, _) ->
(Instr =.. [Type, Lbl], Code = [Instr|L]) ;
(
hash(Gr, Hash - [label(ElseLbl)|L]),
Code = [Instr|Hash],
Instr =.. [Type, else, ElseLbl]
)
),
cs_link(retry, NTV, Rest, L, Link).
hash(Gr, Code - Link) :- % generate hash table with switch inst - cosmetic
hash_table(Gr, HashTbl, Link, Fudge, HashLen),
cs_kind(Gr, Kind),
Code = [switch(Kind, HashLen, Fudge)|HashTbl].
cs_kind([cpinfo(FArg, _, _)|_], Kind) :- % if Gr is bunch of consts or structs
kind(FArg, Kind). % no par needs passing to cs_block
hash_table([cpinfo(FArg, Lbl, _)|Rest], % Construct hash table - dummy code
[pair(FArg, Lbl)|Hash], Link, fudge, Len) :-
hash_table(Rest, Hash, Link, _, Lenl),
Len is Lenl + 1.
hash_table([], Link, Link, _, 0).
kind(Arg, variable) :- % Returns kind of argument
var(Arg), !.
kind(Arg, constant) :-
atomic(Arg), !.
kind('.'/2, list) :- !.
kind(_, structure).
/*--------------------------------------------------------------------------*/
% Local utils
output(_, Code, N/A, _, _) :- % Analyze code *** Temp
clause(wamout(H), true), % if wamout
numbervars(Code, 0, _),
telling(X),
tell(H),
write_plm(N/A, Code),
tell(X),
fail. % do next clause anyway
output(CodeFile, Code, N/A, First, PA) :-
timer(T1),
assemble(CodeFile, Code, N/A, First, PA),
timer(T2).
read_list([H|T]) :- % read a list from keyboard ignoring spaces
eat_spaces(H),
H \= 10,
read_list(T), !.
read_list([]).
eat_spaces(X) :-
(get0(X), X \= 0' ; eat_spaces(X)), !.
puts([]).
puts([H|T]) :- % print out a " string "
put(H),
puts(T).
file_cannot_open(File) :-
throw(comperr($\nCannot open file : \n$, File)).
% report($\nCannot open file : \n$),
% report(File),
% fail.
/* Write to listing window and also, optionally, to listing file (stdout) */
/* This seems dumb, so we've changed it to simply write to the listing file,
which might be sysout. */
write_l(Term) :-
(list_on -> report(Term) ; true).
% report(Term),
% telling(X),
% (list_on -> tell(user), report(Term), tell(X) ; true).
write_w(T) :-
(list_on -> report(T) ; true).
:- end_body(amzi_compiler).