File:
[gforth] /
gforth /
kernel /
comp.fs
Revision
1.66:
download - view:
text,
annotated -
select for diffs
Thu Sep 2 10:12:28 2004 UTC (19 years, 7 months ago) by
anton
Branches:
MAIN
CVS tags:
HEAD
change COMPILE-TO-PRIMS, to call PEEPHOLE-COMPILE, instead of
recursing indirectly (through POSTPONE and COMPILE,). The speedup
resulting from that is minimal (see below); the main benefit is that
COMPILE, is now called once for each compiled word (used in some
measurements for research that I am doing now).
Here's the speedup data:
#startup overhead
[b3:~/gforth:1709] perfex gforth -e bye
tsc 29123160
[b3:~/gforth:1710] perfex gforth -e bye
tsc 29646820
#before the change
[b3:~/gforth:1711] perfex gforth prims2x.fs -e bye
tsc 95616352
[b3:~/gforth:1712] perfex gforth prims2x.fs -e bye
tsc 95032068
#after the change
[b3:~/gforth:1725] perfex gforth prims2x.fs -e bye
tsc 95594688
[b3:~/gforth:1726] perfex gforth prims2x.fs -e bye
tsc 94587916
1: \ compiler definitions 14sep97jaw
2:
3: \ Copyright (C) 1995,1996,1997,1998,2000,2003 Free Software Foundation, Inc.
4:
5: \ This file is part of Gforth.
6:
7: \ Gforth is free software; you can redistribute it and/or
8: \ modify it under the terms of the GNU General Public License
9: \ as published by the Free Software Foundation; either version 2
10: \ of the License, or (at your option) any later version.
11:
12: \ This program is distributed in the hope that it will be useful,
13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: \ GNU General Public License for more details.
16:
17: \ You should have received a copy of the GNU General Public License
18: \ along with this program; if not, write to the Free Software
19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
20:
21: \ \ Revisions-Log
22:
23: \ put in seperate file 14sep97jaw
24:
25: \ \ here allot , c, A, 17dec92py
26:
27: [IFUNDEF] allot
28: [IFUNDEF] forthstart
29: : allot ( n -- ) \ core
30: dup unused u> -8 and throw
31: dp +! ;
32: [THEN]
33: [THEN]
34:
35: \ we default to this version if we have nothing else 05May99jaw
36: [IFUNDEF] allot
37: : allot ( n -- ) \ core
38: \G Reserve @i{n} address units of data space without
39: \G initialization. @i{n} is a signed number, passing a negative
40: \G @i{n} releases memory. In ANS Forth you can only deallocate
41: \G memory from the current contiguous region in this way. In
42: \G Gforth you can deallocate anything in this way but named words.
43: \G The system does not check this restriction.
44: here +
45: dup 1- usable-dictionary-end forthstart within -8 and throw
46: dp ! ;
47: [THEN]
48:
49: : c, ( c -- ) \ core c-comma
50: \G Reserve data space for one char and store @i{c} in the space.
51: here 1 chars allot c! ;
52:
53: : , ( w -- ) \ core comma
54: \G Reserve data space for one cell and store @i{w} in the space.
55: here cell allot ! ;
56:
57: : 2, ( w1 w2 -- ) \ gforth
58: \G Reserve data space for two cells and store the double @i{w1
59: \G w2} there, @i{w2} first (lower address).
60: here 2 cells allot 2! ;
61:
62: \ : aligned ( addr -- addr' ) \ core
63: \ [ cell 1- ] Literal + [ -1 cells ] Literal and ;
64:
65: : align ( -- ) \ core
66: \G If the data-space pointer is not aligned, reserve enough space to align it.
67: here dup aligned swap ?DO bl c, LOOP ;
68:
69: \ : faligned ( addr -- f-addr ) \ float f-aligned
70: \ [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
71:
72: : falign ( -- ) \ float f-align
73: \G If the data-space pointer is not float-aligned, reserve
74: \G enough space to align it.
75: here dup faligned swap
76: ?DO
77: bl c,
78: LOOP ;
79:
80: : maxalign ( -- ) \ gforth
81: \G Align data-space pointer for all alignment requirements.
82: here dup maxaligned swap
83: ?DO
84: bl c,
85: LOOP ;
86:
87: \ the code field is aligned if its body is maxaligned
88: ' maxalign Alias cfalign ( -- ) \ gforth
89: \G Align data-space pointer for code field requirements (i.e., such
90: \G that the corresponding body is maxaligned).
91:
92: ' , alias A, ( addr -- ) \ gforth
93:
94: ' NOOP ALIAS const
95:
96: \ \ Header 23feb93py
97:
98: \ input-stream, nextname and noname are quite ugly (passing
99: \ information through global variables), but they are useful for dealing
100: \ with existing/independent defining words
101:
102: defer (header)
103: defer header ( -- ) \ gforth
104: ' (header) IS header
105:
106: : string, ( c-addr u -- ) \ gforth
107: \G puts down string as cstring
108: dup c, here swap chars dup allot move ;
109:
110: : longstring, ( c-addr u -- ) \ gforth
111: \G puts down string as longcstring
112: dup , here swap chars dup allot move ;
113:
114: : header, ( c-addr u -- ) \ gforth
115: name-too-long?
116: dup max-name-length @ max max-name-length !
117: align here last !
118: current @ 1 or A, \ link field; before revealing, it contains the
119: \ tagged reveal-into wordlist
120: longstring, cfalign
121: alias-mask lastflags cset ;
122:
123: : input-stream-header ( "name" -- )
124: name name-too-short? header, ;
125:
126: : input-stream ( -- ) \ general
127: \G switches back to getting the name from the input stream ;
128: ['] input-stream-header IS (header) ;
129:
130: ' input-stream-header IS (header)
131:
132: 2variable nextname-string
133:
134: has? OS [IF]
135: : nextname-header ( -- )
136: nextname-string 2@ header,
137: nextname-string free-mem-var
138: input-stream ;
139: [THEN]
140:
141: \ the next name is given in the string
142:
143: has? OS [IF]
144: : nextname ( c-addr u -- ) \ gforth
145: \g The next defined word will have the name @var{c-addr u}; the
146: \g defining word will leave the input stream alone.
147: name-too-long?
148: nextname-string free-mem-var
149: save-mem nextname-string 2!
150: ['] nextname-header IS (header) ;
151: [THEN]
152:
153: : noname-header ( -- )
154: 0 last ! cfalign
155: input-stream ;
156:
157: : noname ( -- ) \ gforth
158: \g The next defined word will be anonymous. The defining word will
159: \g leave the input stream alone. The xt of the defined word will
160: \g be given by @code{latestxt}.
161: ['] noname-header IS (header) ;
162:
163: : latestxt ( -- xt ) \ gforth
164: \G @i{xt} is the execution token of the last word defined.
165: \ The main purpose of this word is to get the xt of words defined using noname
166: lastcfa @ ;
167:
168: ' latestxt alias lastxt \ gforth-obsolete
169: \G old name for @code{latestxt}.
170:
171: : latest ( -- nt ) \ gforth
172: \G @var{nt} is the name token of the last word defined; it is 0 if the
173: \G last word has no name.
174: last @ ;
175:
176: \ \ literals 17dec92py
177:
178: : Literal ( compilation n -- ; run-time -- n ) \ core
179: \G Compilation semantics: compile the run-time semantics.@*
180: \G Run-time Semantics: push @i{n}.@*
181: \G Interpretation semantics: undefined.
182: [ [IFDEF] lit, ]
183: lit,
184: [ [ELSE] ]
185: postpone lit ,
186: [ [THEN] ] ; immediate restrict
187:
188: : ALiteral ( compilation addr -- ; run-time -- addr ) \ gforth
189: [ [IFDEF] alit, ]
190: alit,
191: [ [ELSE] ]
192: postpone lit A,
193: [ [THEN] ] ; immediate restrict
194:
195: : char ( '<spaces>ccc' -- c ) \ core
196: \G Skip leading spaces. Parse the string @i{ccc} and return @i{c}, the
197: \G display code representing the first character of @i{ccc}.
198: bl word char+ c@ ;
199:
200: : [char] ( compilation '<spaces>ccc' -- ; run-time -- c ) \ core bracket-char
201: \G Compilation: skip leading spaces. Parse the string
202: \G @i{ccc}. Run-time: return @i{c}, the display code
203: \G representing the first character of @i{ccc}. Interpretation
204: \G semantics for this word are undefined.
205: char postpone Literal ; immediate restrict
206:
207: \ \ threading 17mar93py
208:
209: : cfa, ( code-address -- ) \ gforth cfa-comma
210: here
211: dup lastcfa !
212: 0 A, 0 , code-address! ;
213:
214: [IFUNDEF] compile,
215: defer compile, ( xt -- ) \ core-ext compile-comma
216: \G Compile the word represented by the execution token @i{xt}
217: \G into the current definition.
218:
219: ' , is compile,
220: [THEN]
221:
222: defer basic-block-end ( -- )
223:
224: :noname ( -- )
225: 0 compile-prim1 ;
226: is basic-block-end
227:
228: has? peephole [IF]
229:
230: \ dynamic only
231: : peephole-compile, ( xt -- )
232: \ compile xt, appending its code to the current dynamic superinstruction
233: here swap , compile-prim1 ;
234:
235: : compile-to-prims, ( xt -- )
236: \G compile xt to use primitives (and their peephole optimization)
237: \G instead of ","-ing the xt.
238: \ !! all POSTPONEs here postpone primitives; this can be optimized
239: dup >does-code if
240: ['] does-exec peephole-compile, , EXIT
241: \ dup >body POSTPONE literal ['] call peephole-compile, >does-code , EXIT
242: then
243: dup >code-address CASE
244: docon: OF >body ['] lit@ peephole-compile, , EXIT ENDOF
245: \ docon: OF >body POSTPONE literal ['] @ peephole-compile, EXIT ENDOF
246: \ docon is also used by VALUEs, so don't @ at compile time
247: docol: OF >body ['] call peephole-compile, , EXIT ENDOF
248: dovar: OF >body ['] lit peephole-compile, , EXIT ENDOF
249: douser: OF >body @ ['] useraddr peephole-compile, , EXIT ENDOF
250: dodefer: OF >body ['] lit-perform peephole-compile, , EXIT ENDOF
251: dofield: OF >body @ ['] lit+ peephole-compile, , EXIT ENDOF
252: \ dofield: OF >body @ POSTPONE literal ['] + peephole-compile, EXIT ENDOF
253: \ code words and ;code-defined words (code words could be optimized):
254: dup in-dictionary? IF drop POSTPONE literal ['] execute peephole-compile, EXIT THEN
255: ENDCASE
256: peephole-compile, ;
257:
258: ' compile-to-prims, IS compile,
259: [ELSE]
260: ' , is compile,
261: [THEN]
262:
263: : !does ( addr -- ) \ gforth store-does
264: latestxt does-code! ;
265:
266: \ !! unused, but ifdefed/gosted in some places
267: : (does>) ( R: addr -- )
268: r> cfaligned /does-handler + !does ; \ !! no gforth-native
269:
270: : (does>2) ( addr -- )
271: cfaligned /does-handler + !does ;
272:
273: : dodoes, ( -- )
274: cfalign here /does-handler allot does-handler! ;
275:
276: : (compile) ( -- ) \ gforth-obsolete: dummy
277: true abort" (compile) doesn't work, use POSTPONE instead" ;
278:
279: \ \ ticks
280:
281: : name>comp ( nt -- w xt ) \ gforth
282: \G @i{w xt} is the compilation token for the word @i{nt}.
283: (name>comp)
284: 1 = if
285: ['] execute
286: else
287: ['] compile,
288: then ;
289:
290: : [(')] ( compilation "name" -- ; run-time -- nt ) \ gforth bracket-paren-tick
291: (') postpone ALiteral ; immediate restrict
292:
293: : ['] ( compilation. "name" -- ; run-time. -- xt ) \ core bracket-tick
294: \g @i{xt} represents @i{name}'s interpretation
295: \g semantics. Perform @code{-14 throw} if the word has no
296: \g interpretation semantics.
297: ' postpone ALiteral ; immediate restrict
298:
299: : COMP' ( "name" -- w xt ) \ gforth comp-tick
300: \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
301: (') name>comp ;
302:
303: : [COMP'] ( compilation "name" -- ; run-time -- w xt ) \ gforth bracket-comp-tick
304: \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
305: COMP' swap POSTPONE Aliteral POSTPONE ALiteral ; immediate restrict
306:
307: : postpone, ( w xt -- ) \ gforth postpone-comma
308: \g Compile the compilation semantics represented by the
309: \g compilation token @i{w xt}.
310: dup ['] execute =
311: if
312: drop compile,
313: else
314: swap POSTPONE aliteral compile,
315: then ;
316:
317: : POSTPONE ( "name" -- ) \ core
318: \g Compiles the compilation semantics of @i{name}.
319: COMP' postpone, ; immediate
320:
321: \ \ recurse 17may93jaw
322:
323: : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
324: \g Call the current definition.
325: latestxt compile, ; immediate restrict
326:
327: \ \ compiler loop
328:
329: : compiler ( c-addr u -- )
330: 2dup find-name dup
331: if ( c-addr u nt )
332: nip nip name>comp execute
333: else
334: drop
335: 2dup snumber? dup
336: IF
337: 0>
338: IF
339: swap postpone Literal
340: THEN
341: postpone Literal
342: 2drop
343: ELSE
344: drop compiler-notfound
345: THEN
346: then ;
347:
348: : [ ( -- ) \ core left-bracket
349: \G Enter interpretation state. Immediate word.
350: ['] interpreter IS parser state off ; immediate
351:
352: : ] ( -- ) \ core right-bracket
353: \G Enter compilation state.
354: ['] compiler IS parser state on ;
355:
356: \ \ Strings 22feb93py
357:
358: : S, ( addr u -- )
359: \ allot string as counted string
360: here over char+ allot place align ;
361:
362: : mem, ( addr u -- )
363: \ allot the memory block HERE (do alignment yourself)
364: here over allot swap move ;
365:
366: : ," ( "string"<"> -- )
367: [char] " parse s, ;
368:
369: \ \ Header states 23feb93py
370:
371: : cset ( bmask c-addr -- )
372: tuck @ or swap ! ;
373:
374: : creset ( bmask c-addr -- )
375: tuck @ swap invert and swap ! ;
376:
377: : ctoggle ( bmask c-addr -- )
378: tuck @ xor swap ! ;
379:
380: : lastflags ( -- c-addr )
381: \ the address of the flags byte in the last header
382: \ aborts if the last defined word was headerless
383: latest dup 0= abort" last word was headerless" cell+ ;
384:
385: : immediate ( -- ) \ core
386: \G Make the compilation semantics of a word be to @code{execute}
387: \G the execution semantics.
388: immediate-mask lastflags cset ;
389:
390: : restrict ( -- ) \ gforth
391: \G A synonym for @code{compile-only}
392: restrict-mask lastflags cset ;
393:
394: ' restrict alias compile-only ( -- ) \ gforth
395: \G Remove the interpretation semantics of a word.
396:
397: \ \ Create Variable User Constant 17mar93py
398:
399: : Alias ( xt "name" -- ) \ gforth
400: Header reveal
401: alias-mask lastflags creset
402: dup A, lastcfa ! ;
403:
404: doer? :dovar [IF]
405:
406: : Create ( "name" -- ) \ core
407: Header reveal dovar: cfa, ;
408: [ELSE]
409:
410: : Create ( "name" -- ) \ core
411: Header reveal here lastcfa ! 0 A, 0 , DOES> ;
412: [THEN]
413:
414: : Variable ( "name" -- ) \ core
415: Create 0 , ;
416:
417: : AVariable ( "name" -- ) \ gforth
418: Create 0 A, ;
419:
420: : 2Variable ( "name" -- ) \ double two-variable
421: create 0 , 0 , ;
422:
423: : uallot ( n -- ) \ gforth
424: udp @ swap udp +! ;
425:
426: doer? :douser [IF]
427:
428: : User ( "name" -- ) \ gforth
429: Header reveal douser: cfa, cell uallot , ;
430:
431: : AUser ( "name" -- ) \ gforth
432: User ;
433: [ELSE]
434:
435: : User Create cell uallot , DOES> @ up @ + ;
436:
437: : AUser User ;
438: [THEN]
439:
440: doer? :docon [IF]
441: : (Constant) Header reveal docon: cfa, ;
442: [ELSE]
443: : (Constant) Create DOES> @ ;
444: [THEN]
445:
446: : Constant ( w "name" -- ) \ core
447: \G Define a constant @i{name} with value @i{w}.
448: \G
449: \G @i{name} execution: @i{-- w}
450: (Constant) , ;
451:
452: : AConstant ( addr "name" -- ) \ gforth
453: (Constant) A, ;
454:
455: : Value ( w "name" -- ) \ core-ext
456: (Constant) , ;
457:
458: : AValue ( w "name" -- ) \ core-ext
459: (Constant) A, ;
460:
461: : 2Constant ( w1 w2 "name" -- ) \ double two-constant
462: Create ( w1 w2 "name" -- )
463: 2,
464: DOES> ( -- w1 w2 )
465: 2@ ;
466:
467: doer? :dofield [IF]
468: : (Field) Header reveal dofield: cfa, ;
469: [ELSE]
470: : (Field) Create DOES> @ + ;
471: [THEN]
472:
473: \ \ interpret/compile:
474:
475: struct
476: >body
477: cell% field interpret/compile-int
478: cell% field interpret/compile-comp
479: end-struct interpret/compile-struct
480:
481: : interpret/compile: ( interp-xt comp-xt "name" -- ) \ gforth
482: Create immediate swap A, A,
483: DOES>
484: abort" executed primary cfa of an interpret/compile: word" ;
485: \ state @ IF cell+ THEN perform ;
486:
487: \ IS Defer What's Defers TO 24feb93py
488:
489: doer? :dodefer [IF]
490:
491: : Defer ( "name" -- ) \ gforth
492: \ !! shouldn't it be initialized with abort or something similar?
493: Header Reveal dodefer: cfa,
494: ['] noop A, ;
495:
496: [ELSE]
497:
498: : Defer ( "name" -- ) \ gforth
499: Create ['] noop A,
500: DOES> @ execute ;
501:
502: [THEN]
503:
504: : Defers ( compilation "name" -- ; run-time ... -- ... ) \ gforth
505: \G Compiles the present contents of the deferred word @i{name}
506: \G into the current definition. I.e., this produces static
507: \G binding as if @i{name} was not deferred.
508: ' >body @ compile, ; immediate
509:
510: :noname
511: dodoes, here !does ]
512: defstart :-hook ;
513: :noname
514: ;-hook ?struc
515: [ has? xconds [IF] ] exit-like [ [THEN] ]
516: here 5 cells + postpone aliteral postpone (does>2) [compile] exit
517: finish-code dodoes,
518: defstart :-hook ;
519: interpret/compile: DOES> ( compilation colon-sys1 -- colon-sys2 ; run-time nest-sys -- ) \ core does
520:
521: : <IS> ( "name" xt -- ) \ gforth
522: \g Changes the @code{defer}red word @var{name} to execute @var{xt}.
523: ' >body ! ;
524:
525: : [IS] ( compilation "name" -- ; run-time xt -- ) \ gforth bracket-is
526: \g At run-time, changes the @code{defer}red word @var{name} to
527: \g execute @var{xt}.
528: ' >body postpone ALiteral postpone ! ; immediate restrict
529:
530: ' <IS>
531: ' [IS]
532: interpret/compile: IS ( xt "name" -- ) \ gforth
533: \G A combined word made up from @code{<IS>} and @code{[IS]}.
534:
535: ' <IS>
536: ' [IS]
537: interpret/compile: TO ( w "name" -- ) \ core-ext
538:
539: :noname ' >body @ ;
540: :noname ' >body postpone ALiteral postpone @ ;
541: interpret/compile: What's ( interpretation "name" -- xt; compilation "name" -- ; run-time -- xt ) \ gforth
542: \G @i{Xt} is the XT that is currently assigned to @i{name}.
543:
544: : interpret/compile? ( xt -- flag )
545: >does-code ['] DOES> >does-code = ;
546:
547: \ \ : ; 24feb93py
548:
549: defer :-hook ( sys1 -- sys2 )
550:
551: defer ;-hook ( sys2 -- sys1 )
552:
553: 0 Constant defstart
554:
555: [IFDEF] docol,
556: : (:noname) ( -- colon-sys )
557: \ common factor of : and :noname
558: docol, ]comp
559: [ELSE]
560: : (:noname) ( -- colon-sys )
561: \ common factor of : and :noname
562: docol: cfa,
563: [THEN]
564: defstart ] :-hook ;
565:
566: : : ( "name" -- colon-sys ) \ core colon
567: Header (:noname) ;
568:
569: : :noname ( -- xt colon-sys ) \ core-ext colon-no-name
570: 0 last !
571: cfalign here (:noname) ;
572:
573: [IFDEF] fini,
574: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core semicolon
575: ;-hook ?struc fini, comp[ reveal postpone [ ; immediate restrict
576: [ELSE]
577: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core semicolon
578: ;-hook ?struc [compile] exit
579: [ has? peephole [IF] ] finish-code [ [THEN] ]
580: reveal postpone [ ; immediate restrict
581: [THEN]
582:
583: \ \ Search list handling: reveal words, recursive 23feb93py
584:
585: : last? ( -- false / nfa nfa )
586: latest ?dup ;
587:
588: : (reveal) ( nt wid -- )
589: wordlist-id dup >r
590: @ over ( name>link ) !
591: r> ! ;
592:
593: \ make entry in wordlist-map
594: ' (reveal) f83search reveal-method !
595:
596: Variable warnings ( -- addr ) \ gforth
597: G -1 warnings T !
598:
599: : check-shadow ( addr count wid -- )
600: \G prints a warning if the string is already present in the wordlist
601: >r 2dup 2dup r> (search-wordlist) warnings @ and ?dup if
602: >stderr
603: ." redefined " name>string 2dup type
604: str= 0= if
605: ." with " type
606: else
607: 2drop
608: then
609: space space EXIT
610: then
611: 2drop 2drop ;
612:
613: : reveal ( -- ) \ gforth
614: last?
615: if \ the last word has a header
616: dup ( name>link ) @ 1 and
617: if \ it is still hidden
618: dup ( name>link ) @ 1 xor ( nt wid )
619: 2dup >r name>string r> check-shadow ( nt wid )
620: dup wordlist-map @ reveal-method perform
621: else
622: drop
623: then
624: then ;
625:
626: : rehash ( wid -- )
627: dup wordlist-map @ rehash-method perform ;
628:
629: ' reveal alias recursive ( compilation -- ; run-time -- ) \ gforth
630: \g Make the current definition visible, enabling it to call itself
631: \g recursively.
632: immediate restrict
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>