home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / m / mawk11as.zip / CODE.C < prev    next >
C/C++ Source or Header  |  1991-12-18  |  4KB  |  162 lines

  1.  
  2. /********************************************
  3. code.c
  4. copyright 1991, Michael D. Brennan
  5.  
  6. This is a source file for mawk, an implementation of
  7. the AWK programming language.
  8.  
  9. Mawk is distributed without warranty under the terms of
  10. the GNU General Public License, version 2, 1991.
  11. ********************************************/
  12.  
  13.  
  14. /* $Log:    code.c,v $
  15.  * Revision 5.1  91/12/05  07:55:43  brennan
  16.  * 1.1 pre-release
  17.  * 
  18. */
  19.  
  20. /*  code.c  */
  21.  
  22. #include "mawk.h"
  23. #include "code.h"
  24. #include "init.h"
  25. #include "jmp.h"
  26. #include "field.h"
  27.  
  28.  
  29. #define   MAIN_CODE_SZ  (MAIN_PAGE_SZ*sizeof(INST))
  30.  
  31. INST *code_ptr  ;
  32. INST *main_start , *main_code_ptr ;
  33.  
  34. #if 0
  35. INST *begin_start , *begin_code_ptr ;
  36. INST *end_start , *end_code_ptr ;
  37. #endif 
  38. unsigned main_size /*, begin_size, end_size*/ ;
  39.     /* when the code is done executing its freed,
  40.        that's why this is global */
  41.  
  42. struct be_code begin_code , end_code ;
  43.  
  44. void  PROTO(fdump, (void) ) ;
  45.  
  46. void  be_expand( p )
  47.   struct be_code *p ;
  48. { int delta ;
  49.  
  50.   main_code_ptr = code_ptr ;
  51.  
  52.   if ( p->start )
  53.   {
  54.     delta = p->ptr - p->start ;
  55.     p->start = (INST*) zrealloc(p->start, INST_BYTES(p->size),
  56.             INST_BYTES(PAGE_SZ)) ;
  57.   }
  58.   else
  59.   {
  60.     delta = 0 ;
  61.     p->start = (INST*) zmalloc(INST_BYTES(PAGE_SZ)) ;
  62.   }
  63.  
  64.   p->size = PAGE_SZ ;
  65.   code_ptr = p->start + delta ;
  66. }
  67.  
  68. void  be_shrink(p)
  69.   struct be_code *p ;
  70. {
  71.   int delta = code_ptr - p->start ;
  72.  
  73.   code_ptr = main_code_ptr ;
  74.  
  75.   if ( delta > p->size )
  76.     overflow( p == & begin_code ?
  77.           "BEGIN code" : "END code" , p->size) ;
  78.  
  79.   p->start = (INST*) zrealloc(p->start, INST_BYTES(p->size) ,
  80.             INST_BYTES(delta+2)) ;
  81.  
  82.   p->ptr = p->start + delta ;
  83.   p->size = delta + 2 ;
  84. }
  85.     
  86.  
  87. void  code_init()
  88.   code_ptr = main_code_ptr = main_start 
  89.     = (INST *) zmalloc(MAIN_CODE_SZ) ;
  90.  
  91.   code1(_OMAIN) ; 
  92. }
  93.  
  94. void code_cleanup()
  95. { int some_code_flag = 0 ; /* might only have functions */ 
  96.  
  97.  
  98.   /* set the END code */
  99.   if ( end_code.start )
  100.   { 
  101.     end_code.ptr++ -> op =  _EXIT0 ;
  102.     end_code.ptr++ -> op =  _HALT  ;
  103.     end_code.size = INST_BYTES(end_code.size) ;
  104.   }
  105.  
  106.   /* set the main code */
  107.   if ( end_code.start || code_ptr - main_start > 1 )
  108.   { int gl_offset = code_ptr - main_start ;
  109.     extern INST *next_label ;
  110.     extern int NR_flag ;
  111.  
  112.     if ( NR_flag )  code1(OL_GL_NR) ;
  113.     else code1(OL_GL) ;
  114.  
  115.     code1(_HALT) ;
  116.  
  117.     main_size = code_ptr - main_start ;
  118.     if ( main_size > MAIN_PAGE_SZ )
  119.            overflow("MAIN code" , MAIN_PAGE_SZ) ;
  120.     main_size *= sizeof(INST) ;
  121.     code_ptr = main_start =
  122.                (INST*) zrealloc(main_start, MAIN_CODE_SZ, main_size) ;
  123.  
  124.     next_label = main_start+gl_offset ;
  125.     some_code_flag = 1 ;
  126.   }
  127.   else  /* only BEGIN */
  128.   {
  129.     zfree(main_start, MAIN_CODE_SZ) ;
  130.     main_start = (INST*) 0 ;
  131.   }
  132.  
  133.   /* set the BEGIN code */
  134.   if ( begin_code.start )
  135.   { 
  136.     some_code_flag = 1 ;
  137.     begin_code.ptr++ -> op = main_start ? _JMAIN : _EXIT0 ;
  138.     begin_code.ptr++ -> op = _HALT ;
  139.     begin_code.size = INST_BYTES(begin_code.size) ;
  140.  
  141.     /* execution starts at code_ptr */
  142.     code_ptr = begin_code.start ;
  143.   }
  144.  
  145. #if ! SM_DOS
  146.   if ( dump_code )
  147.   {
  148.     fdump() ; /* dumps all user functions */
  149.     if ( begin_code.start )
  150.     { fprintf(stderr, "BEGIN\n") ; da(begin_code.start, stderr) ; }
  151.     if ( end_code.start )
  152.     { fprintf(stderr, "END\n") ; da(end_code.start, stderr) ; }
  153.     if ( main_start )
  154.     { fprintf(stderr, "MAIN\n") ; da(main_start, stderr) ; }
  155.   }
  156. #endif
  157.  
  158.   if ( some_code_flag == 0 ) mawk_exit(0) ;
  159. }
  160.  
  161.