Changeset 2352


Ignore:
Timestamp:
Feb 8, 2007, 10:27:33 PM (14 years ago)
Author:
yura
Message:
  • Advances in Java-bytecode generation.
  • Printing INPUTs In ASI.
Location:
to-imperative/trunk/compiler
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • to-imperative/trunk/compiler/rfp_asail_jbc.rf

    r2351 r2352  
    1717$box Exports;
    1818
    19 $table Inputs;
    20 
    2119$box Module-Name;
    2220
     
    2422
    2523$box Entry-Name;
    26 
    27 $box Current-Namespace;
    2824
    2925$box Func-Name;
     
    4541$func Step-To-JBC e.step-operators = ;
    4642
    47 $func Const-Expr-To-JBC e.ASAIL-const-expr = e.aux-arrays (e.JAVA-const-expr);
     43$func Const-Expr-To-JBC e.ASAIL-const-expr = ;
    4844
    4945$func Expr-Args-To-JBC e.args = e.aux-arrays (e.java-args);
     
    5147$func Symbol-To-JBC s.RFP-Symbol = e.JAVA-String;
    5248
    53 $func Name-To-JBC t.name = e.JAVA-Name;
    54 
    5549$func Var-To-JBC t.var = ;
    5650
     
    6155$func Access-Mode t.name = s.jbc-access-mode;
    6256
    63 $box Free-Idx;
    64 
    65 $func Free-Index = e.free-index;
    66 
    67 $func Declare-Results (e.ress) e.acc-java-ress = e.decls (e.java-ress);
     57$box Constants;
     58
     59$box MVbox;
     60
     61$func MV = s;
     62
     63MV, <? &MVbox> : s.mv = s.mv;
    6864
    6965RFP-ASAIL-To-JBC (e.module-name) (e.package) (e.exports) e.module =
    70   <Store &Free-Idx>,
    71   <RFP-Clear-Table &Inputs>,
     66  <Store &Constants>,
    7267  {
    7368    <Store &Int <Lookup &RFP-Options INT>>;
     
    7570  },
    7671  <Store &Exports e.exports>,
    77   <Store &Current-Namespace e.module-name>,
    7872  {
    7973    <Store &Entry <Lookup &RFP-Options ENTRIES>>;
     
    8377  <Concat <Map! &To-Word (<Separate (<? &Dir-Separator>) e.package> <Separate ('.') <Rfp2Java e.module-name>>)>>
    8478  :: e.java-module-name,
    85   <Store &Module-Name <Intersperse ('.') e.java-module-name>>,
     79  <Store &Module-Name e.java-module-name>,
    8680  <ClassWriter <"+" &"COMPUTE_MAXS" &"COMPUTE_FRAMES">> :: s.cw,
    87   //<ASAIL-To-JBC-Temp s.cw e.module> : e,
    88   /*{
    89     <? &Entry-Name> : v.name =
     81  /*<ASAIL-To-JBC-Temp s.cw e.module> : e,
    9082      ()
    9183          ('public static void main (java.lang.String[] args) {' (
     
    10092  <"visit" s.cw 46 &"ACC_PUBLIC" <To-Word <Intersperse ('/') e.java-module-name>> <Null> "java/lang/Object" ()>,
    10193  <Module-To-JBC s.cw e.module>,
     94
     95  {
     96    <? &Entry-Name> : s.name =
     97      <Store &MVbox <"visitMethod" s.cw <"+" &"ACC_PUBLIC" &"ACC_STATIC"> "main" "([Ljava/lang/String;)V" <Null> ()>>,
     98      <MVvisitTypeInsn <MV> &NEW "org/refal/plus/Result">,
     99      <MVvisitInsn <MV> &DUP>,
     100      <MVvisitMethodInsn <MV> &INVOKESPECIAL "org/refal/plus/Result" "<init>" "()V">,
     101      <MVvisitMethodInsn <MV> &INVOKESTATIC <To-Word <Intersperse ('/') e.java-module-name>> s.name "(Lorg/refal/plus/Result;)V">,
     102      <MVvisitInsn <MV> &RETURN>,
     103      <MVvisitMaxs <MV> 0 0>,
     104      <MVvisitEnd <MV>>;;
     105  },
     106
     107  <Store &MVbox <"visitMethod" s.cw &"ACC_STATIC" "<clinit>" "()V" <Null> ()>>,
     108  {
     109    <? &Constants> : e (s.const-name e.expr) e =
     110      <Const-Expr-To-JBC e.expr>,
     111      <MVvisitFieldInsn <MV> &PUTSTATIC <To-Word <Intersperse ('/') e.java-module-name>> s.const-name "Lorg/refal/plus/Expr;">;
     112  },
     113  <MVvisitInsn <MV> &RETURN>,
     114  <MVvisitMaxs <MV> 0 0>,
     115  <MVvisitEnd <MV>>,
     116
    102117  <"visitEnd" s.cw>,
    103118  <"toByteArray" s.cw> e.java-module-name;
     
    105120$table Vars;
    106121$table Iter-Vars;
     122$table TMP-Vars;
    107123$box Var-Idx;
    108124
     
    112128$func Bind-Iter-Var e.expr t.var1 t.var2 = s.i;
    113129$func Lookup-Iter-Var e.expr = s.i s.i1 s.i2;
     130$func Bind-TMP-Var t.var = s.i;
     131$func Lookup-TMP-Var t.var = s.i;
    114132
    115133Bind-Vars s.type e.vars =
     
    138156Lookup-Iter-Var e.expr = <Lookup &Iter-Vars e.expr> : s.i s.i1 s.i2 = s.i s.i1 s.i2;
    139157
     158Bind-TMP-Var t.var =
     159  <? &Var-Idx> : s.i,
     160  <Store &Var-Idx <"+" s.i 1>>,
     161  <Bind &TMP-Vars (t.var) (s.i)>,
     162  s.i;
     163
     164Lookup-TMP-Var t.var = <Lookup &TMP-Vars t.var> : s.i = s.i;
    140165
    141166$table Labels;
     
    151176Lookup-Label t.label = <Lookup &Labels t.label> : s.l = s.l;
    152177
    153 $box MVbox;
    154 
    155 $func MV = s;
    156 
    157 MV, <? &MVbox> : s.mv = s.mv;
     178$func Qualified-Module-Name s.module-name = s.qualified-module-name;
     179Qualified-Module-Name s.module-name =
     180  <Lookup &RFP-Includes s.module-name> : {
     181    BOOT e = 'org/refal/plus/library/';
     182    LOCAL e.path,
     183      <? &RFP-Include-Path> : e (e.dir) e,
     184      e.path : \{
     185        e.dir = /*empty*/;
     186        e.dir s.dir-separator e.p = e.p;
     187      } :: e.p,
     188      <Subst (<? &Dir-Separator>) (('/')) e.p>;
     189  } :: e.p,
     190  <To-Word e.p <Rfp2Java s.module-name>>;
    158191
    159192Module-To-JBC s.cw e.module, {
     
    166199      <Clear-Table &Vars>,
    167200      <Clear-Table &Iter-Vars>,
     201      <Clear-Table &TMP-Vars>,
    168202      <Clear-Table &Labels>,
    169203      <Store &Var-Idx 0>,
     
    171205      <Bind-Vars Result e.ress>,
    172206      <"+" &"ACC_STATIC" <Access-Mode t.name>> :: s.accessMode,
    173       <To-Word <Name-To-JBC t.name>> :: s.funcName,
     207      t.name : (s s.funcName),
     208      { <? &Entry> : e t.name e = <Store &Entry-Name <To-Word <Rfp2Java s.funcName>>>;; },
     209      <To-Word <Rfp2Java s.funcName>> :: s.funcName,
    174210      <To-Word '(' <Replicate <Length e.args> "Lorg/refal/plus/Expr;">
    175211        <Replicate <Length e.ress> "Lorg/refal/plus/Result;">')' s.retType> :: s.desc,
     
    184220    (CONSTEXPR s.linkage t.name (e.comment) e.expr) =
    185221      <"+" &"ACC_STATIC" <"+" &"ACC_FINAL" <Access-Mode t.name>>> :: s.accessMode,
    186       {
    187         t.name : (STATIC e) = <Rfp2Java t.name>;
    188         //<RFP-Extract-Qualifiers t.name> :: (e.qualifiers) e.name = e.name;
    189         <Name-To-JBC t.name>;
     222      t.name : {
     223        (STATIC (e)) = <Rfp2Java t.name>;
     224        (s s.const-name) = <Rfp2Java s.const-name>;
    190225      } :: e.n,
    191       <"visitField" s.cw s.accessMode <To-Word e.n> "Lorg/refal/plus/Expr;" <Null> <Null>> :: s.fv,
    192       <FVvisitEnd s.fv>;
     226      <To-Word e.n> :: s.n,
     227      <"visitField" s.cw s.accessMode s.n "Lorg/refal/plus/Expr;" <Null> <Null>> :: s.fv,
     228      <FVvisitEnd s.fv>,
     229      <Put &Constants (s.n e.expr)>;
    193230      //<Const-Expr-To-JBC e.expr> :: e.a (e.j-expr),
    194231      //e.a ('static '<Access-Mode t.name>' final Expr 'e.n' = 'e.j-expr';');
    195232    (OBJ s.linkage s.tag t.name) =
     233      t.name : (s s.obj-name),
    196234      <To-Chars s.tag> : s1 e2,
    197235      'Named' s1 <To-Lower e2> :: e.class-name,
    198       <Bind &Inputs ('org.refal.plus.library.'e.class-name) ()>,
    199236      <"+" &"ACC_STATIC" <"+" &"ACC_FINAL" <Access-Mode t.name>>> :: s.accessMode,
    200       <"visitField" s.cw s.accessMode <To-Word <Name-To-JBC t.name>> "Lorg/refal/plus/Expr;" <Null> <Null>> :: s.fv,
     237      <"visitField" s.cw s.accessMode <To-Word <Rfp2Java s.obj-name>> "Lorg/refal/plus/Expr;" <Null> <Null>> :: s.fv,
    201238      <FVvisitEnd s.fv>;
    202       //<RFP-Extract-Qualifiers t.name> :: (e.qualifiers) e.n,
    203239      //('static '<Access-Mode t.name>' final Expr '<Name-To-JBC t.name>
    204240      //  ' = new Expr (new 'e.class-name' ("'e.n'"));');
     
    217253      s.type : {
    218254        Result =
    219           <MVvisitTypeInsn <MV> &NEW "org/refal/plus/Expr">,
     255          <MVvisitTypeInsn <MV> &NEW "org/refal/plus/Result">,
    220256          <MVvisitInsn <MV> &DUP>,
    221257          <MVvisitMethodInsn <MV> &INVOKESPECIAL "org/refal/plus/Result" "<init>" "()V">,
     
    326362      ('try') ('{' (
    327363        ('if (false)'
    328           ('throw new RefalException ("'<? &Module-Name>'", "'<? &Func-Name>'", '
     364          ('throw new RefalException ("'<Intersperse ('.') <? &Module-Name>>'", "'<? &Func-Name>'", '
    329365            '"This is for avoiding \'Unreachable code\' errors");'
    330366        )   )
     
    337373      ) '}') : e;
    338374    FATAL =
    339       ('throw new RefalException ("'<? &Module-Name>'", "'<? &Func-Name>'", "Unexpected fail");') : e;
     375      ('throw new RefalException ("'<Intersperse ('.') <? &Module-Name>>'", "'<? &Func-Name>'", "Unexpected fail");') : e;
    340376    (ERROR e.expr) =
    341377      <Expr-Ref-To-JBC e.expr>;
    342378      //('throw new RefalException ('e.j-expr');') : e;
    343     /*
    344      * s.call can be CALL or TAILCALL or TAILCALL?
    345      */
    346     (s.call t.name (e.args) (e.ress)) =
    347       <Declare-Results (e.ress)> :: e.decls (e.ress),
    348       <Expr-Args-To-JBC e.args Result e.ress> :: e.arrays (e.args),
    349       <Name-To-JBC t.name>' ('e.args')' :: e.c,
     379    (s.call (s.module s.name) (e.args) (e.ress)),
     380      s.call : \{ CALL = V; TAILCALL = V; TAILCALL? = Z; } :: s.retType =
     381      <Qualified-Module-Name s.module> :: s.module,
     382      <To-Word <Rfp2Java s.name>> :: s.name,
     383      <To-Word '(' <Replicate <Length e.args> "Lorg/refal/plus/Expr;">
     384        <Replicate <Length e.ress> "Lorg/refal/plus/Result;">')' s.retType> :: s.desc,
     385      {
     386        e.args : e (e.arg) e,
     387          <Expr-Ref-To-JBC e.arg>,
     388          $fail;;
     389      },
     390      {
     391        e.ress : e t.var e, <Lookup-Var t.var> : Expr s,
     392          <Bind-TMP-Var t.var> :: s.j,
     393          <MVvisitTypeInsn <MV> &NEW "org/refal/plus/Result">,
     394          <MVvisitInsn <MV> &DUP>,
     395          <MVvisitInsn <MV> &DUP>,
     396          <MVvisitMethodInsn <MV> &INVOKESPECIAL "org/refal/plus/Result" "<init>" "()V">,
     397          <MVvisitVarInsn <MV> &ASTORE s.j>,
     398          $fail;;
     399      },
     400      <MVvisitMethodInsn <MV> &INVOKESTATIC s.module s.name s.desc>,
    350401      {
    351402        s.call : TAILCALL? =
    352           e.arrays e.decls ('if (!'e.c') {' ('return false;') '}');
    353         e.arrays e.decls (e.c';') <? &Res-Assigns>
    354           <Store &Res-Assigns /*empty*/>;
    355       } : e;
     403          <Label> :: s.label,
     404          <MVvisitJumpInsn <MV> &IFNE s.label>,
     405          <MVvisitInsn <MV> &"ICONST_0">,
     406          <MVvisitInsn <MV> &IRETURN>,
     407          <MVvisitLabel <MV> s.label>;;
     408      },
     409      {
     410        e.ress : e t.var e, <Lookup-Var t.var> : Expr s.i,
     411          <Lookup-TMP-Var t.var> :: s.j,
     412          <MVvisitVarInsn <MV> &ALOAD s.j>
     413          <MVvisitMethodInsn <MV> &INVOKEVIRTUAL "org/refal/plus/Result" "getExpr" "()Lorg/refal/plus/Expr;">,
     414          <MVvisitVarInsn <MV> &ASTORE s.i>,
     415          $fail;;
     416      };
    356417  }, $fail; e;
    357418};
    358419
    359420ASAIL-To-JBC-Temp e = ;
    360 
    361 Declare-Results {
    362   (t.var e.r) e.ress, {
    363     <In-Table? &Result t.var> = <Declare-Results (e.r) e.ress (<Rfp2Java t.var>)>;
    364     <? &Ress> : e t.var e     = <Declare-Results (e.r) e.ress (<Rfp2Java t.var>)>;
    365     '_va_' <Free-Index> :: e.new-var,
    366       <Put &Res-Assigns (<Rfp2Java t.var>' = 'e.new-var'.getExpr ();')> =
    367       ('Result 'e.new-var' = new Result ();') <Declare-Results (e.r) e.ress (e.new-var)>;
    368   };
    369   () e.ress = (e.ress);
    370 };
    371421
    372422$func Term-Ref-To-JBC t.term = ;
     
    414464    <Expr-Int-To-JBC e.len>,
    415465    <MVvisitMethodInsn <MV> &INVOKESPECIAL "org/refal/plus/Expr" "<init>" "(Lorg/refal/plus/Expr;II)V">;
    416   (REF t.name) = <Name-To-JBC t.name> :e;
     466  t.term, t.term : \{
     467    (REF (s.module s.name)) = <Qualified-Module-Name s.module> s.name;
     468    (STATIC (e)) = <To-Word <Intersperse ('/') <? &Module-Name>>> t.term;
     469  } :: s.module t.name =
     470    <To-Word <Rfp2Java t.name>> :: s.name,
     471    <MVvisitFieldInsn <MV> &GETSTATIC s.module s.name "Lorg/refal/plus/Expr;">;
    417472  //ERROR-EXPR = 'err';
    418   t.term, t.term : (s.var-tag e) = <Var-To-JBC t.term>;
     473  t.term, t.term : (s.var-tag e),
     474    s.var-tag : \{ VAR; SVAR; TVAR; EVAR; VVAR; } = <Var-To-JBC t.term>;
    419475};
    420476
     
    510566
    511567Const-Expr-To-JBC {
    512   /*empty*/ = ('Expr.empty');
    513   (SUBEXPR t.name s.pos s.len) = ('new Expr ('<Rfp2Java t.name>', 's.pos', 's.len')');
     568  /*empty*/ = <MVvisitFieldInsn <MV> &GETSTATIC "org/refal/plus/Expr" "empty" "Lorg/refal/plus/Expr;">;
     569  /*(SUBEXPR t.name s.pos s.len) =
     570    t.name : \{
     571      (REF (s.module s.name)) = <Qualified-Module-Name s.module> s.name;
     572      (STATIC (e)) = <To-Word <Intersperse ('/') <? &Module-Name>>> t.term;
     573    } :: s.module t.name =
     574    <To-Word <Rfp2Java t.name>> :: s.name,
     575    <MVvisitFieldInsn <MV> &GETSTATIC s.module s.name "Lorg/refal/plus/Expr;">,
     576    <MVvisitLdcInsn <MV> <Integer s.pos>>,
     577    <MVvisitLdcInsn <MV> <Integer s.len>>,
     578    <MVvisitMethodInsn <MV> &INVOKESPECIAL "org/refal/plus/Expr" "<init>" "(Lorg/refal/plus/Expr;II)V">;
    514579                  //FIXME: надо проверять, что s.pos и s.len
    515580                  //       не превышают допустимых величин.
     
    517582  e.expr =
    518583    <Const-Expr-Aux () e.expr> : {
     584    <MVvisitLdcInsn <MV> <Integer <Length e.expr>>>,
     585    <MVvisitTypeInsn <MV> &ANEWARRAY "org/refal/plus/Expr">,
     586    0 e.expr $iter {
     587      e.expr : t.term e.rest,
     588        <MVvisitInsn <MV> &DUP>,
     589        <MVvisitLdcInsn <MV> <Integer s.i>>,
     590        <Term-Ref-To-JBC t.term>,
     591        <MVvisitInsn <MV> &AASTORE>,
     592        <"+" s.i 1> e.rest;
     593    } :: s.i e.expr,
     594    e.expr : empty =
     595    <MVvisitMethodInsn <MV> &INVOKESTATIC "org/refal/plus/Expr" "concat" "([Lorg/refal/plus/Expr;)Lorg/refal/plus/Expr;">;*/
     596
     597
     598
     599
     600  /*e.expr =
     601    <Const-Expr-Aux () e.expr> : {
    519602      (e.arrays) (e1)      = e.arrays (e1);
    520603      (e.arrays) (e1) (e2) = e.arrays ('new Expr ('e1', 'e2')');
    521604      (e.arrays) e.concat  =
    522         '_va_'<Free-Index> :: e.new-var,
     605        '_va_' :: e.new-var,
    523606        e.arrays
    524607        ('static private final Expr[] 'e.new-var' = { '<Concat <Intersperse (', ') e.concat>>' };')
    525608        ('Expr.concat ('e.new-var')');
    526     };
     609    };*/
    527610};
    528611
    529612Const-Expr-Aux (e.accum) e.expr, {
    530   e.expr : s.sym e.rest, <Char? s.sym> =
     613/*  e.expr : s.sym e.rest, <Char? s.sym> =
    531614    <Const-Expr-Aux (e.accum <Symbol-To-JBC s.sym>) e.rest>;
    532615  e.accum : v =
     
    562645  } :: (e.arrays) e.java-item =
    563646    <Const-Expr-Aux () e.rest> :: (e.new-arrays) e.java-rest,
    564     (e.arrays e.new-arrays) e.java-item e.java-rest;
     647    (e.arrays e.new-arrays) e.java-item e.java-rest;*/
    565648  = () /*empty*/;
    566649};
     
    605688  e.arrays (e.java-args);
    606689
    607 Name-To-JBC t.obj-name =
    608   <RFP-Extract-Qualifiers t.obj-name> :: (e.qualifiers) e.name,
    609   <? &Current-Namespace> :: e.namespace,
    610   {
    611     e.qualifiers : e.namespace e.cont = <Rfp2Java e.cont e.name>;
    612     <Bind &Inputs (e.qualifiers) ()>,
    613       <Rfp2Java (e.qualifiers e.name)>;
    614   };
    615 
    616690Var-To-JBC t.var =
    617691  <Lookup-Var t.var> :: s.type s.i,
     
    630704  0;
    631705};
    632 
    633 Free-Index =
    634   <? &Free-Idx> : {
    635     /*empty*/ = 1;
    636     s.idx     = <"+" s.idx 1>;
    637   } :: s.idx,
    638   <Store &Free-Idx s.idx>,
    639   s.idx;
    640 
  • to-imperative/trunk/compiler/rfpc.rf

    r2338 r2352  
    220220        AS-AIL =
    221221          e.Items : t.interf (MODULE t.asail-mod-name e.asail-module),
    222           <Extract-Inputs e.asail-module> :: e.module,
    223222          {
    224             <In-Table? &RFP-Options NO-OPTIM> = e.module;
     223            <In-Table? &RFP-Options NO-OPTIM> = e.asail-module;
    225224            <Verbose "as-ail optimization started">,
    226               <Simplify-ASAIL e.module> :: e.module,
     225              <Simplify-ASAIL e.asail-module> :: e.module,
    227226              <Verbose "as-ail optimization finished"> =
    228227              e.module;
     
    238237              { s.need-close? : 1 = <Close-Channel s.channel>;; };;
    239238          },
     239          <Extract-Inputs e.module> :: e.module,
    240240          \{
    241241            <In-Table? &RFP-Options HH>,
     
    367367            <In-Table? &RFP-Options JBC>,
    368368              <Verbose "compilation from as-ail to java-bytecode started">,
     369              <Bind &RFP-Includes (<Concat t.asail-mod-name>) (LOCAL e.dirname)>,
    369370              {
    370371                <? &RFP-Include-Path> : e (e.dir) e,
Note: See TracChangeset for help on using the changeset viewer.