blog 文章

2017/02/08

relational op

產生 < 的組合語言。
add_1.c
 1 int main()
 2 {
 3   int i;
 4   int j;
 5   i=8;
 6   j=3;
 7 
 9 
10   i<j;
11 
94 }
-----------------------------------------
r.S
 1 .text
 2 .globl main
 3 .type main, @function
 4 main:
 5 pushl %ebp
 6 movl %esp, %ebp
 7 subl $4, %esp # reserve temp object size
 8 subl $8, %esp # reserve local variable
 9 mov $8 ,%eax
10 movl %eax, (%ebp)
11 mov $3 ,%eax
12 movl %eax, -4(%ebp)
13 # gen code: i < j
14 movl (%ebp), %eax # variable: i
15 cmpl -4(%ebp), %eax # variable: j
16 setl %al
17 movzbl %al, %eax
18 pushl %eax
19 popl %eax
20 leave
21 ret
繼續閱讀

2017/01/27

產生變數四則運算的 x86 組合語言

這個難很多。
add_1.c
 1 int main()
 2 {
 3   int i;
 4   int j;
 5 
 6   //i=23;
 7   //j=5678;
 8   //printf("i = %d\n", i+j);
 9   //printf("j = %d\n", j);
10   i=2;
11   j=1;
12   //printf("i+j = %d\n", i+j);
13 
14   printf("i = %d\n", i);
15   printf("j = %d\n", j);
16   //printf("i+5 = %d\n", i+5);
17   printf("5+i = %d\n", 5+i);
18 
19   //(1+2)+((3+4)+(5+6));
20   //printf("1+2 = %d\n", 1+2);
21   //printf("(1+2)+(3+4) = %d\n", (1+2)+(3+4));
22   //printf("1+2*3 = %d\n", 1+2*3);
23   //printf("xx 5*(6+7) = %d\n", 5*(6+7));
24   //printf("yy %d\n", 1+2);
25   //(2*3*4) * (5*(7*(8*9)));
26   // (2*3)*(5*6);
27   
28   //3*9;
29   //4*(2*3);
30   //2-1-6;
31 
32   // toplevel if (child[0]->is_leaf() && child[1]->is_leaf() != true)
33   // toplevel if (child[0]->is_leaf() != true && child[1]->is_leaf())
34   //1+2+5;
35   //5+(1+2);
36 
37   //printf("%d\n", (1+2+3)+ (5+6+7)+(9+10));
38 
39   // if (child[0]->is_leaf() != true && child[1]->is_leaf())
40   //(9+10) + ((1+2+3)+ (5+6+7));
41   //(1+2)+9+(3+5)+(7+8);
42 
43   // (child[0]->is_leaf() && child[1]->is_leaf() != true)
44   //(3+(5+6)+9) + (1+2);
45   //(1+2) + (9 + (3 +(5+6)));
46 
47   //(1+2)+(3+4+5);
48   //(1+2)+(3+4)+(5+6) + (7+8);
49 
50   //1+2+3+4+5;
51   //1+2;
52   //1+(2+3);
53   //1+2+3;
54   //(1+2)+(3+4);
55 }
r.S
 1 .section .rodata
 2 .LC1: 
 3     .string "i = %d\n"
 4 .section .rodata
 5 .LC2: 
 6     .string "j = %d\n"
 7 .section .rodata
 8 .LC3: 
 9     .string "5+i = %d\n"
10 .text
11 .globl main
12 .type main, @function
13 main:
14 pushl %ebp
15 movl %esp, %ebp
16 subl $4, %esp # reserve temp object size
17 subl $8, %esp # reserve local variable
18 mov $2 ,%eax
19 movl %eax, (%ebp)
20 mov $1 ,%eax
21 movl %eax, -4(%ebp)
22 pushl (%ebp)
23 pushl $.LC1
24 call printf
25 addl $8, %esp
26 pushl -4(%ebp)
27 pushl $.LC2
28 call printf
29 addl $8, %esp
30 movl $5, %eax
31 ADD (%ebp), %eax
32 pushl %eax
33 popl %eax
34 pushl %eax
35 pushl $.LC3
36 call printf
37 addl $8, %esp
38 leave
39 ret
繼續閱讀

2017/01/12

產生 (9+10) + ((1+2+3)+ (5+6+7)) x86 組合語言

(9+10) + ((1+2+3)+ (5+6+7));

op
 1                  root
 2                   |
 3                  prog
 4            _______|________
 5            |              |
 6 main |int |func |global  main
 7            |
 8        func_body
 9            |
10            +
11        ____|_____
12        |        |
13        +        +
14       _|__   ___|___
15       |  |   |     |
16       9  10  +     +
17             _|__  _|__
18             |  |  |  |
19             +  3  +  7
20            _|__  _|__
21            |  |  |  |
22            1  2  5  6
23 cg: root : ROOT
24 do nothing
25 cg: prog : PROG
26 do nothing
27 cg: main : FUNC_NAME
28   enter func: main
29 cg: func_body : FUNC_BODY
30 cg: + : ADD
31 handle add +
32 movl $9, %eax
33 ADD $10, %eax
34 pushl %eax
35 handle add +
36 handle add +
37 handle add +
38 movl $1, %eax
39 ADD $2, %eax
40 pushl %eax
41 22 movl $3, %ebx
42 popl %eax
43 merge left ADD %ebx, %eax
44 merge left pushl %eax
45 handle add +
46 handle add +
47 movl $5, %eax
48 ADD $6, %eax
49 pushl %eax
50 22 movl $7, %ebx
51 popl %eax
52 merge left ADD %eax, %ebx
53 merge left pushl %ebx
54 popl  %ebx
55 popl  %eax
56 xx left/right ADD %eax, %ebx
57 pushl %ebx
58 popl  %ebx
59 popl  %eax
60 mm left/right ADD %ebx, %eax
61 add complete
62   exit func: main
63 cg: main : FUNC_CALL
64 do nothing
65   exit func: main
繼續閱讀

