Skip to content

Commit 79ec40c

Browse files
committed
Don't compile subroutine threaded docol if the word is never called.
1 parent 339a423 commit 79ec40c

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

pkg/forth/flag.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ type Flag struct {
2424
// not dependent on current state.
2525
isPure bool
2626
usesReturnStack bool // This primitive word uses the return stack.
27+
28+
calls int // The number of times that this is called, not including tail calls.
2729
}

pkg/forth/ulpBuild.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ func (u *Ulp) buildAssemblyHelper(vm *VirtualMachine, header string) (string, er
171171
if err != nil {
172172
return "", err
173173
}
174+
// count the number of calls
175+
u.countCalls()
174176
// create the different assemblies
175177
asm, err := u.buildAssemblyWords()
176178
if err != nil {
@@ -327,6 +329,32 @@ func (u *Ulp) replaceOtherChars(str string) string {
327329
return sb.String()
328330
}
329331

332+
// Count the number of times that each word is called,
333+
// not including tail calls.
334+
func (u *Ulp) countCalls() {
335+
u.resetCalls()
336+
for _, w := range u.forthWords {
337+
for _, c := range w.Cells {
338+
switch cell := c.(type) {
339+
case CellAddress:
340+
cell.Entry.Flag.calls += 1
341+
}
342+
}
343+
}
344+
}
345+
346+
// Reset the number of times that each word is called.
347+
func (u *Ulp) resetCalls() {
348+
for _, w := range u.forthWords {
349+
for _, c := range w.Cells {
350+
switch cell := c.(type) {
351+
case CellAddress:
352+
cell.Entry.Flag.calls = 0
353+
}
354+
}
355+
}
356+
}
357+
330358
func (u *Ulp) buildInterpreter() string {
331359
i := []string{
332360
// required data, will be placed at the start of .data
@@ -342,8 +370,8 @@ func (u *Ulp) buildInterpreter() string {
342370
"HOST_FUNC: .int 0",
343371
"HOST_PARAM0: .int 0",
344372
".data",
345-
"__ip: .int __forth_VM.INIT", // instruction pointer starts at word VM.INIT
346-
"__rsp: .int __stack_start", // return stack pointer starts at the beginning of the stack section
373+
"__ip: .int __body__forth_VM.INIT", // instruction pointer starts at word VM.INIT
374+
"__rsp: .int __stack_start", // return stack pointer starts at the beginning of the stack section
347375

348376
// boot labels
349377
".boot",

pkg/forth/word.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (w *WordForth) BuildAssembly(u *Ulp) (string, error) {
132132
}
133133
} else { // executable forth word
134134
if u.compileTarget == UlpCompileTargetSubroutine {
135-
if w.Entry.Name != "VM.INIT" { // the init word shouldn't docol
135+
if w.Entry.Flag.calls != 0 { // if this word is directly called
136136
output = append(output, "jump __docol")
137137
}
138138
}

0 commit comments

Comments
 (0)