28 int compile(FILE *in, FILE *out,
int stack_size,
int array_size)
31 unsigned long label_num = 0L;
33 unsigned long *stack = malloc(stack_size *
sizeof *stack);
35 fprintf(stderr,
"error: couldn't allocate stack (%lu bytes)\n", stack_size *
sizeof *stack);
40 snprintf(buf,
BUF_SIZE,
"\t.lcomm buffer %d\n", array_size);
41 fputs(
"\t.section .bss\n", out);
44 fputs(
"\t.section .text\n", out);
45 fputs(
"\t.globl _start\n", out);
46 fputs(
"_start:\n", out);
47 fputs(
"\tmovl $buffer, %edi\n", out);
49 while ((c = fgetc(in)) != EOF) {
52 fputs(
"\tinc %edi\n", out);
56 fputs(
"\tdec %edi\n", out);
60 fputs(
"\tincb (%edi)\n", out);
64 fputs(
"\tdecb (%edi)\n", out);
68 fputs(
"\tmovl $4, %eax\n", out);
69 fputs(
"\tmovl $1, %ebx\n", out);
70 fputs(
"\tmovl %edi, %ecx\n", out);
71 fputs(
"\tmovl $1, %edx\n", out);
72 fputs(
"\tint $0x80\n", out);
76 fputs(
"\tmovl $3, %eax\n", out);
77 fputs(
"\tmovl $0, %ebx\n", out);
78 fputs(
"\tmovl %edi, %ecx\n", out);
79 fputs(
"\tmovl $1, %edx\n", out);
80 fputs(
"\tint $0x80\n", out);
84 if (stack_ptr == stack_size) {
86 unsigned long *temp = realloc(stack, stack_size *
sizeof *stack);
88 fprintf(stderr,
"error: couldn't reallocate stack (%lu bytes)\n", stack_size *
sizeof *stack);
94 stack[stack_ptr++] = ++label_num;
95 fputs(
"\tcmpb $0, (%edi)\n", out);
96 snprintf(buf,
BUF_SIZE,
"\tjz .LE%lu\n", label_num);
98 snprintf(buf,
BUF_SIZE,
".LB%lu:\n", label_num);
102 fputs(
"\tcmpb $0, (%edi)\n", out);
103 snprintf(buf,
BUF_SIZE,
"\tjnz .LB%lu\n", stack[--stack_ptr]);
105 snprintf(buf,
BUF_SIZE,
".LE%lu:\n", stack[stack_ptr]);
113 fputs(
"\tmovl $1, %eax\n", out);
114 fputs(
"\tmovl $0, %ebx\n", out);
115 fputs(
"\tint $0x80\n", out);
#define BUF_SIZE
The size of the stack buffer.
int compile(FILE *in, FILE *out, int stack_size, int array_size)
compiler function
Header for extern functions.