File: gprolog008.html

package info (click to toggle)
gprolog 1.3.0-6.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 13,512 kB
  • ctags: 8,954
  • sloc: ansic: 57,431; perl: 16,620; sh: 5,900; makefile: 1,284
file content (670 lines) | stat: -rw-r--r-- 36,734 bytes parent folder | download | duplicates (2)
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="gprolog.css">
<TITLE>
The GNU Prolog compiler
</TITLE>
</HEAD>
<BODY TEXT=black BGCOLOR=white>
<A HREF="gprolog007.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="gprolog004.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<HR>

<H3 CLASS="subsection"><A NAME="htoc12">3.4</A>&nbsp;&nbsp;The GNU Prolog compiler</H3><UL>
<LI><A HREF="gprolog008.html#toc6">Different kinds of codes</A>
<LI><A HREF="gprolog008.html#toc7">Compilation scheme</A>
<LI><A HREF="gprolog008.html#toc8">Using the compiler</A>
<LI><A HREF="gprolog008.html#toc9">Running an executable</A>
<LI><A HREF="gprolog008.html#toc10">Generating a new interactive interpreter</A>
<LI><A HREF="gprolog008.html#toc11">The hexadecimal predicate name encoding</A>
</UL>

<A NAME="The-GNU-Prolog-compiler"></A>
<A NAME="toc6"></A>
<H4 CLASS="subsubsection"><A NAME="htoc13">3.4.1</A>&nbsp;&nbsp;Different kinds of codes</H4>
<A NAME="Different-kinds-of-codes"></A>
One of the main advantages of GNU Prolog is its ability to produce stand
alone executables. A Prolog program can be compiled to native code to give
rise to a machine-dependent executable using the GNU Prolog compiler. However
native-code predicates cannot be listed nor fully debugged. So there is an
alternative to native-code compilation: byte-code compilation. By default the
GNU Prolog compiler produces native-code but via a command-line option it can
produce a file ready for byte-code loading. This is exactly what
<TT>consult/1</TT> does as was explained above (section&nbsp;<A HREF="gprolog006.html#Consulting-a-Prolog-program">3.2.3</A>). GNU Prolog also manages interpreted code using a Prolog interpreter
written in Prolog. Obviously interpreted code is slower than byte-code but
does not require the invocation of the GNU Prolog compiler. This interpreter is
used each time a meta-call is needed as by <TT>call/1</TT> (section&nbsp;<A HREF="gprolog022.html#call/1">6.2.3</A>).
This also the case of dynamically asserted clauses. The following table
summarizes these three kinds of codes:<BR>
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1>
<TR><TD ALIGN=left NOWRAP>Type</TD>
<TD ALIGN=left NOWRAP>Speed</TD>
<TD ALIGN=center NOWRAP>Debug ?</TD>
<TD ALIGN=left NOWRAP>For what</TD>
</TR>
<TR><TD ALIGN=left NOWRAP>interpreted-code</TD>
<TD ALIGN=left NOWRAP>slow</TD>
<TD ALIGN=center NOWRAP>yes</TD>
<TD ALIGN=left NOWRAP>meta-call and dynamically asserted clauses</TD>
</TR>
<TR><TD ALIGN=left NOWRAP>byte-code</TD>
<TD ALIGN=left NOWRAP>medium</TD>
<TD ALIGN=center NOWRAP>yes</TD>
<TD ALIGN=left NOWRAP>consulted predicates</TD>
</TR>
<TR><TD ALIGN=left NOWRAP>native-code</TD>
<TD ALIGN=left NOWRAP>fast</TD>
<TD ALIGN=center NOWRAP>no</TD>
<TD ALIGN=left NOWRAP>compiled predicates</TD>
</TR></TABLE><BR>
<A NAME="toc7"></A>
<H4 CLASS="subsubsection"><A NAME="htoc14">3.4.2</A>&nbsp;&nbsp;Compilation scheme</H4>
<A NAME="Compilation-scheme"></A>
<B>Native-code compilation</B>: a Prolog source is compiled in several
stages to produce an object file that is linked to the GNU Prolog libraries
to produce an executable. The Prolog source is first compiled to obtain a
WAM [<CITE><A HREF="gprolog071.html#Warren83">9</A></CITE>] file. For a detailed study of the WAM the
interested reader can refer to
<A HREF="http://www.isg.sfu.ca/~hak/documents/wam.html">&#8220;Warren's Abstract
 Machine: A Tutorial Reconstruction&#8221;</A> [<CITE><A HREF="gprolog071.html#Ait-Kaci91">1</A></CITE>]. The WAM file is
