home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples1.exe / MyC / Src / Emit.cs < prev    next >
Encoding:
Text File  |  2000-06-23  |  6.1 KB  |  339 lines

  1. namespace MyC
  2. {
  3. /*
  4.  * the emit routines add "instruction" to the instruction stream (istream)
  5.  * currently the istream is flushed when the emitAsm routine is called
  6.  */
  7. using System;
  8. using System.Reflection;
  9.  
  10. class Emit
  11. {
  12. IAsm iroot;            // root of current instruction list
  13. IAsm icur;            // current insn ptr
  14. IAsm LastComment;        // ptr to last comment placeholder
  15.  
  16. VarList localvars;        // local copy of current localvar list
  17.  
  18. Io io;                // local copy of current IO handle
  19.  
  20. Exe exe;            // instance of exe class for generation
  21.  
  22. /*
  23.  * get the function definition entry
  24.  */
  25. public Var GetFunc()
  26.   {
  27.   IAsm a;
  28.   if (iroot == null)
  29.     return null;
  30.   a = iroot;
  31.   while (a != null)
  32.     {
  33.     if (a.getIType() == IAsm.I_FUNC_BEGIN)
  34.       return (a.getVar());
  35.     a = a.getNext();
  36.     }
  37.   return null;            /* didn't find anything */
  38.   }
  39.  
  40. void NextInsn(int incr)
  41.   {
  42.   int ncount = 0;
  43.   if (iroot == null)
  44.     {
  45.     icur = iroot = new IAsm();
  46.     }
  47.   else
  48.     {
  49.     ncount = icur.getICount() + incr;    /* propogate previous count */
  50.     icur.setNext(new IAsm());
  51.     icur = icur.getNext();
  52.     }
  53.   icur.setICount(ncount);
  54.   }
  55.  
  56. /*
  57.  * Emit a field def to ilist
  58.  */
  59. public void FieldDef(Var e)
  60.   {
  61.   NextInsn(0);
  62.   icur.setIType(IAsm.I_FIELD);
  63.   icur.setVar(e);
  64.   }
  65.  
  66. /*
  67.  * Emit function begin to ilist
  68.  */
  69. public void FuncBegin(Var e)
  70.   {
  71.   NextInsn(0);
  72.   icur.setIType(IAsm.I_FUNC_BEGIN);
  73.   icur.setVar(e);
  74.   }
  75.  
  76. /*
  77.  * Emit the local declarations
  78.  */
  79. public void LocalVars(VarList v)
  80.   {
  81.   NextInsn(0);
  82.   localvars = v;
  83.   icur.setIType(IAsm.I_LOCALDEF);
  84.   }
  85.  
  86. /*
  87.  * Emit instruction to ilist
  88.  */
  89. public void Insn(String s)
  90.   {
  91.   NextInsn(1);
  92.   icur.setIType(IAsm.I_INSN);
  93.   icur.setInsn(s);
  94.   }
  95.  
  96. public void Label(String lname)
  97.   {                // this is the branch target
  98.   NextInsn(0);
  99.   icur.setIType(IAsm.I_LABEL);
  100.   icur.setLabel(lname);
  101.   }
  102.  
  103. public void Branch(String s, String lname)
  104.   {                // this is the branch source
  105.   NextInsn(1);
  106.   icur.setIType(IAsm.I_BRANCH);
  107.   icur.setInsn(s);
  108.   icur.setLabel(lname);
  109.   }
  110.  
  111. public void Store(Var e)
  112.   {
  113.   NextInsn(1);
  114.   icur.setIType(IAsm.I_INSN_STORE);
  115.   icur.setVar(e);
  116.   }
  117.  
  118. public void Load(Var e)
  119.   {
  120.   NextInsn(1);
  121.   icur.setIType(IAsm.I_INSN_LOAD);
  122.   icur.setVar(e);
  123.   }
  124.  
  125. public void LoadConst(String s)
  126.   {
  127.   NextInsn(1);
  128.   icur.setIType(IAsm.I_INSN_LOAD_CONST);
  129.   icur.setInsn(s);
  130.   }
  131.  
  132. public void Call(Var e)
  133.   {
  134.   NextInsn(1);
  135.   icur.setIType(IAsm.I_CALL);
  136.   icur.setVar(e);            /* this is the callname */
  137.   }
  138.  
  139. public void Finish()
  140.   {
  141.   iroot = icur = null;
  142.   }
  143.  
  144. public void CommentHolder()
  145.   {
  146.   NextInsn(0);
  147.   icur.setIType(IAsm.I_COMMENT);
  148.   LastComment = icur;        /* save away this insn loc to store comment */
  149.   icur.setCommentLine(io.commentGetCurrentLine());
  150.   }
  151.  
  152. public void CommentFill(String comment)
  153.   {
  154. #if DEBUG
  155.   Console.Write("CommentFill S=");
  156.   for (int _debug_i=0; _debug_i<comment.Length;_debug_i++)
  157.     {
  158.     int _debug_d = comment[_debug_i];
  159.     char _debug_c = (char) (_debug_d + 96);
  160.     if (_debug_d < 32)
  161.       Console.Write("^"+Char.ToString(_debug_c));
  162.     else
  163.       Console.Write(comment[_debug_i]);
  164.     Console.Write("[");
  165.     Console.Write(Int32.ToString(_debug_d));
  166.     Console.Write("],");
  167.     }
  168.   Console.WriteLine(";");
  169. #endif
  170.   if (LastComment != null)
  171.     LastComment.setComment(comment);
  172.   }
  173.  
  174. public void Ret()
  175.   {
  176.   NextInsn(1);
  177.   icur.setIType(IAsm.I_RET);
  178.   }
  179.  
  180. public void FuncEnd()
  181.   {
  182.   NextInsn(0);
  183.   icur.setIType(IAsm.I_FUNC_END);
  184.   }
  185.  
  186. /*
  187.  * Emit exe instructions now
  188.  * this flushes the istream
  189.  */
  190. public void IL()
  191.   {
  192.   IAsm a = iroot;
  193.   IAsm p;
  194.  
  195.   while (a != null)
  196.     {
  197.     switch (a.getIType())
  198.       {
  199.       case IAsm.I_INSN:
  200.     exe.Insn(a);
  201.     break;
  202.       case IAsm.I_LABEL:
  203.     exe.Label(a);
  204.     break;
  205.       case IAsm.I_BRANCH:
  206.     exe.Branch(a);
  207.     break;
  208.       case IAsm.I_INSN_STORE:
  209.     exe.Store(a);
  210.     break;
  211.       case IAsm.I_INSN_LOAD:
  212.     exe.Load(a);
  213.     break;
  214.       case IAsm.I_INSN_LOAD_CONST:
  215.     exe.LoadConst(a);
  216.     break;
  217.       case IAsm.I_FUNC_BEGIN:
  218.     exe.FuncBegin(a); /* Emit function beginning */
  219.     break;
  220.       case IAsm.I_FUNC_END:
  221.     exe.FuncEnd();
  222.     break;
  223.       case IAsm.I_CALL:
  224.     exe.Call(a);
  225.     break;
  226.       case IAsm.I_RET:
  227.     exe.Ret(a);
  228.     break;
  229.       case IAsm.I_FIELD:
  230.     exe.FieldDef(a);
  231.     break;
  232.       case IAsm.I_LOCALDEF:
  233.     exe.LocalVars(localvars);
  234.     break;
  235.       case IAsm.I_COMMENT:
  236.     exe.Comment(a);
  237.     break;
  238.       default:
  239.     io.Abort("Unhandled instruction type " + Int32.ToString(a.getIType()));
  240.       }
  241.     p = a;
  242.     a = a.getNext();
  243.     }
  244.   }
  245.  
  246. /*
  247.  * Emit assembly instructions now
  248.  * this flushes the istream
  249.  */
  250. public void LIST()
  251.   {
  252.   IAsm a = iroot;
  253.   IAsm p;
  254.   Asm x = new Asm(io);
  255.  
  256.   while (a != null)
  257.     {
  258.     switch (a.getIType())
  259.       {
  260.       case IAsm.I_INSN:
  261.     x.Insn(a);
  262.     break;
  263.       case IAsm.I_LABEL:
  264.     x.Label(a);
  265.     break;
  266.       case IAsm.I_BRANCH:
  267.     x.Branch(a);
  268.     break;
  269.       case IAsm.I_INSN_STORE:
  270.     x.Store(a);
  271.     break;
  272.       case IAsm.I_INSN_LOAD:
  273.     x.Load(a);
  274.     break;
  275.       case IAsm.I_INSN_LOAD_CONST:
  276.     x.LoadConst(a);
  277.     break;
  278.       case IAsm.I_FUNC_BEGIN:
  279.     x.FuncBegin(a); /* Emit function beginning */
  280.     break;
  281.       case IAsm.I_FUNC_END:
  282.     x.FuncEnd();
  283.     break;
  284.       case IAsm.I_CALL:
  285.     x.Call(a);
  286.     break;
  287.       case IAsm.I_RET:
  288.     x.Ret(a);
  289.     break;
  290.       case IAsm.I_COMMENT:
  291.     x.Comment(a);
  292.     break;
  293.       case IAsm.I_FIELD:
  294.     x.FieldDef(a);
  295.     break;
  296.       case IAsm.I_LOCALDEF:
  297.     x.LocalVars(localvars);
  298.     break;
  299.       default:
  300.     io.Abort("Unhandled instruction type " + Int32.ToString(a.getIType()));
  301.       }
  302.     p = a;
  303.     a = a.getNext();
  304.     }
  305.   }
  306.  
  307. public void BeginModule()
  308.   {
  309.   exe.BeginModule(io.GetInputFilename());
  310.   }
  311.  
  312. public void BeginClass()
  313.   {
  314.   exe.BeginClass(Io.GetClassname(), TypeAttributes.Public);
  315.   if (Io.genlist)
  316.     io.Out(".class " + Io.GetClassname() + "{\n");
  317.   }
  318.  
  319. public void EndClass()
  320.   {
  321.   exe.EndClass();
  322.   if (Io.genlist)
  323.     io.Out("}\n");
  324.   }
  325.  
  326. public void EndModule()
  327.   {
  328.   exe.EndModule();
  329.   }
  330.  
  331. public Emit(Io o)
  332.   {
  333.   io = o;
  334.   exe = new Exe(Io.GetClassname());
  335.   }
  336.  
  337. }
  338. }
  339.