home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / INTERP.C < prev    next >
C/C++ Source or Header  |  1992-09-06  |  25KB  |  826 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* interp.c */
  21. /* Ghostscript language interpreter */
  22. #include "memory_.h"
  23. #include "ghost.h"
  24. #include "errors.h"
  25. #include "estack.h"
  26. #include "name.h"
  27. #include "dict.h"
  28. #include "dstack.h"
  29. #include "oper.h"
  30. #include "packed.h"
  31. #include "save.h"
  32. #include "stream.h"
  33. #include "store.h"
  34.  
  35. /* Imported operator procedures */
  36. extern int obj_le(P2(os_ptr, os_ptr));
  37. extern int zop_add(P1(os_ptr));
  38. extern int zop_def(P1(os_ptr));
  39. extern int zop_sub(P1(os_ptr));
  40.  
  41. /* 
  42.  * The procedure to call if an operator requests rescheduling.
  43.  * This causes an error unless the context machinery has been installed.
  44.  */
  45. private int no_reschedule() { return e_invalidcontext; }
  46. int (*gs_interp_reschedule_proc)(P0()) = no_reschedule;
  47. /*
  48.  * The procedure to call for time-slicing.
  49.  * This is a no-op unless the context machinery has been installed.
  50.  */
  51. private int no_time_slice() { return 0; }
  52. int (*gs_interp_time_slice_proc)(P0()) = no_time_slice;
  53.  
  54. /* Forward references */
  55. private int interp(P2(ref *pref, ref *perror_object));
  56. private int interp_exit(P1(os_ptr));
  57. private int i_interp_exit;
  58. private int copy_stack(P3(const ref *, uint, ref *));
  59.  
  60. /* Configuration parameters */
  61. #define max_ostack 800
  62. #define max_estack 150
  63. #define max_dstack 20
  64. uint min_dstack_size;        /* set by iinit.c */
  65.  
  66. /* See estack.h for a description of the execution stack. */
  67.  
  68. /* The logic for managing icount and iref below assumes that */
  69. /* there are no control operators which pop and then push */
  70. /* information on the execution stack. */
  71.  
  72. /* Stacks */
  73. #define os_guard_under 10
  74. #define os_guard_over 10
  75. private ref ostack[os_guard_under+max_ostack+os_guard_over];
  76. private ref estack[max_estack];
  77. ref dstack[max_dstack];
  78. os_ptr osp_nargs[os_max_nargs];        /* for checking osp */
  79.  
  80. /* Stack pointers */
  81. os_ptr osbot, osp, ostop;
  82. es_ptr esbot, esp, estop;
  83. es_ptr esfile;                /* cache pointer to currentfile */
  84. ds_ptr dsp, dstop;
  85.  
  86. /* Object related to error handling */
  87. extern ref name_errordict;
  88. extern ref name_ErrorNames;
  89.  
  90. /* Extended types.  The interpreter may replace the type of operators */
  91. /* in procedures with these, to speed up the interpretation loop. */
  92. #define tx_op t_next_index
  93. extern int zadd(P1(os_ptr));
  94. extern int zdef(P1(os_ptr));
  95. extern int zdup(P1(os_ptr));
  96. extern int zexch(P1(os_ptr));
  97. extern int zif(P1(os_ptr));
  98. extern int zifelse(P1(os_ptr));
  99. extern int zindex(P1(os_ptr));
  100. extern int zle(P1(os_ptr));
  101. extern int zpop(P1(os_ptr));
  102. extern int zroll(P1(os_ptr));
  103. extern int zsub(P1(os_ptr));
  104. private const op_proc_p special_ops[] = {
  105.     zadd, zdef, zdup, zexch, zif, zifelse, zindex, zle, zpop, zroll, zsub
  106. };
  107. typedef enum {
  108.     tx_op_add = tx_op,
  109.     tx_op_def,
  110.     tx_op_dup,
  111.     tx_op_exch,
  112.     tx_op_if,
  113.     tx_op_ifelse,
  114.     tx_op_index,
  115.     tx_op_le,
  116.     tx_op_pop,
  117.     tx_op_roll,
  118.     tx_op_sub,
  119.     tx_next_op
  120. } special_op_types;
  121. #define num_special_ops ((int)tx_next_op - tx_op)
  122. #define t_invalid tx_next_op        /* first invalid type */
  123.  
  124. /* Initialize the interpreter */
  125. void
  126. gs_interp_reset()
  127. {    /* Just reset the stacks. */
  128.     osp = osbot - 1;
  129.     esp = estack - 1;
  130.     dsp = dstack + (min_dstack_size - 1);
  131. }
  132. void
  133. interp_init(int ndict)
  134. {    /* Initialize the guard entries on the operand stack */
  135.     /* with objects that have invalid type and attributes. */
  136.     osbot = ostack + os_guard_under;
  137.     osp = osbot - 1, ostop = osbot + (max_ostack-1);
  138.        {    register os_ptr op;
  139.         for ( op = ostack; op < osbot; op++ )
  140.             make_tav(op, t_invalid, 0, index, 0);
  141.        }
  142.        {    register int i;
  143.         for ( i = 1; i <= os_max_nargs; i++ )
  144.             op_nargs_check(i) = osbot + i - 1;
  145.        }
  146.     esbot = estack, esp = estack - 1, estop = estack + (max_estack-1);
  147.     esfile = 0;
  148.     /* Initialize the dictionary stack to the first ndict */
  149.     /* dictionaries.  ndict is a parameter because during */
  150.     /* initialization, only systemdict exists. */
  151.     dsp = dstack + ndict - 1, dstop = dstack + (max_dstack-1);
  152. }
  153.  
  154. /* Look up an operator during initialization, */
  155. /* changing its type if appropriate. */
  156. void
  157. interp_fix_op(ref *opref)
  158. {    register int i = num_special_ops;
  159.     op_proc_p proc = real_opproc(opref);
  160.     while ( --i >= 0 && proc != special_ops[i] ) ;
  161.     if ( i >= 0 )
  162.       make_tav(opref, tx_op + i, a_executable, opproc,
  163.            (dummy_op_proc_p)proc);
  164. }
  165.  
  166. /* Invoke the interpreter.  If execution completes normally, return 0. */
  167. /* if an error occurs, then if user_errors is true and the error is a */
  168. /* recoverable one (not an overflow condition), let the user handle it; */
  169. /* otherwise, return the error code. */
  170. /* In case of a quit or a fatal error, also store the exit code. */
  171. int
  172. gs_interpret(ref *pref, int user_errors, int *pexit_code, ref *perror_object)
  173. {    ref *epref = pref;
  174.     ref erref;
  175.     ref *perrordict, *pErrorNames;
  176.     int code, ccode;
  177.     ref saref;
  178.     /* Push a special exit procedure on the execution stack */
  179.     es_ptr esp0 = ++esp;
  180.     make_oper(esp0, i_interp_exit, (dummy_op_proc_p)interp_exit);
  181.     *pexit_code = 0;
  182. retry:    code = interp(epref, perror_object);
  183.     switch ( code )
  184.     {
  185.     case e_Fatal:
  186.         *pexit_code = 255;
  187.         return code;
  188.     case e_Quit:
  189.         if ( (*pexit_code = (int)osp->value.intval) != 0 )
  190.             code = e_Fatal;
  191.         return code;
  192.     case e_InterpreterExit:
  193.         return 0;
  194.     }
  195.     /* Adjust osp in case of operand stack underflow */
  196.     if ( osp < osbot - 1 )
  197.         osp = osbot - 1;
  198.     if ( !user_errors ) return code;
  199.     if ( dict_find(&systemdict, &name_errordict, &perrordict) <= 0 ||
  200.          dict_find(&systemdict, &name_ErrorNames, &pErrorNames) <= 0
  201.        )
  202.         return code;    /* errordict or ErrorNames not found?? */
  203.     switch ( code )
  204.        {
  205.     case e_dictstackoverflow:
  206.         if ( osp + 1 >= ostop ) return e_stackoverflow;
  207.         ccode = copy_stack(dstack, dsp - dstack + 1, &saref);
  208.         if ( ccode < 0 ) return ccode;
  209.         dsp = &dstack[min_dstack_size - 1];
  210.         *++osp = saref;
  211.         break;
  212.     case e_execstackoverflow:
  213.         if ( osp + 1 >= ostop ) return e_stackoverflow;
  214.         ccode = copy_stack(estack, esp - estack + 1, &saref);
  215.         if ( ccode < 0 ) return ccode;
  216.         esp = esp0;
  217.         *++osp = saref;
  218.         break;
  219.     case e_stackoverflow:
  220.         ccode = copy_stack(ostack, osp - osbot + 1, &saref);
  221.         if ( ccode < 0 ) return ccode;
  222.         osp = osbot;
  223.         *osbot = saref;
  224.         break;
  225.        }
  226.     if ( -code > r_size(pErrorNames) )
  227.         return code;        /* unknown error??? */
  228.     if ( dict_find(perrordict, &pErrorNames->value.refs[-code - 1], &epref) <= 0 )
  229.         return code;        /* error name not in errordict??? */
  230.     erref = *epref;
  231.     epref = &erref;
  232.     /* Push the error object on the operand stack */
  233.     *++osp = *perror_object;
  234.     goto retry;
  235. }    
  236. private int
  237. interp_exit(os_ptr op)
  238. {    return e_InterpreterExit;
  239. }
  240.  
  241. /* Copy the contents of an overflowed stack into an array. */
  242. private int
  243. copy_stack(const ref *stk, uint size, ref *arr)
  244. {    ref *abody = alloc_refs(size, "overflowed stack");
  245.     if ( abody == 0 ) return e_VMerror;
  246.     refcpy_to_new(abody, stk, size);
  247.     make_tasv(arr, t_array, a_all, size, refs, abody);
  248.     return 0;
  249. }
  250.  
  251. /* Main interpreter. */
  252. /* If execution terminates normally, return e_InterpreterExit. */
  253. /* If an error occurs, leave the current object in *perror_object */
  254. /* and return a (negative) error code. */
  255. #define return_with_error(code, objp)\
  256.   { store_state(iesp);\
  257.     esp = iesp; osp = iosp; *perror_object = *(objp);\
  258.     return_error(code);\
  259.   }
  260. #define return_with_error_short(code, objp)\
  261.   { store_state_short(iesp);\
  262.     esp = iesp; osp = iosp; *perror_object = *(objp);\
  263.     return_error(code);\
  264.   }
  265. private int
  266. interp(ref *pref /* object to interpret */, ref *perror_object)
  267. {    register ref *iref = pref;
  268.     register int icount = 0;    /* # of consecutive tokens at iref */
  269.     register os_ptr iosp = osp;    /* private copy of osp */
  270.     register es_ptr iesp = esp;    /* private copy of esp */
  271.     int code;
  272.     ref token;        /* token read from file or string, */
  273.                 /* must be declared in this scope */
  274.     register ref *pvalue;
  275.     os_ptr whichp;
  276.     esfile = 0;        /* clear cache */
  277.     /* From here on, if icount > 0, iref and icount correspond */
  278.     /* to the top entry on the execution stack: icount is the */
  279.     /* count of sequential entries remaining AFTER the current one. */
  280. #define add1_short(pref) (ref *)((ushort *)(pref) + 1)
  281. #define store_state(ep)\
  282.   ( icount > 0 ? (ep->value.refs = iref + 1, r_set_size(ep, icount)) : 0 )
  283. #define store_state_short(ep)\
  284.   ( icount > 0 ? (ep->value.refs = add1_short(iref), r_set_size(ep, icount)) : 0 )
  285. #define next()\
  286.   if ( --icount > 0 ) { iref++; goto top; } else goto out
  287. #define next_short()\
  288.   if ( --icount <= 0 ) { if ( icount < 0 ) goto up; iesp--; }\
  289.   iref = add1_short(iref); goto top;
  290.     /* We want to recognize executable arrays here, */
  291.     /* so we push the argument on the estack and enter */
  292.     /* the loop at the bottom. */
  293.     if ( iesp >= estop ) return_with_error (e_execstackoverflow, pref);
  294.     *++iesp = *pref;
  295.     goto bot;
  296. top:    /*
  297.      * This is the top of the interpreter loop.
  298.      * iref points to the ref being interpreted.
  299.      * Note that this might be an element of a packed array,
  300.      * not a real ref: we carefully arranged the first 16 bits of
  301.      * a ref and of a packed array element so they could be distinguished
  302.      * from each other.  (See ghost.h and packed.h for more detail.)
  303.      */
  304. #ifdef DEBUG
  305. if ( gs_debug['I'] || gs_debug['i'] &&
  306.      (*(ushort *)iref <= packed_max_full_ref ? r_type(iref) == t_name :
  307.       *(short *)iref < 0)
  308.    )
  309.    {    void debug_print_ref(P1(ref *));
  310.     int edepth = iesp - esbot;
  311.     char depth[10];
  312.     sprintf(depth, "%2d", edepth);
  313.     dputs(depth);
  314.     edepth -= strlen(depth);
  315.     do { dputc('.'); } while ( --edepth > 0 );    /* indent */
  316.     dprintf3("%lx(%2d)<%2d>: ",
  317.          (ulong)iref, icount, (uint)(iosp - osbot + 1));
  318.     debug_print_ref(iref);
  319.     if ( iosp >= osbot )
  320.        {    dputs(" // ");
  321.         debug_print_ref(iosp);
  322.        }
  323.     dputc('\n');
  324.     fflush(dstderr);
  325.    }
  326. #endif
  327. /* Object that have attributes (arrays, dictionaries, files, and strings) */
  328. /* use lit and exec; other objects use plain and plain_exec. */
  329. #define lit(t) type_xe_value(t, a_execute)
  330. #define exec(t) type_xe_value(t, a_execute + a_executable)
  331. #define nox(t) type_xe_value(t, 0)
  332. #define nox_exec(t) type_xe_value(t, a_executable)
  333. #define plain(t) type_xe_value(t, 0)
  334. #define plain_exec(t) type_xe_value(t, a_executable)
  335.     /*
  336.      * We have to populate enough cases of the switch statement to force
  337.      * some compilers to use a dispatch rather than a testing loop.
  338.      * What a nuisance!
  339.      */
  340.     switch ( r_type_xe(iref) )
  341.        {
  342.     /* Access errors. */
  343. #define cases_nox()\
  344.   case nox_exec(t_array): case nox_exec(t_dictionary):\
  345.   case nox_exec(t_file): case nox_exec(t_string):\
  346.   case nox_exec(t_mixedarray): case nox_exec(t_shortarray)
  347.     cases_nox():
  348.         return_with_error (e_invalidaccess, iref);
  349.     /*
  350.      * Literal objects.  We have to enumerate all the types.
  351.      * In fact, we have to include some extra plain_exec entries
  352.      * just to populate the switch.  We break them up into groups
  353.      * to avoid overflowing some preprocessors.
  354.      */
  355. #define cases_lit_1()\
  356.   case lit(t_array): case nox(t_array):\
  357.   case plain(t_boolean): case plain_exec(t_boolean):\
  358.   case plain(t_condition): case plain_exec(t_condition):\
  359.   case lit(t_dictionary): case nox(t_dictionary)
  360. #define cases_lit_2()\
  361.   case lit(t_file): case nox(t_file):\
  362.   case plain(t_fontID): case plain_exec(t_fontID):\
  363.   case plain(t_gstate): case plain_exec(t_gstate):\
  364.   case plain(t_integer): case plain_exec(t_integer)
  365. #define cases_lit_3()\
  366.   case plain(t_lock): case plain_exec(t_lock):\
  367.   case plain(t_mark): case plain_exec(t_mark):\
  368.   case plain(t_name):\
  369.   case plain(t_null):\
  370.   case plain(t_oparray):\
  371.   case plain(t_operator)
  372. #define cases_lit_4()\
  373.   case plain(t_real): case plain_exec(t_real):\
  374.   case plain(t_save): case plain_exec(t_save):\
  375.   case lit(t_string): case nox(t_string)
  376. #define cases_lit_5()\
  377.   case lit(t_mixedarray): case nox(t_mixedarray):\
  378.   case lit(t_shortarray): case nox(t_shortarray):\
  379.   case plain(t_device): case plain_exec(t_device)
  380.     cases_lit_1():
  381.     cases_lit_2():
  382.     cases_lit_3():
  383.     cases_lit_4():
  384.     cases_lit_5():
  385.         break;
  386.     /* Special operators. */
  387.     case plain_exec(tx_op_add):
  388. x_add:        if ( (code = zop_add(iosp)) < 0 )
  389.             return_with_error (code, iref);
  390.         iosp--;
  391.         next();
  392.     case plain_exec(tx_op_def):
  393. x_def:        if ( (code = zop_def(iosp)) < 0 )
  394.             return_with_error (code, iref);
  395.         iosp -= 2;
  396.         next();
  397.     case plain_exec(tx_op_dup):
  398. x_dup:        if ( iosp < op_nargs_check(1) )
  399.             return_with_error (e_stackunderflow, iref);
  400.         iosp++;
  401.         ref_assign(iosp, iosp - 1);
  402.         next();
  403.     case plain_exec(tx_op_exch):
  404. x_exch:        if ( iosp < op_nargs_check(2) )
  405.             return_with_error (e_stackunderflow, iref);
  406.         ref_assign(&token, iosp);
  407.         ref_assign(iosp, iosp - 1);
  408.         ref_assign(iosp - 1, &token);
  409.         next();
  410.     case plain_exec(tx_op_if):
  411. x_if:        if ( !r_has_type(iosp - 1, t_boolean) )
  412.           return_with_error (e_typecheck, iref);
  413.         if ( !iosp[-1].value.index )
  414.           { iosp -= 2;
  415.             next();
  416.           }
  417.         if ( iesp >= estop )
  418.           return_with_error (e_execstackoverflow, iref);
  419.         store_state(iesp);
  420.         whichp = iosp;
  421.         iosp -= 2;
  422.         goto ifup;
  423.     case plain_exec(tx_op_ifelse):
  424. x_ifelse:    if ( !r_has_type(iosp - 2, t_boolean) )
  425.             return_with_error (e_typecheck, iref);
  426.         if ( iesp >= estop )
  427.             return_with_error (e_execstackoverflow, iref);
  428.         store_state(iesp);
  429.         whichp = (iosp[-2].value.index ? iosp - 1 : iosp);
  430.         iosp -= 3;
  431.         /* Open code "up" for the array case(s) */
  432. ifup:        switch( r_type_xe(whichp) )
  433.            {
  434.         default:
  435.             /* This is an unusual enough case that we go ahead */
  436.             /* and clear the currentfile cache without */
  437.             /* checking whether we have an exec(t_file), */
  438.             /* since we don't want to add another case */
  439.             /* to the switch. */
  440.             esfile = 0;
  441.             ref_assign(iesp + 1, whichp);
  442.             iref = iesp + 1;
  443.             icount = 0;
  444.             goto top;
  445.         case exec(t_array):
  446.         case exec(t_mixedarray):
  447.         case exec(t_shortarray): ;
  448.            }
  449.         if ( (icount = r_size(whichp) - 1) <= 0 )
  450.            {    if ( icount < 0 ) goto up;    /* 0-element proc */
  451.             iref = whichp->value.refs;    /* 1-element proc */
  452.             goto top;
  453.            }
  454.         ++iesp;
  455.         /* Do a ref_assign, but also set iref. */
  456.         iesp->tas = whichp->tas;
  457.         iref = iesp->value.refs = whichp->value.refs;
  458.         goto top;
  459.     case plain_exec(tx_op_index):
  460. x_index:    if ( (code = zindex(iosp)) < 0 )
  461.             return_with_error (code, iref);
  462.         next();
  463.     case plain_exec(tx_op_le):
  464. x_le:        code = obj_le(iosp - 1, iosp);
  465.         if ( code < 0 )
  466.             return_with_error (code, iref);
  467.         iosp--;
  468.         make_bool(iosp, code);
  469.         next();
  470.     case plain_exec(tx_op_pop):
  471. x_pop:        if ( iosp < op_nargs_check(1) )
  472.             return_with_error (e_stackunderflow, iref);
  473.         iosp--;
  474.         next();
  475.     case plain_exec(tx_op_roll):
  476. x_roll:        if ( (code = zroll(iosp)) < 0 )
  477.             return_with_error (code, iref);
  478.         iosp -= 2;
  479.         next();
  480.     case plain_exec(tx_op_sub):
  481. x_sub:        if ( (code = zop_sub(iosp)) < 0 )
  482.             return_with_error (code, iref);
  483.         iosp--;
  484.         next();
  485.     /* Executable types. */
  486.     case plain_exec(t_null):
  487.         goto bot;
  488.     case plain_exec(t_oparray):
  489.         /* Replace with the definition and go again. */
  490.         pvalue =
  491.           &op_array_table.value.refs[op_index(iref) - op_def_count];
  492. prst:        /* Prepare to call the procedure (array) in *pvalue. */
  493.         store_state(iesp);
  494. pr:        /* Call the array in *pvalue.  State has been stored. */
  495.         if ( (icount = r_size(pvalue) - 1) <= 0 )
  496.            {    if ( icount < 0 ) goto up;    /* 0-element proc */
  497.             iref = pvalue->value.refs;    /* 1-element proc */
  498.             goto top;
  499.            }
  500.         if ( iesp >= estop )
  501.             return_with_error (e_execstackoverflow, pvalue);
  502.         ++iesp;
  503.         /* Do a ref_assign, but also set iref. */
  504.         iesp->tas = pvalue->tas;
  505.         iref = iesp->value.refs = pvalue->value.refs;
  506.         goto top;
  507.     case plain_exec(t_operator):
  508.        {    esp = iesp;        /* save for operator */
  509.         osp = iosp;        /* ditto */
  510.         /* Operator routines take osp as an argument. */
  511.         /* This is just a convenience, since they adjust */
  512.         /* osp themselves to reflect the results. */
  513.         /* Operators that (net) push information on the */
  514.         /* operand stack must check for overflow: */
  515.         /* this normally happens automatically through */
  516.         /* the push macro (in oper.h). */
  517.         /* Operators that do not typecheck their operands, */
  518.         /* or take a variable number of arguments, */
  519.         /* must check explicitly for stack underflow. */
  520.         /* (See oper.h for more detail.) */
  521.         /* Note that each case must set iosp = osp: */
  522.         /* this is so we can switch on code without having to */
  523.         /* store it and reload it (for dumb compilers). */
  524.         switch ( code = (*real_opproc(iref))(iosp) )
  525.            {
  526.         case 0:            /* normal case */
  527.             iosp = osp;
  528.             next();
  529.         case o_push_estack:    /* store the state and go to up */
  530.             iosp = osp;
  531.             store_state(iesp);
  532.             iesp = esp;
  533.             goto up;
  534.         case o_pop_estack:    /* just go to up */
  535.             iosp = osp;
  536.             if ( esp == iesp ) goto bot;
  537.             iesp = esp;
  538.             goto up;
  539.         case o_reschedule:
  540.             store_state(iesp);
  541.             goto res;
  542.         case e_typecheck:
  543.             /* This might be an operand stack */
  544.             /* underflow: check the required # of */
  545.             /* operands now. */
  546.             if ( osp < osbot - 1 + op_num_args(iref) )
  547.                 code = e_stackunderflow;
  548.             /* (falls through) */
  549.            }
  550.         iosp = osp;
  551.         return_with_error (code, iref);
  552.        }
  553.     case plain_exec(t_name):
  554.         pvalue = iref->value.pname->pvalue;
  555.         if ( !pv_valid(pvalue) )
  556.            {    ref *pdvalue;
  557.             if ( (pdvalue = dict_find_name(iref)) == 0 )
  558.                 return_with_error (e_undefined, iref);
  559.             pvalue = pdvalue;
  560.            }
  561.         /* Dispatch on the type of the value. */
  562.         /* Again, we have to over-populate the switch. */
  563.         switch ( r_type_xe(pvalue) )
  564.            {
  565.         cases_nox():    /* access errors */
  566.             return_with_error (e_invalidaccess, iref);
  567.         cases_lit_1():
  568.         cases_lit_2():
  569.         cases_lit_3():
  570.         cases_lit_4():
  571.         cases_lit_5():
  572.             /* Just push the value */
  573.             if ( iosp >= ostop )
  574.                 return_with_error (e_stackoverflow, pvalue);
  575.             ++iosp;
  576.             ref_assign(iosp, pvalue);
  577.             next();
  578.         case exec(t_array):
  579.         case exec(t_mixedarray):
  580.         case exec(t_shortarray):
  581.             /* This is an executable procedure, execute it. */
  582.             goto prst;
  583.         case plain_exec(tx_op_add): goto x_add;
  584.         case plain_exec(tx_op_def): goto x_def;
  585.         case plain_exec(tx_op_dup): goto x_dup;
  586.         case plain_exec(tx_op_exch): goto x_exch;
  587.         case plain_exec(tx_op_if): goto x_if;
  588.         case plain_exec(tx_op_ifelse): goto x_ifelse;
  589.         case plain_exec(tx_op_index): goto x_index;
  590.         case plain_exec(tx_op_le): goto x_le;
  591.         case plain_exec(tx_op_pop): goto x_pop;
  592.         case plain_exec(tx_op_roll): goto x_roll;
  593.         case plain_exec(tx_op_sub): goto x_sub;
  594.         case plain_exec(t_null):
  595.             goto bot;
  596.         case plain_exec(t_oparray):
  597.             pvalue =
  598.               &op_array_table.value.refs[op_index(pvalue) -
  599.                              op_def_count];
  600.             goto prst;
  601.         case plain_exec(t_operator):
  602.            {    /* Shortcut for operators. */
  603.             /* See above for the logic. */
  604.             esp = iesp;
  605.             osp = iosp;
  606.             switch ( code = (*real_opproc(pvalue))(iosp) )
  607.                {
  608.             case 0:            /* normal case */
  609.                 iosp = osp;
  610.                 next();
  611.             case o_push_estack:    /* store the state and go to up */
  612.                 iosp = osp;
  613.                 store_state(iesp);
  614.                 iesp = esp;
  615.                 goto up;
  616.             case o_pop_estack:    /* just go to up */
  617.                 iosp = osp;
  618.                 if ( esp == iesp ) goto bot;
  619.                 iesp = esp;
  620.                 goto up;
  621.             case o_reschedule:
  622.                 store_state(iesp);
  623.                 goto res;
  624.             case e_typecheck:
  625.                 if ( osp < osbot - 1 + op_num_args(pvalue) )
  626.                     code = e_stackunderflow;
  627.                }
  628.             iosp = osp;
  629.             return_with_error (code, pvalue);
  630.            }
  631.         case plain_exec(t_name):
  632.         case exec(t_file):
  633.         case exec(t_string):
  634.         default:
  635.             /* Not a procedure, reinterpret it. */
  636.             store_state(iesp);
  637.             icount = 0;
  638.             iref = pvalue;
  639.             goto top;
  640.            }
  641.     case exec(t_file):
  642.        {    /* Executable file.  Read the next token and interpret it. */
  643.            stream *s;
  644.         code = file_check_read(iref, &s);
  645.         if ( code < 0 ) return_with_error (code, iref);
  646. rt:        if ( iosp >= ostop )    /* check early */
  647.           return_with_error (e_stackoverflow, iref);
  648.         osp = iosp;        /* scan_token uses ostack */
  649.         switch ( code = scan_token(s, 0, (ref *)(iosp + 1)) )
  650.            {
  651.         case 0:            /* read a token */
  652.             /* It's worth checking for literals, which make up */
  653.             /* the majority of input tokens, before storing the */
  654.             /* state on the e-stack.  Note that because of //, */
  655.             /* the token may have *any* type and attributes. */
  656.             switch ( r_type(iosp + 1) )
  657.                {
  658.             case t_name: case t_string:
  659.                 if ( r_has_attr(iosp + 1, a_executable) )
  660.                     break;
  661.             /* Executable arrays aren't executed at the */
  662.             /* top level -- they're treated as literals. */
  663.             case t_array: case t_mixedarray: case t_shortarray:
  664.             case t_integer: case t_real:
  665.                 ++iosp;
  666.                 goto rt;
  667.                }
  668.             store_state(iesp);
  669.             /* Push the file on the e-stack */
  670.             if ( iesp >= estop )
  671.                 return_with_error (e_execstackoverflow, iref);
  672.             ++iesp;
  673.             ref_assign(iesp, iref);
  674.             iref = iosp + 1;
  675.             icount = 0;
  676.             goto top;
  677.         case 1:            /* end of file */
  678.             code = file_close(iref, s);
  679.             if ( code < 0 ) return_with_error (code, iref);
  680.             goto bot;
  681.         default:        /* error */
  682.             return_with_error (code, iref);
  683.            }
  684.        }
  685.     case exec(t_string):
  686.        {    /* Executable string.  Read a token and interpret it. */
  687.         stream ss;
  688.         sread_string(&ss, iref->value.bytes, r_size(iref));
  689.         osp = iosp;        /* scan_token uses ostack */
  690.         switch ( code = scan_token(&ss, 1, &token) )
  691.           {
  692.         case 0:            /* read a token */
  693.             store_state(iesp);
  694.             /* Push the updated string back on the e-stack */
  695.             if ( iesp >= estop )
  696.               return_with_error (e_execstackoverflow, iref);
  697.             ++iesp;
  698.             iesp->tas.type_attrs = iref->tas.type_attrs;
  699.             iesp->value.bytes = ss.cptr + 1;
  700.             r_set_size(iesp, ss.cbuf + ss.bsize - ss.cptr - 1);
  701.             iref = &token;
  702.             icount = 0;
  703.             goto top;
  704.         case 1:            /* end of string */
  705.             goto bot;
  706.         default:        /* error */
  707.             return_with_error (code, iref);
  708.           }
  709.        }
  710.     /* Handle packed arrays here by re-dispatching. */
  711.     /* This also picks up some anomalous cases of non-packed arrays. */
  712.     default:
  713.         switch ( *(ushort *)iref >> packed_type_shift )
  714.            {
  715.         case pt_full_ref:
  716.         case pt_full_ref+1:
  717.             if ( iosp >= ostop )
  718.               return_with_error_short(e_stackoverflow, iref);
  719.             ++iosp;
  720.             /* We know that refs are properly aligned: */
  721.             /* see packed.h for details. */
  722.             ref_assign(iosp, iref);
  723.             next();
  724.         case pt_executable_operator:
  725.            {    uint index = *(ushort *)iref & packed_int_mask;
  726.             op_index_ref(index, &token);
  727.             store_state_short(iesp);
  728.             icount = 0;
  729.             iref = &token;
  730.            }    goto top;
  731.         case pt_integer:
  732.             if ( iosp >= ostop )
  733.               return_with_error_short(e_stackoverflow, iref);
  734.             ++iosp;
  735.             make_int(iosp, (*(short *)iref & packed_int_mask) +
  736.                     packed_min_intval);
  737.             next_short();
  738.         case pt_literal_name:
  739.         case pt_literal_name+1:
  740.             if ( iosp >= ostop )
  741.               return_with_error_short(e_stackoverflow, iref);
  742.             ++iosp;
  743.             name_index_ref((uint)*(ushort *)iref &
  744.                          packed_max_name_index,
  745.                        iosp);
  746.             next_short();
  747.         case pt_executable_name:
  748.         case pt_executable_name+1:
  749.            {    ref nref;
  750.             uint nidx = *(ushort *)iref & packed_max_name_index;
  751.             name_index_ref(nidx, &nref);
  752.             pvalue = nref.value.pname->pvalue;
  753.             if ( !pv_valid(pvalue) )
  754.                {    ref *pdvalue;
  755.                 if ( (pdvalue = dict_find_name_by_index(nidx)) == 0 )
  756.                   return_with_error_short(e_undefined, &nref);
  757.                 pvalue = pdvalue;
  758.                }
  759.             switch ( r_type_xe(pvalue) )
  760.                {
  761.             case exec(t_array):
  762.             case exec(t_mixedarray):
  763.             case exec(t_shortarray):
  764.                 /* This is an executable procedure, */
  765.                 /* execute it. */
  766.                 store_state_short(iesp);
  767.                 goto pr;
  768.             default:        /* handles other literals */
  769.                 /* Not a procedure, reinterpret it. */
  770.                 store_state_short(iesp);
  771.                 icount = 0;
  772.                 iref = pvalue;
  773.                 goto top;
  774.                }
  775.            }
  776.         /* default can't happen here */
  777.            }
  778.        }
  779.     /* Literal type, just push it. */
  780.     if ( iosp >= ostop ) return_with_error (e_stackoverflow, iref);
  781.     ++iosp;
  782.     ref_assign(iosp, iref);
  783. bot:    next();
  784. out:    /* At most 1 more token in the current procedure. */
  785.     /* (We already decremented icount.) */
  786.     if ( !icount )
  787.        {    /* Pop the execution stack for tail recursion. */
  788.         iesp--;
  789.         iref++;
  790.         goto top;
  791.        }
  792. up:    /* See if there is anything left on the execution stack. */
  793.     switch ( r_type_xe(iesp) )
  794.        {
  795.     default:
  796.         iref = iesp--;
  797.         icount = 0;
  798.         goto top;
  799.     case exec(t_array):
  800.     case exec(t_mixedarray):
  801.     case exec(t_shortarray): ;
  802.        }
  803.     iref = iesp->value.refs;        /* next element of array */
  804.     icount = r_size(iesp) - 1;
  805.     if ( icount <= 0 )        /* <= 1 more elements */
  806.        {    iesp--;            /* pop, or tail recursion */
  807.         if ( icount < 0 ) goto up;
  808.        }
  809.     goto top;
  810. res:    /* Some operator has asked for context rescheduling. */
  811.     code = (*gs_interp_reschedule_proc)();
  812.     if ( code < 0 ) return_with_error (code, iref);
  813.     /* Reload state information from memory. */
  814.     iosp = osp;
  815.     iesp = esp;
  816.     goto up;
  817. }
  818.  
  819. /* ------ Initialization procedure ------ */
  820.  
  821. op_def interp_op_defs[] = {
  822.         /* Internal operators */
  823.     {"0%interp_exit", interp_exit, &i_interp_exit},
  824.     op_def_end(0)
  825. };
  826.