summaryrefslogtreecommitdiff
path: root/examples/gfcc/ImperJVM.gf
blob: 9acbfa2633417d16e4d5fa9588a68e36553b58b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
--# -path=.:../prelude

concrete ImperJVM of Imper = open Prelude, Precedence, ResImper in {

flags lexer=codevars ; unlexer=code ; startcat=Stm ;
  lincat
    Stm = Instr ;
    Typ = SS ;
    Exp = SS ;
    Var = SS ;

  lin
    Decl  typ cont = instrc (
      "alloc_" ++ typ.s ++ cont.$0
      ) cont ;
    Assign t x exp = instrc (
      exp.s ++ 
      t.s ++ "_store" ++ x.s
      ) ;
    Return t exp   = instr (
      exp.s ++ 
      t.s ++ "_return") ;
    While exp loop = instrc (
      "TEST:" ++ exp.s ++ 
      "ifzero_goto" ++ "END" ++ ";" ++ 
      loop.s ++ 
      "END"
      ) ;
    Block stm      = instrc stm.s ;
    End            = ss [] ** {s3 = []} ;

    EVar  t x  = instr (t.s ++ "_load" ++ x.s) ;
    EInt    n  = instr ("ipush" ++ n.s) ;
    EFloat a b = instr ("fpush" ++ a.s ++ "." ++ b.s) ;
    EAddI      = binop "iadd" ;
    EAddF      = binop "fadd" ;
    EMulI      = binop "imul" ;
    EMulF      = binop "fmul" ;
    ELtI       = binop ("call" ++ "ilt") ;
    ELtF       = binop ("call" ++ "flt") ;

    TInt       = ss "i" ;
    TFloat     = ss "f" ;

}