Skip to content

Commit 339a423

Browse files
committed
Use the word body address rather than the word address. Helps with optimizations.
1 parent ee607c9 commit 339a423

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

pkg/forth/cell.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ func (c CellLiteral) AddToList(u *Ulp) error {
167167
if err != nil {
168168
return err
169169
}
170+
_, ok := c.cell.(CellAddress)
171+
if ok {
172+
ref = "__body" + ref
173+
}
170174
litref := c.reference(ref)
171175
u.literals[litref] = ref
172176
cellAddress, ok := c.cell.(CellAddress)
@@ -185,6 +189,10 @@ func (c CellLiteral) BuildExecution(u *Ulp) (string, error) {
185189
if err != nil {
186190
return "", err
187191
}
192+
_, ok := c.cell.(CellAddress)
193+
if ok {
194+
name = "__body" + name
195+
}
188196
switch u.compileTarget {
189197
case UlpCompileTargetToken:
190198
ref := c.reference(name)
@@ -356,10 +364,10 @@ func (c *CellTailCall) AddToList(u *Ulp) error {
356364
func (c *CellTailCall) BuildExecution(u *Ulp) (string, error) {
357365
switch u.compileTarget {
358366
case UlpCompileTargetToken:
359-
return fmt.Sprintf(".int %s + 0x8000", c.dest.Entry.ulpName), nil
367+
return fmt.Sprintf(".int %s + 0x8000", c.dest.Entry.BodyLabel()), nil
360368
case UlpCompileTargetSubroutine:
361369
// put the address after the docol
362-
return fmt.Sprintf("move r2, %s + 1\r\njump r2", c.dest.Entry.ulpName), nil
370+
return fmt.Sprintf("move r2, %s\r\njump r2", c.dest.Entry.BodyLabel()), nil
363371
default:
364372
return "", fmt.Errorf("Unknown compile target %d, please file a bug report", u.compileTarget)
365373
}

pkg/forth/dictionary.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func (d *DictionaryEntry) ClearVisited() {
3737
d.Flag.visited = false
3838
}
3939

40+
func (d *DictionaryEntry) BodyLabel() string {
41+
return "__body" + d.ulpName
42+
}
43+
4044
// The Forth Dictionary. This architecture uses individual entries
4145
// representing words rather than a flat cell structure.
4246
type Dictionary struct {

pkg/forth/primitive.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ func PrimitiveSetup(vm *VirtualMachine) error {
592592
"st r2, r1, 0", // store return address
593593
"move r2, __rsp", // put pointer on rsp
594594
"st r1, r2, 0", // store rsp
595-
"add r2, r0, 1", // go past the docol
595+
"move r2, r0", // put address into instruction pointer
596596
"jump r2", // jump to the forth word, past the docol
597597
},
598598
NonStandardNext: true,
@@ -761,9 +761,9 @@ func PrimitiveSetup(vm *VirtualMachine) error {
761761
ulpAsmSrt: PrimitiveUlpSrt{
762762
Asm: []string{
763763
"ld r0, r3, 0", // get the address from stack
764-
"ld r0, r0, 1", // get the body address in the move instruction
765-
"rsh r0, r0, 4", // shift it into the correct space
766-
"st r0, r3, 0", // store the body address on stack
764+
"ld r0, r0, 0", // get the move instruction
765+
"rsh r0, r0, 4", // shift the address into the correct space
766+
"st r0, r3, 0", // store the address on stack
767767
},
768768
},
769769
},

pkg/forth/word.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,20 @@ func (w *WordForth) AddToList(u *Ulp) error {
113113

114114
func (w *WordForth) BuildAssembly(u *Ulp) (string, error) {
115115
output := make([]string, 1)
116-
output[0] = w.Entry.ulpName + ":"
116+
label := w.Entry.ulpName + ":"
117+
bodyLabel := w.Entry.BodyLabel() + ":"
118+
output[0] = label
117119
if w.Entry.Flag.Data { // data word
120+
output = append(output, bodyLabel)
118121
for _, cell := range w.Cells {
119122
ref, err := cell.OutputReference(u)
120123
if err != nil {
121124
return "", err
122125
}
126+
_, ok := cell.(CellAddress)
127+
if ok {
128+
ref = "__body" + ref
129+
}
123130
val := ".int " + ref
124131
output = append(output, val)
125132
}
@@ -129,6 +136,7 @@ func (w *WordForth) BuildAssembly(u *Ulp) (string, error) {
129136
output = append(output, "jump __docol")
130137
}
131138
}
139+
output = append(output, bodyLabel)
132140
for _, cell := range w.Cells {
133141
asm, err := cell.BuildExecution(u)
134142
if err != nil {
@@ -207,7 +215,8 @@ func (w *WordPrimitive) AddToList(u *Ulp) error {
207215
}
208216

209217
func (w *WordPrimitive) BuildAssembly(u *Ulp) (string, error) {
210-
out := w.Entry.ulpName + ":" + "\r\n"
218+
label := w.Entry.ulpName + ":\r\n"
219+
bodyLabel := w.Entry.BodyLabel() + ":\r\n"
211220
asm := make([]string, 0)
212221
switch u.compileTarget {
213222
case UlpCompileTargetToken:
@@ -235,7 +244,8 @@ func (w *WordPrimitive) BuildAssembly(u *Ulp) (string, error) {
235244
default:
236245
return "", fmt.Errorf("Unknown compile target %d, please file a bug report", u.compileTarget)
237246
}
238-
out += strings.Join(asm, "\r\n")
247+
asmStr := strings.Join(asm, "\r\n")
248+
out := label + bodyLabel + asmStr
239249
return out, nil
240250
}
241251

0 commit comments

Comments
 (0)