translated to a machine-independent language specifically designed for GNU
Prolog. This language is close to a (universal) assembly language and is
based on a very reduced instruction set. For this reason this language is
called mini-assembly (MA). The mini-assembly file is then
mapped to the assembly language of the target machine. This assembly file is
assembled to give rise to an object file which is then linked with the GNU
Prolog libraries to provide an executable. The compiler also takes into
account Finite Domain constraint definition files. It translates them to C
and invoke the C compiler to obtain object files. The following figure
presents this compilation scheme:<BR>
<BR>
<DIV CLASS="center"><IMG SRC="compil-scheme.gif"></DIV><BR>
<BR>
Obviously all intermediate stages are hidden to the user who simply invokes
the compiler on his Prolog file(s) (plus other files: C,...) and
obtains an executable. However, it is also possible to stop the compiler at
any given stage. This can be useful, for instance, to see the WAM code
produced (perhaps when learning the WAM). Finally it is possible to give any
kind of file to the compiler which will insert it in the compilation chain
at the stage corresponding to its type. The type of a file is determined
using the suffix of its file name. The following table presents all
recognized types/suffixes:<BR>
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1>
<TR><TD ALIGN=left NOWRAP>Suffix of the file</TD>
<TD ALIGN=left NOWRAP>Type of the file</TD>
<TD ALIGN=left NOWRAP>Handled by:</TD>
</TR>
<TR><TD ALIGN=left NOWRAP><TT>.pl</TT>, <TT>.pro</TT></TD>
<TD ALIGN=left NOWRAP>Prolog source file</TD>
<TD ALIGN=left NOWRAP><TT>pl2wam</TT></TD>
</TR>
<TR><TD ALIGN=left NOWRAP><TT>.wam</TT></TD>
<TD ALIGN=left NOWRAP>WAM source file</TD>
<TD ALIGN=left NOWRAP><TT>wam2ma</TT></TD>
</TR>
<TR><TD ALIGN=left NOWRAP><TT>.ma</TT></TD>
<TD ALIGN=left NOWRAP>Mini-assembly source file</TD>
<TD ALIGN=left NOWRAP><TT>ma2asm</TT></TD>
</TR>
<TR><TD ALIGN=left NOWRAP><TT>.s</TT></TD>
<TD ALIGN=left NOWRAP>Assembly source file</TD>
<TD ALIGN=left NOWRAP>the assembler</TD>
</TR>
<TR><TD ALIGN=left NOWRAP><TT>.c</TT>, <TT>.C</TT>, <TT>.CC</TT>, <TT>.cc</TT>, <TT>.cxx</TT>,
<TT>.c++</TT>, <TT>.cpp</TT></TD>
<TD ALIGN=left NOWRAP>C or C++ source file</TD>
<TD ALIGN=left NOWRAP>the C compiler</TD>
</TR>
<TR><TD ALIGN=left NOWRAP><TT>.fd</TT></TD>
<TD ALIGN=left NOWRAP>Finite Domain constraint source file</TD>
<TD ALIGN=left NOWRAP><TT>fd2c</TT></TD>
</TR>
<TR><TD ALIGN=left NOWRAP>any other suffix (<TT>.o</TT>, <TT>.a</TT>,...)</TD>
<TD ALIGN=left NOWRAP>any other type
(object, library,...)</TD>
<TD ALIGN=left NOWRAP>the linker (C linker)</TD>
</TR></TABLE><BR>
<B>Byte-code compilation</B>: the same compiler can be used to compile a
source Prolog file for byte-code. In that case the Prolog to WAM compiler is
invoked using a specific option and produces a WAM for byte-code source file
(suffixed <TT>.wbc</TT>) that can be later loaded using <TT>load/1</TT>
(section&nbsp;<A HREF="gprolog046.html#load/1">7.23.2</A>). Note that this is exactly what <TT>consult/1</TT>
(section&nbsp;<A HREF="gprolog046.html#consult/1">7.23.1</A>) does as explained above (section&nbsp;<A HREF="gprolog006.html#Consulting-a-Prolog-program">3.2.3</A>).<BR>
<BR>
<A NAME="toc8"></A>
<H4 CLASS="subsubsection"><A NAME="htoc15">3.4.3</A>&nbsp;&nbsp;Using the compiler</H4>
<A NAME="Using-the-compiler"></A>
The GNU Prolog compiler is a command-line compiler similar in spirit to a Unix
C compiler like <TT>gcc</TT>. To invoke the compiler use the <TT>gplc</TT>
command as follows:
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD ALIGN=left NOWRAP><TT>% gplc </TT>[<I><TT>OPTION</TT></I>]...<I><TT>&nbsp;FILE</TT></I>...</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;&nbsp;</TD>
<TD ALIGN=left NOWRAP>(the <TT>%</TT> symbol is the operating system shell prompt)</TD>
</TR></TABLE></DL>
The arguments of <TT>gplc</TT> are file names that are dispatched in the
compilation scheme depending on the type determined from their suffix as was
explained previously (section&nbsp;<A HREF="#Compilation-scheme">3.4.2</A>). All object files are then
linked to produce an executable. Note however that GNU Prolog has no module
facility (since there is not yet an ISO reference for Prolog modules) thus a
predicate defined in a Prolog file is visible from any other predicate
defined in any other file. GNU Prolog allows the user to split a big Prolog
source into several files but does not offer any way to hide a predicate
from others.<BR>
<BR>
The simplest way to obtain an executable from a Prolog source file
<TT>prog.pl</TT> is to use:
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>% gplc prog.pl</TT></DL>
This will produce an native executable called <TT>prog</TT> which can be
executed as follows:
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>% prog</TT></DL>
However, there are several options that can be used to control the
compilation:<BR>
<BR>
<B>General options</B>:<BR>
<TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD VALIGN=top ALIGN=left><TT>-o</TT> <I><TT>FILE</TT></I>, <TT>&ndash;output</TT> <I><TT>FILE</TT></I></TD>
<TD VALIGN=top ALIGN=left NOWRAP>use
<I><TT>FILE</TT></I> as the name of the output file</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>-W</TT>, <TT>&ndash;wam-for-native</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>stop after producing WAM files(s)</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>-w</TT>, <TT>&ndash;wam-for-byte-code</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>stop after producing WAM for
byte-code file(s) (force <TT>&ndash;no-call-c</TT>)</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>-M</TT>, <TT>&ndash;mini-assembly</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>stop after producing mini-assembly
files(s)</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>-S</TT>, <TT>&ndash;assembly</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>stop after producing assembly files (s)</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>-F</TT>, <TT>&ndash;fd-to-c</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>stop after producing C files(s) from FD
constraint definition file(s)</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>-c</TT>, <TT>&ndash;object</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>stop after producing object files(s)</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;temp-dir</TT> <I><TT>PATH</TT></I></TD>
<TD VALIGN=top ALIGN=left NOWRAP>use <I><TT>PATH</TT></I> as directory
for temporary files</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;no-del-temp</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>do not delete temporary files</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;no-decode-hexa</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>do not decode hexadecimal predicate names</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>-v</TT>, <TT>&ndash;verbose</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>print executed commands</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>-h</TT>, <TT>&ndash;help</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>print a help and exit</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;version</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>print version number and exit</TD>
</TR></TABLE><BR>
<B>Prolog to WAM compiler options</B>:<BR>
<TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;pl-state</TT> <I><TT>FILE</TT></I></TD>
<TD VALIGN=top ALIGN=left NOWRAP>read <I><TT>FILE</TT></I> to set the
initial Prolog state</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;no-susp-warn</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>do not show warnings for suspicious predicates</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;no-singl-warn</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>do not show warnings for named singleton
variables</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;no-redef-error</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>no not show errors for built-in predicate
redefinitions</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;foreign-only</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>only compile <TT>foreign/1-2</TT> directives</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;no-call-c</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>do not allow the use of <TT>fd_tell</TT>,
<TT>'$call_c</TT>',...</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;no-inline</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>do not inline predicates</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;no-reorder</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>do not reorder predicate arguments</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;no-reg-opt</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>do not optimize registers</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;min-reg-opt</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>minimally optimize registers</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;no-opt-last-subterm</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>do not optimize last subterm
compilation</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;fast-math</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>use fast mathematical mode (assume integer
arithmetics)</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;keep-void-inst</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>keep void WAM instructions in the output file</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;compile-msg</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>print a compile message</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;statistics</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>print statistics information</TD>
</TR></TABLE><BR>
<B>WAM to mini-assembly translator options</B>:<BR>
<TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;comment</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>include comments in the output file</TD>
</TR></TABLE><BR>
<B>Mini-assembly to assembly translator options</B>:<BR>
<TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;comment</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>include comments in the output file</TD>
</TR></TABLE><BR>
<B>C compiler options</B>:<BR>
<TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;c-compiler</TT> <I><TT>FILE</TT></I></TD>
<TD VALIGN=top ALIGN=left NOWRAP>use <I><TT>FILE</TT></I> as C compiler</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>-C</TT> <I><TT>OPTION</TT></I></TD>
<TD VALIGN=top ALIGN=left NOWRAP>pass <I><TT>OPTION</TT></I> to the C compiler</TD>
</TR></TABLE><BR>
<B>Assembler options</B>:<BR>
<TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD VALIGN=top ALIGN=left><TT>-A</TT> <I><TT>OPTION</TT></I></TD>
<TD VALIGN=top ALIGN=left NOWRAP>pass <I><TT>OPTION</TT></I> to the assembler</TD>
</TR></TABLE><BR>
<B>Linker options</B>:<BR>
<TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;local-size</TT> <I><TT>N</TT></I></TD>
<TD VALIGN=top ALIGN=left NOWRAP>set default local stack size to
<I><TT>N</TT></I> Kb</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;global-size</TT> <I><TT>N</TT></I></TD>
<TD VALIGN=top ALIGN=left NOWRAP>set default global stack size to
<I><TT>N</TT></I> Kb</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;trail-size</TT> <I><TT>N</TT></I></TD>
<TD VALIGN=top ALIGN=left NOWRAP>set default trail stack size to
<I><TT>N</TT></I> Kb</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;cstr-size</TT> <I><TT>N</TT></I></TD>
<TD VALIGN=top ALIGN=left NOWRAP>set default constraint stack size to
<I><TT>N</TT></I> Kb</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;fixed-sizes</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>do not consult environment variables at run-time
(use default sizes)</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;no-top-level</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>do not link the top-level (force
<TT>&ndash;no-debugger</TT>)</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;no-debugger</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>do not link the Prolog/WAM debugger</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;min-pl-bips</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>link only used Prolog built-in predicates</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;min-fd-bips</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>link only used FD solver built-in predicates</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;min-bips</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>shorthand for: <TT>&ndash;no-top-level</TT>
<TT>&ndash;min-pl-bips</TT> <TT>&ndash;min-fd-bips</TT></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;min-size</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>shorthand for: <TT>&ndash;min-bips</TT> <TT>&ndash;strip</TT></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;no-fd-lib</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>do not look for the FD library (maintenance only)</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>-s</TT>, <TT>&ndash;strip</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>strip the executable</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>-L</TT> <I><TT>OPTION</TT></I></TD>
<TD VALIGN=top ALIGN=left NOWRAP>Pass <I><TT>OPTION</TT></I> to the linker</TD>
</TR></TABLE><BR>
It is possible to only give the prefix of an option if there is no ambiguity.<BR>
<BR>
The name of the output file is controlled via the <TT>-o</TT>
<I><TT>FILE</TT></I> option. If present the output file produced will be
named <I><TT>FILE</TT></I>. If not specified, the output file name depends on the
last stage reached by the compiler. If the link is not done the output file
name(s) is the input file name(s) with the suffix associated with the last
stage. If the link is done, the name of the executable is the name (without
suffix) of the first file name encountered in the command-line. Note that if
the link is not done <TT>-o</TT> has no sense in the presence of multiple
input file names. For this reason, several meta characters are available for substitution in <I><TT>FILE</TT></I>:
<UL CLASS="itemize"><LI CLASS="li-itemize">
<TT>%f</TT> is substitued by the whole input file name.
<LI CLASS="li-itemize"><TT>%F</TT> is similar to <TT>%f</TT> but the directory part is omitted.
<LI CLASS="li-itemize"><TT>%p</TT> is substitued by the whole prefix file name (omitting the suffix).
<LI CLASS="li-itemize"><TT>%P</TT> is similar to <TT>%p</TT> but the directory part is omitted.
<LI CLASS="li-itemize"><TT>%s</TT> is substitued by the file suffix (including the dot).
<LI CLASS="li-itemize"><TT>%d</TT> is substitued by the directory part (empty if no directory is specified).
<LI CLASS="li-itemize"><TT>%c</TT> is substitued by the value of an internal counter starting from 1 and auto-incremented.
</UL>
By default the compiler runs in the native-code compilation scheme. To
generate a WAM file for byte-code use the <TT>&ndash;wam-for-byte-code</TT>
option. The resulting file can then be loaded using <TT>load/1</TT>
(section&nbsp;<A HREF="gprolog046.html#load/1">7.23.2</A>).<BR>
<BR>
To execute the Prolog to WAM compiler in a given <EM>read environment</EM>
(operator definitions, character conversion table,...) use
<TT>&ndash;pl-state</TT> <I><TT>FILE</TT></I>. The state file should be
produced by <TT>write_pl_state_file/1</TT>
(section&nbsp;<A HREF="gprolog045.html#write-pl-state-file/1">7.22.5</A>).<BR>
<BR>
By default the Prolog to WAM compiler inlines calls to some deterministic
built-in predicates (e.g. <TT>arg/3</TT> and <TT>functor/3</TT>). Namely a
call to such a predicate will not yield a classical predicate call but a
simple C function call (which is obviously faster). It is possible to avoid
this using <TT>&ndash;no-inline</TT>.<BR>
<BR>
Another optimization performed by the Prolog to WAM compiler is unification
reordering. The arguments of a predicate are reordered to optimize
unification. This can be deactivated using <TT>&ndash;no-reorder</TT>. The
compiler also optimizes the unification/loading of nested compound terms.
More precisely, the compiler emits optimized instructions when the last
subterm of a compound term is itself a compound term (e.g. lists). This can
be deactivated using <TT>&ndash;no-opt-last-subterm</TT>.<BR>
<BR>
By default the Prolog to WAM compiler fully optimizes the allocation of
registers to decrease both the number of instruction produced and the number
of used registers. A good allocation will generate many <EM>void
instructions</EM> that are removed from the produced file except if
<TT>&ndash;keep-void-inst</TT> is specified. To prevent any optimization use
<TT>&ndash;no-reg-opt</TT> while <TT>&ndash;min-reg-opt</TT> forces the compiler to
only perform simple register optimizations.<BR>
<BR>
The Prolog to WAM compiler emits an error when a control construct or a
built-in predicate is redefined. This can be avoided using
<TT>&ndash;no-redef-error</TT>. The compiler also emits warnings for suspicious
predicate definitions like <TT>-/2</TT> since this often corresponds to an
earlier syntax error (e.g. <TT>-</TT> instead of <TT>_</TT>. This can be
deactivated by specifying <TT>&ndash;no-susp-warn</TT>. Finally, the compiler
warns when a singleton variable has a name (i.e. not the generic anonymous
name <TT>_</TT>). This can be deactivated specifying
<TT>&ndash;no-singl-warn</TT>. <BR>
<BR>
Predicate names are encoded with an hexadecimal representation. This is
explained in more detail later (section&nbsp;<A HREF="#The-hexadecimal-predicate-name-encoding">3.4.6</A>). By default the error messages from the linker (e.g. multiple
definitions for a given predicate, reference to an undefined
predicate,...) are filtered to replace any hexadecimal representation
by the real predicate name. Specifying the <TT>&ndash;no-decode-hexa</TT>
prevents <TT>gplc</TT> from filtering linker output messages and hexadecimal
representations are then shown.<BR>
<BR>
When producing an executable it is possible to specify default stack sizes
(using <TT>&ndash;<I>STACK_NAME</I>-size</TT>) and to prevent it from consulting
environment variables (using <TT>&ndash;fixed-sizes</TT>) as was explained above
(section&nbsp;<A HREF="gprolog007.html#Adjusting-the-size-of-Prolog-stacks">3.3</A>). By default the produced
executable will include the top-level, the Prolog/WAM debugger
and all Prolog and FD built-in predicates. It is possible to avoid linking
the top-level (section&nbsp;<A HREF="gprolog006.html#The-GNU-Prolog-interactive-interpreter">3.2</A>) by specifying
<TT>&ndash;no-top-level</TT>. In this case, at least one
<TT>initialization/1</TT> directive (section&nbsp;<A HREF="gprolog021.html#initialization/1">6.1.13</A>) should be
defined. The option <TT>&ndash;no-debugger</TT> does not link the debugger. To
include only used built-in predicates that are actually used the options
<TT>&ndash;no-pl-bips</TT> and/or <TT>&ndash;no-fd-bips</TT> can be specified. For the
smallest executable all these options should be specified. This can be
abbreviated by using the shorthand option <TT>&ndash;min-bips</TT>. By default,
executables are not <EM>stripped</EM>, i.e. their symbol table is not
removed. This table is only useful for the C debugger (e.g. when interfacing
Prolog and C). To remove the symbol table (and then to reduce the size of
the final executable) use <TT>&ndash;strip</TT>. Finally <TT>&ndash;min-size</TT> is a
shortcut for <TT>&ndash;min-bips</TT> and <TT>&ndash;strip</TT>, i.e. the produced
executable is as small as possible.<BR>
<BR>
Example: compile and link two Prolog sources <TT>prog1.pl</TT> and
<TT>prog2.pl</TT>. The resulting executable will be named <TT>prog1</TT>
(since <TT>-o</TT> is not specified):
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>% gplc prog1.pl prog2.pl</TT></DL>
Example: compile the Prolog file <TT>prog.pl</TT> to study basic WAM code.
The resulting file will be named <TT>prog.wam</TT>:
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>% gplc -W &ndash;no-inline &ndash;no-reorder &ndash;keep-void-inst prog.pl</TT></DL>
Example: compile the Prolog file <TT>prog.pl</TT> and its C interface file
<TT>utils.c</TT> to provide an autonomous executable called
<TT>mycommand</TT>. The executable is not stripped to allow the use of the C
debugger:
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>% gplc -o mycommand prog.pl utils.c</TT></DL>
Example: detail all steps to compile the Prolog file <TT>prog.pl</TT> (the
resulting executable is stripped). All intermediate files are produced
(<TT>prog.wam</TT>, <TT>prog.ma</TT>, <TT>prog.s</TT>, <TT>prog.o</TT> and
the executable <TT>prog</TT>):
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list">
<PRE CLASS="verbatim">
% gplc -W prog.pl
% gplc -M --comment prog.wam
% gplc -S --comment prog.ma
% gplc -c prog.s
% gplc -o prog -s prog.o
</PRE></DL>
<A NAME="toc9"></A>
<H4 CLASS="subsubsection"><A NAME="htoc16">3.4.4</A>&nbsp;&nbsp;Running an executable</H4>
<A NAME="Running-an-executable"></A>
In this section we explain what happens when running an executable produced
by the GNU Prolog native-code compiler. The default main function first starts
the Prolog engine. This function collects all linked objects (issued from the
compilation of Prolog files) and initializes them. The initialization of a
Prolog object file consists in adding to appropriate tables new atoms, new
predicates and executing its system directives. A system directive is
generated by the Prolog to WAM compiler to reflect a (user) directive
executed at compile-time such as <TT>op/3</TT> (section&nbsp;<A HREF="gprolog021.html#op/3">6.1.10</A>). Indeed, when the
compiler encounters such a directive it immediately executes it and also
generates a system directive to execute it at the start of the executable.
When all system directives have been executed the Prolog engine executes all
initialization directives defined with <TT>initialization/1</TT>
(section&nbsp;<A HREF="gprolog021.html#initialization/1">6.1.13</A>). If several initialization directives appear in the
same file they are executed in the order of appearance. If several
initialization directives appear in different files the order in which they
are executed is machine-dependant. However, on most machines the order will
be the reverse order in which the associated files have been linked (this is
not true under native win32). When all initialization directives have been
executed the default main function looks for the GNU Prolog
top-level. If present (i.e. it has been linked) it is called otherwise
the program simply ends. Note that if the top-level is not linked and if
there is no initialization directive the program is useless since it simply
ends without doing any work. The default main function detects such a
behavior and emits a warning message.<BR>
<BR>
Example: compile an empty file <TT>prog.pl</TT> without linking the
top-level and execute it:
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list">
<PRE CLASS="verbatim">
% gplc --no-top-level prog.pl
% prog
Warning: no initial goal executed
   use a directive :- initialization(Goal)
   or remove the link option --no-top-level (or --min-bips or --min-size)
</PRE></DL>
<A NAME="toc10"></A>
<H4 CLASS="subsubsection"><A NAME="htoc17">3.4.5</A>&nbsp;&nbsp;Generating a new interactive interpreter</H4>
<A NAME="Generating-a-new-interactive-interpreter"></A>
In this section we show how to define a new top-level extending the
GNU Prolog interactive interpreter with new predicate definitions. The
obtained top-level can then be considered as an enriched version of the basic
GNU Prolog top-level (section&nbsp;<A HREF="gprolog006.html#The-GNU-Prolog-interactive-interpreter">3.2</A>). Indeed, each
added predicate can be viewed as a predefined predicate just like any other
built-in predicate. This can be achieved by compiling these predicates and
including the top-level at link-time.<BR>
<BR>
The real question is: why would we include some predicates in a new
top-level instead of simply consulting them under the GNU Prolog top-level ?
There are two reasons for this:
<UL CLASS="itemize"><LI CLASS="li-itemize">the predicate cannot be consulted. This is the case of a predicate
calling foreign code, like a predicate interfacing with C (section&nbsp;<A HREF="gprolog065.html#Interfacing-Prolog-and-C">9</A>) or a predicate defining a new FD constraint.<BR>
<BR>
<LI CLASS="li-itemize">the performance of the predicate is crucial. Since it is compiled to
native-code such a predicate will be executed very quickly. Consulting will
load it as byte-code. The gain is much more noticeable if the program is run
under the debugger. The included version will not be affected by the
debugger while the consulted version will be several times slower.
Obviously, a predicate should be included in a new top-level only when it is
itself debugged since it is difficult to debug native-code.</UL>
To define a new top-level simply compile the set of desired predicates and
linking them with the GNU Prolog top-level (this is the default) using
<TT>gplc</TT> (section&nbsp;<A HREF="#Using-the-compiler">3.4.3</A>).<BR>
<BR>
Example: let us define a new top-level called <TT>my_top_level</TT>
including all predicates defined in <TT>prog.pl</TT>:
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>% gplc -o my_top_level prog.pl</TT></DL>
By the way, note that if <TT>prog.pl</TT> is an empty Prolog file the
previous command will simply create a new interactive interpreter similar to
the GNU Prolog top-level.<BR>
<BR>
Example: as before where some predicates of <TT>prog.pl</TT> call C functions
defined in <TT>utils.c</TT>:
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>% gplc -o my_top_level prog.pl utils.c</TT></DL>
In conclusion, defining a particular top-level is nothing else but a
particular case of the native-code compilation. It is simple to do and very
useful in practice.<BR>
<BR>
<A NAME="toc11"></A>
<H4 CLASS="subsubsection"><A NAME="htoc18">3.4.6</A>&nbsp;&nbsp;The hexadecimal predicate name encoding</H4>
<A NAME="The-hexadecimal-predicate-name-encoding"></A>
When the GNU Prolog compiler compiles a Prolog source to an object file it
has to associate a symbol to each predicate name. However, the syntax of
symbols is restricted to identifiers: string containing only letters, digits
or underscore characters. On the other hand, predicate names (i.e. atoms) can
contain any character with quotes if necessary (e.g. <TT>'x+y=z'</TT> is a
valid predicate name). The compiler has then to encode predicate names
respecting the syntax of identifiers. To achieve this, GNU Prolog uses an
hexadecimal representation where each predicate name is translated to a
symbol beginning with an <TT>X</TT> followed by the hexadecimal notation of
the code of each character of the name.<BR>
<BR>
Example: <TT>'x+y=z'</TT> will be encoded as <TT>X782B793D7A</TT> since
<TT>78</TT> is the hexadecimal representation of the code of <TT>x</TT>,
<TT>2B</TT> of the code of <TT>+</TT>, etc.<BR>
<BR>
Since Prolog allows the user to define several predicates with the same name
but with a different arity GNU Prolog encodes predicate indicators (predicate
name followed by the arity). The symbol associated with the predicate name is
then followed by an underscore and by the decimal notation of the arity.<BR>
<BR>
Example: <TT>'x+y=z'/3</TT> will be encoded as <TT>X782B793D7A_3</TT>.<BR>
<BR>
So, from the mini-assembly stage, each predicate indicator is replaced
by its hexadecimal encoding. The knowledge of this encoding is normally not
of interest for the user, i.e. the Prolog programmer. For this reason the
GNU Prolog compiler hides this encoding. When an error occurs on a predicate
(undefined predicate, predicate with multiple definitions,...) the
compiler has to decode the symbol associated with the predicate indicator. For
this <TT>gplc</TT> filters each message emitted by the linker to locate and
decode eventual predicate indicators. This filtering can be deactivated
specifying <TT>&ndash;no-decode-hexa</TT> when invoking <TT>gplc</TT> (section&nbsp;<A HREF="#Using-the-compiler">3.4.3</A>).<BR>
<BR>
This filter is provided as an utility that can be invoked using the
<TT>hexgplc</TT> command as follows:
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD ALIGN=left NOWRAP><TT>% hexgplc </TT>[<I><TT>OPTION</TT></I>]...<I><TT>&nbsp;FILE</TT></I>...</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;&nbsp;</TD>
<TD ALIGN=left NOWRAP>(the <TT>%</TT> symbol is the operating system shell prompt)</TD>
</TR></TABLE></DL>
<B>Options</B>:<BR>
<TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;encode</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>encoding mode (default mode is decoding)</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;relax</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>decode also predicate names (not only predicate
indicators)</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;printf</TT> <I><TT>FORMAT</TT></I></TD>
<TD VALIGN=top ALIGN=left NOWRAP>pass encoded/decoded string to C
<TT>printf(3)</TT> with <I><TT>FORMAT</TT></I></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;aux-father</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>decode an auxiliary predicate as its father</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;aux-father2</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>decode an auxiliary predicate as its father +
auxiliary number</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;cmd-line</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>encode/decode each argument of the command-line</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>-H</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>same as: <TT>&ndash;cmd-line &ndash;encode</TT></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>-P</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>same as: <TT>&ndash;cmd-line &ndash;relax</TT></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;help</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>print a help and exit</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>&ndash;version</TT></TD>
<TD VALIGN=top ALIGN=left NOWRAP>print version number and exit</TD>
</TR></TABLE><BR>
It is possible to give a prefix of an option if there is no ambiguity.<BR>
<BR>
Without arguments <TT>hexgplc</TT> runs in decoding mode reading its
standard input and decoding each symbol corresponding to a predicate
indicator. To use <TT>hexgplc</TT> in the encoding mode the
<TT>&ndash;encode</TT> option must be specified. By default <TT>hexgplc</TT> only
decodes predicate indicators, this can be relaxed using <TT>&ndash;relax</TT> to
also take into account simple predicate names (the arity can be omitted). It
is possible to format the output of an encoded/decoded string using
<TT>&ndash;printf <I>FORMAT</I></TT> in that case each string
<I><TT>S</TT></I> is passed to the C <TT>printf(3)</TT> function as
<TT>printf(<I>FORMAT</I>,<I>S</I>)</TT>. <BR>
<BR>
Auxiliary predicates are generated by the Prolog to WAM compiler when
simplifying some control constructs like <TT>';'/2</TT> present in the body
of a clause. They are of the form
<TT>'$<I>NAME</I>/<I>ARITY</I>_$aux<I>N</I>'</TT> where
<TT><I>NAME</I>/<I>ARITY</I></TT> is the predicate indicator of the
simplified (i.e. father) predicate and <I><TT>N</TT></I> is a sequential
number (a predicate can give rise to several auxiliary predicates). It is
possible to force <TT>hexgplc</TT> to decode an auxiliary predicate as its
father predicate indicator using <TT>&ndash;aux-father</TT> or as its father
predicate indicator followed by the sequential number using
<TT>&ndash;aux-father2</TT>.<BR>
<BR>
If no file is specified, <TT>hexgplc</TT> processes its standard input
otherwise each file is treated sequentially. Specifying the
<TT>&ndash;cmd-line</TT> option informs <TT>hexgplc</TT> that each argument is not
a file name but a string that must be encoded (or decoded). This is useful to
encode/decode a particular string. For this reason the option <TT>-H</TT>
(encode to hexadecimal) and <TT>-P</TT> (decode to Prolog) are provided as
shorthand. Then, to obtain the hexadecimal representation of a predicate
<I><TT>P</TT></I> use: 
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>% hexgplc -H <I>P</I></TT></DL>
Example:
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list">
<PRE CLASS="verbatim">
% hexgplc -H 'x+y=z'
X782B793D7A
</PRE></DL>

<HR SIZE=2>
Copyright (C) 1999-2007 Daniel Diaz
<BR>
<BR>
Verbatim copying and distribution of this entire article is permitted in any
medium, provided this notice is preserved. <BR>
<BR>
<A HREF="index.html#copyright">More about the copyright</A>
<HR>
<A HREF="gprolog007.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="gprolog004.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
</BODY>
</HTML>