@@ -332,3 +332,42 @@ func (c *CellBranch0) IsRecursive(check *WordForth) bool {
332332func (c * CellBranch0 ) String () string {
333333 return fmt .Sprintf ("Branch0{%p}" , c .dest )
334334}
335+
336+ // Used during an optimization pass to add
337+ // tail calls.
338+ type CellTailCall struct {
339+ dest * WordForth
340+ }
341+
342+ func (c * CellTailCall ) Execute (vm * VirtualMachine ) error {
343+ return fmt .Errorf ("Cannot directly execute a tail call, please file a bug repot" )
344+ }
345+
346+ func (c * CellTailCall ) AddToList (u * Ulp ) error {
347+ // Add the destination to the list.
348+ return c .dest .AddToList (u )
349+ }
350+
351+ func (c * CellTailCall ) BuildExecution (u * Ulp ) (string , error ) {
352+ switch u .compileTarget {
353+ case UlpCompileTargetToken :
354+ return fmt .Sprintf (".int %s + 0x8000" , c .dest .Entry .ulpName ), nil
355+ case UlpCompileTargetSubroutine :
356+ // put the address after the docol
357+ return fmt .Sprintf ("move r2, %s + 1\r \n jump r2" , c .dest .Entry .ulpName ), nil
358+ default :
359+ return "" , fmt .Errorf ("Unknown compile target %d, please file a bug report" , u .compileTarget )
360+ }
361+ }
362+
363+ func (c * CellTailCall ) OutputReference (u * Ulp ) (string , error ) {
364+ return "" , fmt .Errorf ("Cannot refer to a tail call, please file a bug report" )
365+ }
366+
367+ func (c * CellTailCall ) IsRecursive (check * WordForth ) bool {
368+ return c .dest .IsRecursive (check )
369+ }
370+
371+ func (c * CellTailCall ) String () string {
372+ return fmt .Sprintf ("TailCall{%s}" , c .dest .Entry .Name )
373+ }
0 commit comments