2016/09/07

simple c interpreter - pointer test

simple c interpreter 支援指標操作, 一樣會 Segmentation fault。
pointer.test
descent@u64:simple_compiler$ cat test_pattern/p6.c
// pointer test
int main()
{
  int x,y;
  int a,b;
  int *p;

  x=987;
  y=29;
  p=&x;

  //p = 100;

  a=*p;

  //printf("a: %d\n", a);

  p=&y;
  a=*p;

  //printf("new a: %d\n", a);
  //printf("a: %d, p: %p\n", a, p); 
  printf("&x: %p\n", &x);
  p=100;
  printf("p: %d\n", p); 
  *p;

}
descent@u64:simple_compiler$ ./c_parser < test_pattern/p6.c 
\tree( root( prog( main |int |func |global( func_body( var(x |int)(y |int))( var(a |int)(b |int))( var(p |ptr<1> |int))( =(x)(987))( =(y)(29))( =(p)( &(x)))( =(a)( *(p)))( =(p)( &(y)))( =(a)( *(p)))( printf(&x: \%p\\n)( &(x)))( =(p)(100))( printf(p: \%d\\n)(p))( *(p))))(main)))
 op: root
&x: 1632ff0
p: 100
Segmentation fault (core dumped)
繼續閱讀

2016/07/28

smple c interpreter (1)

new:
  • global variable support.
  • return function value.
  • variable can use = to receive function return value.
r2
 1 descent@debianlinux:simple_compiler$ cat test_pattern/p2
 2 int a;
 3 
 4 int f2(int i)
 5 {
 6   1+2;
 7   a=3;
 8   return 2+5+i+a;
 9 }
10 
11 int main()
12 {
13   int x,y;
14   int a;
15 
16   a=99;
17   printf("a: %d\n", a);
18   
19   y=2;
20   x = f2(y+1);
21 
22   printf("f2(): %d\n", x);
23 }
24 
25 
26 descent@debianlinux:simple_compiler$ ./c_parser < test_pattern/p2
27 xx return
28 expr return
29 STRING: a: %d
30 
31 STRING: f2(): %d
32 
33 \tree( root( prog( g_var(a |int |global))( f2 |int |func( para(i |int))( func_body( +(1)(2))( =(a)(3))( return( +( +( +(2)(5))(i))(a)))))( main |int |func( func_body( var(x |int)(y |int))( var(a |int))( =(a)(99))( printf(a: \%d\\n)(a))( =(y)(2))( =(x)( f2( +(y)(1))))( printf(f2\(\): \%d\\n)(x))))(main)))
34  op: root
35 can not handle op: f2, ast type: NAME
36 can not handle op: main, ast type: NAME
37 a: 99
38 f2(): 13
繼續閱讀

2016/07/27

simple c interpreter (0)

2016/04/17 ~ 2016/07/27 測試項目
  • if/else 
  • while 
  • function call 
  • 變數 
  • printf 
剩下全域變數還沒實作, 這已經不是不能完成, 而是怎麼完成她會比較美。
function return 也沒做。
c style string 也沒做。
還好多沒做。

simple c interpreter
 1 int x;
 2 int f1()
 3 {
 4   2*3;
 5   12+13;
 6 }
 7 
 8 int f2(int i)
 9 {
10   int a;
11 
12   a=2;
13 
14   while(a <5)
15   {
16     a=a+1;
17     printf("a < 5, a: %d\n", a);
18   }
19   3+i;
20   a*i;
21   printf("a*i: %d\n", a*i);
22 }
23 int main()
24 {
25   int z;
26 
27   z=6;
28   if (z > 1)
29   {
30     printf("z > 1\n");
31   }
32   else
33   {
34     printf("z <= 1\n");
35   }
36   printf("z: %d, 1+2=%d, 5*6 = %d\n", z, 1+2, 5*6);
37   f2(5);
38 }
 
39 ./c_parser  < test_pattern/p3
40 
41 \tree( root( prog( var(x |int |global))( f1 |int |func( func_body( *(2)(3))( +(12)(13))))( f2 |int |func( para(i |int))( func_body( var(a |int))( =(a)(2))( while( <(a)(5))( while_block( =(a)( +(a)(1)))( printf(a < 5, a: %d
42 )(a))))( +(3)(i))( *(a)(i))( printf(a*i: %d
43 )( *(a)(i)))))( main |int |func( func_body( var(z |int))( =(z)(6))( if( >(z)(1))( then_block( printf(z > 1
44 )))( else_block( printf(z <= 1
45 ))))( printf(z: %d, 1+2=%d, 5*6 = %d
46 )(z)( +(1)(2))( *(5)(6)))( f2(5))))(main)))
47  op: root
48 can not handle op: f1, ast type: NAME
49 can not handle op: f2, ast type: NAME
50 can not handle op: main, ast type: NAME
51 z > 1
52 z: 6, 1+2=3, 5*6 = 30
53 a < 5, a: 3
54 a < 5, a: 4
55 a < 5, a: 5
56 a*i: 25
繼續閱讀

2016/07/26

eval function

test_pattern/p1
 1 int x;
 2 int f1()
 3 {
 4   2*3;
 5   12+13;
 6 }
 7 
 8 int f2(int i)
 9 {
10   int a;
11   5*i;
12 }
13 int main()
14 {
15   int z;
16   61+2;
17   9*7;
18   f1();
19   f2(z);
20 }
function ast
繼續閱讀