Advertisement
Guest User

SVARS.TXT

a guest
Mar 16th, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.68 KB | Software | 0 0
  1.  
  2.  
  3. The following screens cover specialized variables and
  4. related words such as QUAN, TO, AT, IS, and VALUE.
  5. These screens were originally written by Martin Tracy
  6. (MicroMotion) as a handout to the L.A. FIG group in 1984.
  7. - Scott Squires
  8.  
  9. SCR# 0
  10. \ IS & TO solutions Chuck Moore 22JUN84 MJT
  11. It all started at the Catalina standardization meeting (1978),
  12. when Chuck Moore suggested the first "TO" solution.
  13. The problem was that moving data between variables and the
  14. stack resulted in inefficient and unreadable constructs like:
  15.  
  16. X @ Y @ + Z !
  17.  
  18. instead of
  19.  
  20. X Y + TO Z
  21.  
  22. Chuck Moore's suggestion was investigated and elaborated on by
  23. Paul Bartholdi (Observatoire De Geneve, Switzerland) in
  24. Forth Dimensions Vol 1 (4 & 5) 1978/79.
  25.  
  26. SCR# 1
  27. \ IS & TO solutions Bartholdi 22JUN84 MJT
  28. \ Bartholdi's implementation, if it were written in Forth-83,
  29. \ would look something like this:
  30.  
  31. VARIABLE %VAR \ this switch is set 0 to store and -1 to fetch.
  32.  
  33. \ for readability:
  34. 0 CONSTANT FALSE -1 CONSTANT TRUE
  35.  
  36. : ON ( addr --) TRUE SWAP ! ;
  37. : OFF ( addr --) FALSE SWAP ! ;
  38.  
  39. : TO ( --)
  40. \ simply turns the %VAR switch on.
  41. \ See the new definition for VARIABLE on the following screen.
  42. %VAR ON ;
  43.  
  44.  
  45. SCR# 2
  46. \ IS & TO solutions Bartholdi (continued) 22JUN84 MJT
  47. \ VARIABLEs now have to be smarter.
  48.  
  49. : VARIABLE
  50. CREATE ( --) ( creates a normal VARIABLE body)
  51. 0 ,
  52. DOES> %VAR @ ( checks the TO switch)
  53. IF ( n --) ( If the variable was preceded by TO)
  54. ! %VAR OFF ( n is stored into the variable and )
  55. ( %VAR is reset.)
  56. ELSE ( -- n) ( If the variable was not preceded by TO )
  57. @ ( the variable acts just like a CONSTANT.)
  58. THEN ;
  59.  
  60. \ VARIABLE could be written as easily in machine code.
  61.  
  62.  
  63. SCR# 3
  64. \ IS & TO solutions Bartholdi (continued) 22JUN84 MJT
  65. \ Here is TO in action:
  66.  
  67. VARIABLE X VARIABLE Y VARIABLE Z
  68. 3 TO X 4 TO Y
  69.  
  70. X X * Y Y * + TO Z ( Z is set to 25)
  71.  
  72.  
  73. SCR# 4
  74. \ IS & TO solutions Bartholdi (continued) 22JUN84 MJT
  75. The "TO" solution was used widely in Europe and especially
  76. at the University of Utrecht. Nevertheless, there are
  77. several problems with this solution. One is that the address
  78. of a variable is not easily available. For example, to add
  79. 7 to the value of Z ( defined on the previous screen), you
  80. would need to use
  81.  
  82. 7 ' Z >BODY +!
  83. or
  84. 7 Z + TO Z
  85.  
  86. instead of
  87.  
  88. 7 Z +!
  89.  
  90.  
  91. SCR# 5
  92. \ IS & TO solutions Bartholdi (continued) 22JUN84 MJT
  93. Various extensions to the "TO" solution were proposed, such as
  94.  
  95. AT Z
  96.  
  97. to obtain the address of Z and
  98.  
  99. 7 +TO Z
  100.  
  101. to add 7 to the value of Z.
  102.  
  103. Furthermore, the %VAR switch must be checked at run-time, which
  104. slows execution speed. Since either @ or ! is executed inside
  105. the defining VARIABLE word, this TO will always be slower than
  106. the equivalent explicit @ and ! operations.
  107.  
  108.  
  109. SCR# 6
  110. \ IS & TO solutions Rosen 22JUN84 MJT
  111. At the same time that Bartholdi published his "TO" solution,
  112. George Lyons suggested that a variable might have two code field
  113. addresses (cfa's) -- one for the @ and one for the ! operation
  114. (Forth Dimensions Vol 1 [5] 1979)
  115. The text interpreter could then make the decision as to which
  116. cfa to compile or execute.
  117.  
  118. At the 1982 FORML conference, Evan Rosen (Valpar, International)
  119. presented his implementation, using three cfa's for the three
  120. most common data operations:
  121.  
  122. fetching: pushing data to the stack from memory
  123. storing: popping data from the stack to memory
  124. pointing: pushing the address of data to the stack
  125.  
  126.  
  127.  
  128. SCR# 7
  129. \ IS & TO solutions Rosen 22JUN84 MJT
  130. Rosen used the defining word QUAN instead of VARIABLE.
  131. A QUAN has this internal structure:
  132.  
  133. -------
  134. nfa & | name &
  135. lfa | link
  136. -------
  137. cfa0 | ^code0 -- like a CONSTANT -- the default action
  138. -------
  139. cfa1 | ^code1 -- like a VARIABLE ! -- selected by TO
  140. -------
  141. cfa2 | ^code2 -- like a VARIABLE -- selected by AT
  142. -------
  143. pfa | value
  144. -------
  145.  
  146.  
  147. SCR# 8
  148. \ IS & TO solutions Rosen ( continued) 22JUN84 MJT
  149. Unlike the simpler Bartholdi routines, TO and AT must be
  150. state-smart compiler words. Since in an indirect-threaded-code
  151. (ITC) Forth the code field must always point to machine-code,
  152. a run-time subroutine must be written for each of the three
  153. data actions. Because the parameter field is at a different
  154. distance from each code field, only the run-time subroutine
  155. for a variable (code2) functions unchanged.
  156.  
  157. The Forth-83 standard specifies that only the normal cfa can
  158. be compiled in the address stream. It furthermore requires
  159. that the ' ("tick") word must return the pfa of a word, where
  160. data is normally stored. For these reasons, the QUAN concept
  161. does not meet the current (1983) standard. However, the
  162. following implementation of QUAN will run on several Forth-83
  163. systems.
  164.  
  165.  
  166. SCR# 9
  167. \ IS & TO solutions Rosen ( continued) 22JUN84 MJT
  168. \ Here are the run-time subroutines for the code field of QUAN.
  169. \ They are written in MicroMotion Masterforth 6502 assembler.
  170.  
  171. CODE code0 ( -- n)
  172. \ like constant, but the pfa is four bytes further away.
  173. 6 # LDY W )Y LDA PHA INY W )Y LDA PUSH JMP END-CODE
  174.  
  175. CODE code1 ( n --)
  176. \ like variable !, but the pfa is two bytes further away.
  177. 4 # LDY BOT LDA W )Y STA
  178. INY BOT 1+ LDA W )Y STA POP JMP END-CODE
  179.  
  180. CODE code2 ( n --)
  181. \ this is the run-time code for a variable.
  182. CLC W LDA 2 * ADC PHA TYA W 1+ ADC PUSH JMP END-CODE
  183.  
  184.  
  185. SCR# 10
  186. \ IS & TO solutions Rosen ( continued) 22JUN84 MJT
  187. : TO
  188. \ compiles the 2nd cfa (code1) of the QUAN that follows.
  189. STATE @
  190. IF ( --) ( compiling) ' 2+ ,
  191. ELSE ( n --) ( interpreting) ' 6 + !
  192. THEN ; IMMEDIATE
  193.  
  194. : AT
  195. \ compiles the 3rd cfa (code2) of the QUAN that follows.
  196. STATE @
  197. IF ( -- addr) ( compiling) ' 4 + ,
  198. ELSE ( --) ( interpreting) ' 6 +
  199. THEN ; IMMEDIATE
  200.  
  201.  
  202. SCR# 11
  203. \ IS & TO solutions Rosen ( continued) 22JUN84 MJT
  204. \ NOTE: USING -2 ALLOT to replace the first code field (code0)
  205. \ isn't Forth-83 standard either. A less elegant but more
  206. \ correct ;CODE could be used instead.
  207.  
  208. : QUAN
  209. \ defines the three-headed QUAN.
  210. CREATE ( --) -2 ALLOT
  211. [ ' code0 >BODY ] LITERAL ,
  212. [ ' code1 >BODY ] LITERAL ,
  213. [ ' code2 >BODY ] LITERAL ,
  214. ( the body:) 0 , ;
  215.  
  216.  
  217. SCR# 12
  218. \ IS & TO solutions Rosen ( continued) 22JUN84 MJT
  219. \ QUANs work like this:
  220.  
  221. QUAN X QUAN Y QUAN Z
  222. 3 TO X 4 TO Y X X * Y Y * + TO Z
  223.  
  224. : SETZ ( x y --)
  225. \ stores the sum of the squares of x and y into z.
  226. DUP * SWAP DUP * + TO Z ;
  227.  
  228.  
  229. SCR# 13
  230. \ IS & TO solutions Rosen ( continued) 22JUN84 MJT
  231. The QUAN solution can be extended to any words with multiple
  232. actions selected by special prefixes (such as TO).
  233. For example, a double-number DQUAN could be easily defined and
  234. could use the same prefixes (AT and TO) as a single QUAN.
  235. Rosen suggested a VECT defining word for vectored execution.
  236. Klaus Schleisiek (Universitaet Dortmund, Germany) generalized
  237. this even further [J] of Forth Ap & Res Vol 1[2] 1983), using
  238. a grammar for defining defining words suggested by Bill
  239. Ragsdale (FORML 1983).
  240.  
  241. The generality of the QUAN solution is, however, somewhat of
  242. an illusion, since carefully crafted machine-code subroutines
  243. must be written for each code field. Furthermore, the QUAN
  244. solution does not meet the Forth-83 standard and must be
  245. adjusted for each Forth implementation.
  246.  
  247.  
  248. SCR# 14
  249. \ IS & TO solutions Laxen/Perry/Tracy 22JUN84 MJT
  250. Henry Laxen and Mike Perry, in their public-domain FORTH-83
  251. (No Visible Support, 1984), used the
  252. word IS as an alternative to the TO solution. Martin Tracy
  253. (Mastering Forth, Brady, 1984) further enhanced the IS solution
  254. with the defining word VALUE.
  255.  
  256. The IS soultion takes advantage of the fact that in many Forth
  257. applications the @'s greatly outnumber the !'s. In other words,
  258. the data value is used more often than it is changed. Like the
  259. original TO solution, the default action of a data object is to
  260. leave its value on the stack. IS is used to change the value
  261. when necessary.
  262.  
  263.  
  264. SCR# 15
  265. \ IS & TO solutions Laxen/Perry/Tracy (cont.) 22JUN84 MJT
  266. \ The defining word VALUE is used to create a data object which
  267. \ leaves its value on the stack.
  268.  
  269. : VALUE
  270. CREATE ( n --) ,
  271. DOES> ( -- n) @ ;
  272.  
  273. \ Notice that this is a possible definition of CONSTANT.
  274. \ However, the Forth-83 standard expressly forbids changing the
  275. \ value of a constant. Furthermore, if code is to be ROMable,
  276. \ a metacompiler might decide to put CONSTANTs in ROM and
  277. \ VALUEs in RAM.
  278.  
  279.  
  280. SCR# 16
  281. \ IS & TO solutions Laxen/Perry/Tracy (cont.) 22JUN84 MJT
  282.  
  283. : IS ( n --)
  284. \ stores n into body of the word that follows.
  285. STATE @
  286. IF ( compiling) ' >BODY [COMPILE] LITERAL COMPILE !
  287. ELSE ( interpreting) ' >BODY !
  288. THEN ; IMMEDIATE
  289.  
  290. \ IS can be further optimized by writing a run-time routine
  291. \ to be compiled by IS. This routine stores n into the body
  292. \ of the word which follows in the address stream.
  293. \ However, there is no way in the Forth-83 standard of
  294. \ determining this address.
  295.  
  296.  
  297. SCR# 17
  298. \ IS & TO solutions Laxen/Perry/Tracy (cont.) 22JUN84 MJT
  299. \ Here is IS in action:
  300.  
  301. 3 VALUE X 4 VALUE Y 0 VALUE Z
  302.  
  303. X X * IS X Y Y * IS Y X Y + IS Z
  304.  
  305. : SETZ ( x y --)
  306. \ stores the sum of the squares of the x and y into z.
  307. DUP * SWAP DUP * + IS Z ;
  308.  
  309. \ Since IS stores into the pfa of a word, it can be used to
  310. \ set VALUEs, VARIABLEs, execution vectors, and so on.
  311. \ IS is simple, effective, and meets the Forth-83 standard.
  312.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement