-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathemit.c
More file actions
98 lines (88 loc) · 2.71 KB
/
Copy pathemit.c
File metadata and controls
98 lines (88 loc) · 2.71 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h> //for fopen, printf
#include <stdlib.h> //for malloc, exit
#include "types.h"
#include "emit.h"
//C code emiting variables and functions
char header_buffer[10000]; // allocate a large enough buffer
int cur_header_len = 0; // keeps track of the current length
char code_buffer[50000]; // allocate a large enough buffer
int cur_code_len = 0; // keeps track of the current length
void c_emit_header(char* text)
{
//go on appending to header_buffer
cur_header_len += snprintf(header_buffer + cur_header_len,
sizeof(header_buffer) - cur_header_len, text);
}
void c_emit_header_line(char* text)
{
//go on appending to header_buffer
cur_header_len += snprintf(header_buffer + cur_header_len,
sizeof(header_buffer) - cur_header_len, text);
cur_header_len += snprintf(header_buffer + cur_header_len,
sizeof(header_buffer) - cur_header_len, "\n");
}
void c_emit_code(char* text)
{
//go on appending to code_buffer
cur_code_len += snprintf(code_buffer + cur_code_len,
sizeof(code_buffer) - cur_code_len, text);
}
void c_emit_code_line(char* text)
{
//go on appending to code_buffer
cur_code_len += snprintf(code_buffer + cur_code_len,
sizeof(code_buffer) - cur_code_len, text);
cur_code_len += snprintf(code_buffer + cur_code_len,
sizeof(code_buffer) - cur_code_len, "\n");
}
void c_emit_outfile(char* outfile)
{
FILE* fp;
fp = fopen(outfile, "w");
if (fp == NULL)
{
printf("Unable to open output file.\n");
exit(1);
}
else
{
//write c to file
header_buffer[cur_header_len]='\0';
code_buffer[cur_code_len]='\0';
fprintf(fp, "%s", header_buffer);
fprintf(fp, "%s", code_buffer);
fclose(fp);
}
}
//IR code emiting variables and functions
char ir_buffer[50000]; // allocate a large enough buffer
int cur_ir_len = 0; // keeps track of the current length
void ir_emit_code_line(char* text)
{
//go on appending to ir_buffer
cur_ir_len += snprintf(ir_buffer + cur_ir_len,
sizeof(ir_buffer) - cur_ir_len, text);
cur_ir_len += snprintf(ir_buffer + cur_ir_len,
sizeof(ir_buffer) - cur_ir_len, "\n");
if(DEBUG)
{
printf("%s\n", text);
}
}
void ir_emit_outfile(char* outfile)
{
FILE* fp;
fp = fopen(outfile, "w");
if (fp == NULL)
{
printf("Unable to open output file.\n");
exit(1);
}
else
{
//write ir to file
ir_buffer[cur_ir_len]='\0';
fprintf(fp, "%s", ir_buffer);
fclose(fp);
}
}