Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #39424

Re: ColorForth - another dead end?

From anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups comp.lang.forth
Subject Re: ColorForth - another dead end?
Date 2015-05-29 15:04 +0000
Organization Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID <2015May29.170433@mips.complang.tuwien.ac.at> (permalink)
References (3 earlier) <NOadnZasMuRy7P_InZ2dnUU7-Kdi4p2d@supernews.com> <5ecf958e-bd96-4a9d-a32a-61f5d8a7b6ba@googlegroups.com> <9bc6ede1-d4bd-43f1-a684-6de2163fa7ab@googlegroups.com> <f45e39ab-cd65-4f11-9508-5b3dafc653f3@googlegroups.com> <924c582d-d98f-4afb-876d-8a991a6a4727@googlegroups.com>

Show all headers | View raw


Raimond Dragomir <raimond.dragomir@gmail.com> writes:
[Someone:]
>> : field ( offset size -- new-offset )           \ stream: name     =20
>>     create =20
>>         over ,  +
>>     does> ( struct-adr base-adr -- field-adr )
>>         @ + ;
>>=20
>This is a classical example of inefficiency (like also the CONSTANT example=
> implemented with create does>). The field word will do @ + at RUNTIME even=
> if the offsets are well known at compile time, and this is a very common s=
>ituation!.
>
>There is a solution to this though. First, you need to delay literal compil=
>ing
>in order to have a chance to do constant folding (or any other mechanism)
>Second you need a smart + which should be immediate and perform a + if ther=
>e
>are at least two literals pending, or compile the + otherwise;
>And third, you need to make the defined word immediate.
>In this situation, when the field word is encountered, it is executed at co=
>mpile time (it is immediate). It performs @ fetching the offset value - ok,=
> the offset is always known at compile time. Then it performs the +. + sees=
> at least one literal on the stack - the offset just fetched. If it sees at=
> least one more literal (supposed to be based address) it adds them at comp=
>ile time, leaving the computed address on the stack. Otherwise it compiles =
>the + (this will trigger compiling first the offset as a literal).
>Simple :)

Not simple, and pretty wrong.  The fields defined by the word above are
not defined as immediate, and if you define them as immediate, they would
not do the required thing.

The problem with the definition above is that the body of the field
can be changed later with >BODY !, so the best a compiler can do is to
inline the DOES> part as

<addr> @ +

A simple, but somewhat unusual way to avoid this problem is to define
this as

: +FIELD ( n1 n2 "<spaces>name" -- n3 )
  over >r + 
  : r> postpone literal postpone + postpone ;
;

(+FIELD is the Forth-2012 name of this word).

Let's compare the compiled code:

4 4 field f1 \ with the FIELD above
4 4 +field f2
create x 8 allot
: foo1 x f1 @ + ;
: foo2 x f2 @ + ;

with VFX Forth 4.71:
see foo1 
FOO1 
( 080C09E0    8B1560090C08 )          MOV       EDX, [080C0960]
( 080C09E6    039AB0090C08 )          ADD       EBX, [EDX+080C09B0]
( 080C09EC    C3 )                    NEXT,
( 13 bytes, 3 instructions )
 ok
see foo2 
FOO2 
( 080C0A10    031DB4090C08 )          ADD       EBX, [080C09B4]
( 080C0A16    C3 )                    NEXT,
( 7 bytes, 2 instructions )

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2015: http://www.mpeforth.com/euroforth2015/euroforth2015.htm

Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

