Skip to content

Commit 2fe3e3d

Browse files
committed
Change token threaded primitive NEXT values to flags.
1 parent b296b46 commit 2fe3e3d

File tree

2 files changed

+55
-40
lines changed

2 files changed

+55
-40
lines changed

pkg/forth/primitive.go

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
371371
"st r0, r1, 0", // store value on rsp
372372
"st r1, r2, __rsp", // save rsp
373373
"add r3, r3, 1", // decrement stack
374-
"jump __next_skip_r2",
375374
},
375+
Next: TokenNextSkipR2,
376376
},
377377
ulpAsmSrt: PrimitiveUlpSrt{
378378
Asm: []string{
@@ -411,8 +411,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
411411
"st r1, r2, __rsp", // store rsp
412412
"sub r3, r3, 1", // increment stack
413413
"st r0, r3, 0", // store value on stack
414-
"jump __next_skip_r2",
415414
},
415+
Next: TokenNextSkipR2,
416416
},
417417
ulpAsmSrt: PrimitiveUlpSrt{
418418
Asm: []string{
@@ -571,6 +571,7 @@ func PrimitiveSetup(vm *VirtualMachine) error {
571571
"add r3, r3, 1", // decrement stack pointer
572572
"jump __ins_asm", // start execution of the token
573573
},
574+
Next: TokenNextNonstandard,
574575
},
575576
ulpAsmSrt: PrimitiveUlpSrt{
576577
Asm: []string{
@@ -661,8 +662,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
661662
"ld r0, r3, 0", // get the address from stack
662663
"ld r0, r0, 0", // load the value
663664
"st r0, r3, 0", // store the value on stack
664-
"jump __next_skip_load",
665665
},
666+
Next: TokenNextSkipLoad,
666667
},
667668
ulpAsmSrt: PrimitiveUlpSrt{
668669
Asm: []string{
@@ -704,8 +705,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
704705
"ld r1, r3, 1", // load the value
705706
"st r1, r0, 0", // store the value in address
706707
"add r3, r3, 2", // decrement the stack
707-
"jump __next_skip_r2",
708708
},
709+
Next: TokenNextSkipR2,
709710
},
710711
ulpAsmSrt: PrimitiveUlpSrt{
711712
Asm: []string{
@@ -754,8 +755,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
754755
"ld r0, r0, 0", // the body address token is in the front, load it
755756
"ld r0, r0, 0", // get the address from the token
756757
"st r0, r3, 0", // store the body address on stack
757-
"jump __next_skip_load",
758758
},
759+
Next: TokenNextSkipLoad,
759760
},
760761
ulpAsmSrt: PrimitiveUlpSrt{
761762
Asm: []string{
@@ -810,8 +811,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
810811
"__c_ampersand.0:",
811812
"and r1, r1, 0xFF", // mask off the upper bits
812813
"st r1, r3, 0", // store the masked value
813-
"jump __next_skip_r2",
814814
},
815+
Next: TokenNextSkipR2,
815816
},
816817
ulpAsmSrt: PrimitiveUlpSrt{
817818
Asm: []string{
@@ -883,8 +884,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
883884
"or r2, r2, r1", // merge the new value and old value
884885
"st r2, r0, 0", // store into the address
885886
"add r3, r3, 2", // decrement stack
886-
"jump next",
887887
},
888+
Next: TokenNextNormal,
888889
},
889890
ulpAsmSrt: PrimitiveUlpSrt{
890891
Asm: []string{
@@ -956,8 +957,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
956957
"or r0, r0, 0x8000", // set the bit
957958
"__char_plus.1:",
958959
"st r0, r3, 0", // store the result
959-
"jump __next_skip_r2",
960960
},
961+
Next: TokenNextSkipLoad,
961962
},
962963
ulpAsmSrt: PrimitiveUlpSrt{
963964
Asm: []string{
@@ -1013,8 +1014,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
10131014
"and r0, r0, 0x7FFF", // mask off the upper bit
10141015
"st r0, r3, 0", // store the result
10151016
"__aligned.0:",
1016-
"jump __next_skip_load",
10171017
},
1018+
Next: TokenNextSkipLoad,
10181019
},
10191020
ulpAsmSrt: PrimitiveUlpSrt{
10201021
Asm: []string{
@@ -1131,12 +1132,12 @@ func PrimitiveSetup(vm *VirtualMachine) error {
11311132
},
11321133
ulpAsm: PrimitiveUlp{
11331134
Asm: []string{
1134-
"ld r0, r2, __rsp", // load the return stack pointer
1135-
"ld r1, r0, 0", // load the return address into r1
1136-
"sub r0, r0, 1", // decrement pointer
1137-
"st r0, r2, __rsp", // store the updated return stack pointer
1138-
"jump __next_skip_load", // skip loading, r1 and r2 are already fine
1135+
"ld r0, r2, __rsp", // load the return stack pointer
1136+
"ld r1, r0, 0", // load the return address into r1
1137+
"sub r0, r0, 1", // decrement pointer
1138+
"st r0, r2, __rsp", // store the updated return stack pointer
11391139
},
1140+
Next: TokenNextSkipLoad,
11401141
},
11411142
ulpAsmSrt: PrimitiveUlpSrt{
11421143
Asm: []string{
@@ -1203,8 +1204,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
12031204
"add r0, r0, r1",
12041205
"add r3, r3, 1",
12051206
"st r0, r3, 0",
1206-
"jump __next_skip_r2",
12071207
},
1208+
Next: TokenNextSkipR2,
12081209
},
12091210
ulpAsmSrt: PrimitiveUlpSrt{
12101211
Asm: []string{
@@ -1273,8 +1274,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
12731274
"sub r0, r0, r1",
12741275
"add r3, r3, 1",
12751276
"st r0, r3, 0",
1276-
"jump __next_skip_r2",
12771277
},
1278+
Next: TokenNextSkipR2,
12781279
},
12791280
ulpAsmSrt: PrimitiveUlpSrt{
12801281
Asm: []string{
@@ -1313,8 +1314,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
13131314
"and r0, r0, r1",
13141315
"add r3, r3, 1",
13151316
"st r0, r3, 0",
1316-
"jump __next_skip_r2",
13171317
},
1318+
Next: TokenNextSkipR2,
13181319
},
13191320
ulpAsmSrt: PrimitiveUlpSrt{
13201321
Asm: []string{
@@ -1353,8 +1354,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
13531354
"or r0, r0, r1",
13541355
"add r3, r3, 1",
13551356
"st r0, r3, 0",
1356-
"jump __next_skip_r2",
13571357
},
1358+
Next: TokenNextSkipR2,
13581359
},
13591360
ulpAsmSrt: PrimitiveUlpSrt{
13601361
Asm: []string{
@@ -1408,8 +1409,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
14081409
"jumpr __mult.0, 0, gt", // loop if y != 0
14091410
// finalize
14101411
"add r3, r3, 1", // decrement stack, z already in place
1411-
"jump next",
14121412
},
1413+
Next: TokenNextNormal,
14131414
},
14141415
ulpAsmSrt: PrimitiveUlpSrt{
14151416
Asm: []string{
@@ -1499,8 +1500,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
14991500
// done! store r and q
15001501
"st r2, r3, 1", // r
15011502
"st r1, r3, 0", // q
1502-
"jump next",
15031503
},
1504+
Next: TokenNextNormal,
15041505
},
15051506
ulpAsmSrt: PrimitiveUlpSrt{
15061507
Asm: []string{
@@ -1569,8 +1570,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
15691570
"lsh r0, r0, r1",
15701571
"add r3, r3, 1",
15711572
"st r0, r3, 0",
1572-
"jump __next_skip_r2",
15731573
},
1574+
Next: TokenNextSkipR2,
15741575
},
15751576
ulpAsmSrt: PrimitiveUlpSrt{
15761577
Asm: []string{
@@ -1609,8 +1610,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
16091610
"rsh r0, r0, r1",
16101611
"add r3, r3, 1",
16111612
"st r0, r3, 0",
1612-
"jump __next_skip_r2",
16131613
},
1614+
Next: TokenNextSkipR2,
16141615
},
16151616
ulpAsmSrt: PrimitiveUlpSrt{
16161617
Asm: []string{
@@ -1652,8 +1653,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
16521653
"ld r0, r3, 1",
16531654
"st r1, r3, 1",
16541655
"st r0, r3, 0",
1655-
"jump __next_skip_r2",
16561656
},
1657+
Next: TokenNextSkipR2,
16571658
},
16581659
ulpAsmSrt: PrimitiveUlpSrt{
16591660
Asm: []string{
@@ -1689,8 +1690,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
16891690
"ld r0, r3, 0",
16901691
"sub r3, r3, 1",
16911692
"st r0, r3, 0",
1692-
"jump __next_skip_load",
16931693
},
1694+
Next: TokenNextSkipLoad,
16941695
},
16951696
ulpAsmSrt: PrimitiveUlpSrt{
16961697
Asm: []string{
@@ -1727,8 +1728,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
17271728
"add r0, r0, r3",
17281729
"ld r0, r0, 1",
17291730
"st r0, r3, 0",
1730-
"jump __next_skip_load",
17311731
},
1732+
Next: TokenNextSkipLoad,
17321733
},
17331734
ulpAsmSrt: PrimitiveUlpSrt{
17341735
Asm: []string{
@@ -1768,8 +1769,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
17681769
"sub r0, r0, r1",
17691770
"ld r0, r0, 0",
17701771
"st r0, r3, 0",
1771-
"jump __next_skip_r2",
17721772
},
1773+
Next: TokenNextSkipR2,
17731774
},
17741775
ulpAsmSrt: PrimitiveUlpSrt{
17751776
Asm: []string{
@@ -1822,8 +1823,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
18221823
"ld r0, r3, 2",
18231824
"st r1, r3, 2",
18241825
"st r0, r3, 0",
1825-
"jump __next_skip_r2",
18261826
},
1827+
Next: TokenNextSkipR2,
18271828
},
18281829
ulpAsmSrt: PrimitiveUlpSrt{
18291830
Asm: []string{
@@ -1851,8 +1852,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
18511852
ulpAsm: PrimitiveUlp{
18521853
Asm: []string{
18531854
"add r3, r3, 1",
1854-
"jump __next_skip_load",
18551855
},
1856+
Next: TokenNextSkipLoad,
18561857
},
18571858
ulpAsmSrt: PrimitiveUlpSrt{
18581859
Asm: []string{
@@ -1931,8 +1932,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
19311932
"move r0, 0",
19321933
"__loopcheck.2:",
19331934
"st r0, r3, 0", // save value
1934-
"jump __next_skip_r2",
19351935
},
1936+
Next: TokenNextSkipR2,
19361937
},
19371938
ulpAsmSrt: PrimitiveUlpSrt{
19381939
Asm: []string{
@@ -2001,8 +2002,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
20012002
"__u_lessthan.0:", // then
20022003
"add r3, r3, 1", // decrement stack
20032004
"st r0, r3, 0", // store the result
2004-
"jump __next_skip_r2",
20052005
},
2006+
Next: TokenNextSkipR2,
20062007
},
20072008
ulpAsmSrt: PrimitiveUlpSrt{
20082009
Asm: []string{
@@ -2035,8 +2036,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
20352036
"sub r0, r0, r3",
20362037
"sub r3, r3, 1",
20372038
"st r0, r3, 0",
2038-
"jump __next_skip_load",
20392039
},
2040+
Next: TokenNextSkipLoad,
20402041
},
20412042
ulpAsmSrt: PrimitiveUlpSrt{
20422043
Asm: []string{
@@ -2056,8 +2057,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
20562057
ulpAsm: PrimitiveUlp{
20572058
Asm: []string{
20582059
"move r3, __stack_end", // set the stack pointer to the end of the stack
2059-
"jump __next_skip_load",
20602060
},
2061+
Next: TokenNextSkipLoad,
20612062
},
20622063
ulpAsmSrt: PrimitiveUlpSrt{
20632064
Asm: []string{
@@ -2072,6 +2073,7 @@ func PrimitiveSetup(vm *VirtualMachine) error {
20722073
Asm: []string{
20732074
"halt",
20742075
},
2076+
Next: TokenNextNonstandard,
20752077
},
20762078
ulpAsmSrt: PrimitiveUlpSrt{
20772079
Asm: []string{
@@ -2116,8 +2118,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
21162118
"st r0, r2, HOST_PARAM0", // set the param
21172119
"st r1, r2, HOST_FUNC", // set the function indicator
21182120
"add r3, r3, 2", // decrease the stack by 2
2119-
"jump __next_skip_r2",
21202121
},
2122+
Next: TokenNextSkipR2,
21212123
},
21222124
ulpAsmSrt: PrimitiveUlpSrt{
21232125
Asm: []string{
@@ -2144,8 +2146,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
21442146
"ld r0, r2, HOST_FUNC",
21452147
"sub r3, r3, 1",
21462148
"st r0, r3, 0",
2147-
"jump __next_skip_load",
21482149
},
2150+
Next: TokenNextSkipLoad,
21492151
},
21502152
ulpAsmSrt: PrimitiveUlpSrt{
21512153
Asm: []string{
@@ -2170,8 +2172,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
21702172
"ld r0, r2, MUTEX_TURN", // read turn
21712173
"jumpr __mutex.take.0, 0, gt", // loop if turn>0
21722174
"__mutex.take.1:",
2173-
"jump __next_skip_r2",
21742175
},
2176+
Next: TokenNextSkipLoad,
21752177
},
21762178
ulpAsmSrt: PrimitiveUlpSrt{
21772179
Asm: []string{
@@ -2194,8 +2196,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
21942196
ulpAsm: PrimitiveUlp{
21952197
Asm: []string{
21962198
"st r2, r2, MUTEX_FLAG0", // flag0 = 0
2197-
"jump __next_skip_load",
21982199
},
2200+
Next: TokenNextSkipLoad,
21992201
},
22002202
ulpAsmSrt: PrimitiveUlpSrt{
22012203
Asm: []string{
@@ -2259,8 +2261,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
22592261
"sub r0, r0, r1", // subtract high
22602262
"add r3, r3, 2", // decrement stack
22612263
"st r0, r3, 0", // store zhigh
2262-
"jump __next_skip_r2",
22632264
},
2265+
Next: TokenNextSkipR2,
22642266
},
22652267
ulpAsmSrt: PrimitiveUlpSrt{
22662268
Asm: []string{
@@ -2335,8 +2337,8 @@ func PrimitiveSetup(vm *VirtualMachine) error {
23352337
"add r0, r0, r1", // add high
23362338
"add r3, r3, 2", // decrement stack
23372339
"st r0, r3, 0", // store zhigh
2338-
"jump __next_skip_r2",
23392340
},
2341+
Next: TokenNextSkipR2,
23402342
},
23412343
ulpAsmSrt: PrimitiveUlpSrt{
23422344
Asm: []string{

pkg/forth/word.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,10 @@ type PrimitiveGo func(*VirtualMachine, *DictionaryEntry) error
162162
type TokenNextType int
163163

164164
const (
165-
TokenNextNormal TokenNextType = iota
166-
TokenNextNonstandard
165+
TokenNextNonstandard TokenNextType = iota
166+
TokenNextNormal
167+
TokenNextSkipR2
168+
TokenNextSkipLoad
167169
)
168170

169171
// The ULP assembly for a primitive Word that uses token threading.
@@ -209,6 +211,17 @@ func (w *WordPrimitive) BuildAssembly(u *Ulp) (string, error) {
209211
switch u.compileTarget {
210212
case UlpCompileTargetToken:
211213
asm = append(asm, w.Ulp.Asm...)
214+
switch w.Ulp.Next {
215+
case TokenNextNonstandard:
216+
case TokenNextNormal:
217+
asm = append(asm, "jump next")
218+
case TokenNextSkipR2:
219+
asm = append(asm, "jump __next_skip_r2")
220+
case TokenNextSkipLoad:
221+
asm = append(asm, "jump __next_skip_load")
222+
default:
223+
return "", fmt.Errorf("Unknown compile target %d, please file a bug report", w.Ulp.Next)
224+
}
212225
case UlpCompileTargetSubroutine:
213226
asm = append(asm, w.UlpSrt.Asm...)
214227
if !w.UlpSrt.NonStandardNext {

0 commit comments

Comments
 (0)