ColorForth - another dead end? borota@gmail.com - 2015-05-24 12:22 -0700
  Re: ColorForth - another dead end? hughaguilar96@gmail.com - 2015-05-24 14:00 -0700
    Re: ColorForth - another dead end? borota@gmail.com - 2015-05-24 16:18 -0700
      Re: ColorForth - another dead end? hughaguilar96@gmail.com - 2015-05-24 16:45 -0700
      Re: ColorForth - another dead end? "Elizabeth D. Rather" <erather@forth.com> - 2015-05-24 14:50 -1000
        Re: ColorForth - another dead end? hughaguilar96@gmail.com - 2015-05-24 18:12 -0700
          Re: ColorForth - another dead end? onewhohopes@gmail.com - 2015-05-28 13:00 -0700
            Re: ColorForth - another dead end? hughaguilar96@gmail.com - 2015-05-28 18:55 -0700
            Re: ColorForth - another dead end? Howerd <howerdo@yahoo.co.uk> - 2015-05-29 00:49 -0700
              Re: ColorForth - another dead end? Raimond Dragomir <raimond.dragomir@gmail.com> - 2015-05-29 07:56 -0700
                Re: ColorForth - another dead end? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2015-05-29 15:04 +0000
                Re: ColorForth - another dead end? Raimond Dragomir <raimond.dragomir@gmail.com> - 2015-05-29 10:03 -0700
                Re: ColorForth - another dead end? Raimond Dragomir <raimond.dragomir@gmail.com> - 2015-05-29 10:15 -0700
                Re: ColorForth - another dead end? hughaguilar96@gmail.com - 2015-05-29 19:28 -0700
                Re: ColorForth - another dead end? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2015-05-30 12:34 +0000
                Re: ColorForth - another dead end? hughaguilar96@gmail.com - 2015-05-29 19:19 -0700
                Re: ColorForth - another dead end? Raimond Dragomir <raimond.dragomir@gmail.com> - 2015-05-29 22:30 -0700
                Re: ColorForth - another dead end? "Rod Pemberton" <fork@lllljunkqwer.cpm> - 2015-05-30 00:14 -0400
                Re: ColorForth - another dead end? Raimond Dragomir <raimond.dragomir@gmail.com> - 2015-05-29 22:22 -0700
                Re: ColorForth - another dead end? "Alex McDonald" <blog@rivadpm.com> - 2015-05-30 09:27 +0100
                Re: ColorForth - another dead end? Raimond Dragomir <raimond.dragomir@gmail.com> - 2015-05-30 02:05 -0700
                Re: ColorForth - another dead end? "Alex McDonald" <blog@rivadpm.com> - 2015-05-30 10:21 +0100
                Re: ColorForth - another dead end? Raimond Dragomir <raimond.dragomir@gmail.com> - 2015-05-30 04:08 -0700
                Re: ColorForth - another dead end? hughaguilar96@gmail.com - 2015-05-30 07:43 -0700
                Re: ColorForth - another dead end? Raimond Dragomir <raimond.dragomir@gmail.com> - 2015-05-30 09:42 -0700
                Re: ColorForth - another dead end? hughaguilar96@gmail.com - 2015-05-30 10:25 -0700
                Re: ColorForth - another dead end? Raimond Dragomir <raimond.dragomir@gmail.com> - 2015-05-30 22:36 -0700
                Re: ColorForth - another dead end? hughaguilar96@gmail.com - 2015-05-31 16:38 -0700
                Re: ColorForth - another dead end? "Alex McDonald" <blog@rivadpm.com> - 2015-05-31 09:11 +0100
                Re: ColorForth - another dead end? humptydumpty <ouatubi@gmail.com> - 2015-05-31 11:31 -0700
                Re: ColorForth - another dead end? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2015-06-01 13:21 +0000
                Re: ColorForth - another dead end? humptydumpty <ouatubi@gmail.com> - 2015-06-01 08:18 -0700
                Re: ColorForth - another dead end? humptydumpty <ouatubi@gmail.com> - 2015-06-01 09:07 -0700
                Re: ColorForth - another dead end? humptydumpty <ouatubi@gmail.com> - 2015-06-01 23:31 -0700
                Field name overloading (was: ColorForth - another dead end?) anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2015-06-02 09:47 +0000
                Re: Field name overloading (was: ColorForth - another dead end?) humptydumpty <ouatubi@gmail.com> - 2015-06-02 13:07 -0700
                Re: ColorForth - another dead end? humptydumpty <ouatubi@gmail.com> - 2015-06-01 08:20 -0700
                Re: ColorForth - another dead end? "Rod Pemberton" <fork@lllljunkqwer.cpm> - 2015-05-30 15:34 -0400
                Re: ColorForth - another dead end? hughaguilar96@gmail.com - 2015-05-29 22:55 -0700
                Re: ColorForth - another dead end? Raimond Dragomir <raimond.dragomir@gmail.com> - 2015-05-30 00:54 -0700
              Re: ColorForth - another dead end? stephen@mpeforth.com (Stephen Pelc) - 2015-05-30 17:20 +0000
                Re: ColorForth - another dead end? albert@spenarnc.xs4all.nl (Albert van der Horst) - 2015-05-31 15:17 +0000
        Re: ColorForth - another dead end? Hannu Vuolasaho <vuokkosetae@gmail.com> - 2015-05-25 10:06 +0300
        Re: ColorForth - another dead end? Jason Damisch <jasondamisch@yahoo.com> - 2015-05-25 11:11 -0700
          Re: ColorForth - another dead end? rickman <gnuarm@gmail.com> - 2015-05-25 14:22 -0400
            Re: ColorForth - another dead end? "Elizabeth D. Rather" <erather@forth.com> - 2015-05-25 14:57 -1000
              Re: ColorForth - another dead end? borota@gmail.com - 2015-05-25 20:42 -0700
              Re: ColorForth - another dead end? hughaguilar96@gmail.com - 2015-05-29 19:46 -0700
                Re: ColorForth - another dead end? "Rod Pemberton" <fork@lllljunkqwer.cpm> - 2015-05-30 00:03 -0400
  Re: ColorForth - another dead end? Howerd <howerdo@yahoo.co.uk> - 2015-05-24 21:51 -0700
    Re: ColorForth - another dead end? albert@spenarnc.xs4all.nl (Albert van der Horst) - 2015-05-25 11:17 +0000
  Re: ColorForth - another dead end? Mark Wills <markwills1970@gmail.com> - 2015-05-25 01:50 -0700
    Re: ColorForth - another dead end? Howerd <howerdo@yahoo.co.uk> - 2015-05-25 02:11 -0700
  Re: ColorForth - another dead end? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2015-05-25 14:11 +0000

csiph-web