-
Notifications
You must be signed in to change notification settings - Fork 0
/
zl_compiler.h
219 lines (186 loc) · 8.71 KB
/
zl_compiler.h
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#ifndef _ZL_COMPILER_H_
#define _ZL_COMPILER_H_
//#include "zl_vars.h"
//#include "../../exemples/zinc/zlist.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include "zl_parser.hpp"
#include "zl_vars.h"
//#include "zl_compiler.h"
#include "zl_opcodes.h"
#include "../zinc/utstrings.h"
//extern FILE *yyin;
//extern FILE *yyout;
//int zl_compile(unsigned char **hardcode, char *code, char **error_msg);
//int zl_compile(unsigned char **hardcode, unsigned char **data_table,/* unsigned long *vars_count,*/ zl_funcs_list *funcs, char *code, zl_names_map **vars_map, char **error_msg);
int zl_compile(unsigned char **hardcode, unsigned long *hard_code_size, char *code, char **warning_msg, char **error_msg,
unsigned char **const_sect,
unsigned long *const_size,
unsigned char **data_sect,
unsigned long *data_size,
unsigned char **reloc_sect,
unsigned long *reloc_size,
unsigned char **import_sect,
unsigned long *import_size,
unsigned char **export_sect,
unsigned long *export_size,
unsigned char **map_sect,
unsigned long *map_size
);
typedef struct _cl_stack_buffer
{
void *data;
struct _cl_stack_buffer *next_node;
} cl_stack_buffer;
typedef struct _cl_parser_params
{
// source code
char *source_code;
unsigned long sc_offset;
unsigned long sc_length;
// hard code
unsigned char *hard_code[256];
unsigned long hc_active; // mean count, last, active hard_code dimension
unsigned long hc_fill[256];
unsigned long hc_buffer_size[256];
struct _cl_stack_buffer *cl_stack;
struct _cl_stack_buffer *cl_loop_stack;
char *error_msg;
char *warning_msg;
unsigned long error_code;
unsigned long lineno;
/* revision 1
unsigned char *data_table;
unsigned long data_fill;
unsigned long data_buffer_size;
zl_funcs_list *funcs_list;
zl_names_map *vars_table;
zl_names_map *jumps_table;
zl_names_map *labels_table;
zl_names_map *structs_table;
zl_names_map *funcs_table;
//*/
//unsigned long current_jump; // autoincrement
//?unsigned long current_func;
// revision 2
unsigned long current_level; // autoincrement // rename to current_scope
unsigned long stack_size; // function stack usage
struct _cl_var_node *vars_table;
struct _cl_data_node *data_table;
struct _cl_label_node *labels_table; // inter compiler use only
struct _cl_label_node *funcs_table;
} cl_parser_params;
/*
typedef struct _zl_section_node
{
unsigned long flags; // ïàðàìåòðû ñåêöèè
union
{
char *name; // èìÿ ëîêàëüíîé ïåðåìåííîé, ôóíêöèè èëè ìåòêè
void *data; // äàííûå äëÿ ñåêöèè const
};
union
{
unsigned long offset; // ñìåùåíèå îòíîñèòåëüíî hard_code äëÿ ìåòêè, ôóíêöèè èëè ëîêàëüíîé ïåðåìåííîé
unsigned long size; // ðàçìåð äàííûå äëÿ ñåêöèè const
};
db_list_node *reference_offsets;
} zl_section_node;
//*/
//cl_var_node *cl_var_define(cl_var_node **vars_table, char *name, unsigned long level);
//cl_var_node *cl_var_define(cl_var_node **vars_table, cl_var_node *var_node);
cl_var_node *cl_var_define(cl_var_node **vars_table, char *name, unsigned long level);
cl_var_node *cl_var_find(cl_var_node *vars_table, char *name);
unsigned long cl_var_free_level(cl_var_node **vars_table, unsigned long level);
unsigned long cl_var_reference(cl_var_node *var_node, unsigned long dimension, unsigned long offset);
void cl_var_swap_and_join_dimensions(cl_var_node *vars_table, unsigned long dimension, unsigned long offset);
void cl_var_join_dimension(cl_var_node *vars_table, unsigned long dimension_src, unsigned long dimension_dst, unsigned long offset);
cl_label_node *cl_label_define(cl_label_node **labels_table, char *name);
cl_label_node *cl_label_find(cl_label_node *labels_table, char *name);
unsigned long cl_label_reference(cl_label_node *label_node, unsigned long dimension, unsigned long offset);
void cl_label_swap_and_join_dimensions(cl_label_node *label_node, unsigned long dimension, unsigned long offset);
void cl_label_join_dimension(cl_label_node *label_node, unsigned long dimension_src, unsigned long dimension_dst, unsigned long offset);
unsigned long cl_label_fix(cl_label_node *labels_table, unsigned char *hard_code);
unsigned long cl_label_free(cl_label_node **labels_table);
cl_data_node *cl_const_define(cl_data_node **data_table, unsigned long dimension, char *data, unsigned long size, unsigned long reference);
void cl_const_swap_and_join_dimensions(cl_data_node *data_table, unsigned long dimension, unsigned long offset);
void cl_const_join_dimension(cl_data_node *data_table, unsigned long dimension_src, unsigned long dimension_dst, unsigned long offset);
cl_data_node *cl_const_free(cl_data_node **data_table);
//unsigned long cl_const_build_fix_free(cl_data_node *const_table, unsigned char *hard_code, unsigned char **const_section, unsigned long *size);
unsigned long cl_link_sections(
cl_data_node *const_table,
cl_var_node *vars_table,
cl_label_node *funcs_table,
unsigned char **hard_code,
unsigned long *hard_code_size,
unsigned char **const_section,
unsigned long *const_size,
unsigned char **data_section,
unsigned long *data_size,
unsigned char **reloc_section,
unsigned long *reloc_size,
unsigned char **import_section,
unsigned long *import_size,
unsigned char **export_section,
unsigned long *export_size,
unsigned char **map_section,
unsigned long *map_size
);
// ãëîáàëüíûå ïåðåìåííûå äîëæíû õðàíèòü ðàçìåð è èíèöèàëèçàöèîííûå äàííûå
//db_list_node *cl_section_data_push(db_list_node **section, unsigned long flags, char *data, unsigned long size);
//db_list_node *cl_reference_add(db_list_node **refs, unsigned long offset);
//unsigned long cl_data_add(cl_parser_params *pp, unsigned char *data, unsigned long size);
//Ñòðîêîâûå êîíñòàíòû äîáàâëÿþòñÿ â òàáëèöó äàííûõ è íà âûõîäå ôóíêöèÿ
//âîçâðàùàåò îôñåò çàíåñ¸ííûõ äàííûõ îòíîñèòåëüíî íà÷àëà òàáëèöû.
unsigned long cl_code_add(cl_parser_params *pp, unsigned char *data, unsigned long size);
unsigned long cl_code_add(unsigned char **hard_code, unsigned long *hc_size, unsigned char *data, unsigned long size);
void cl_code_replace(unsigned char *hard_code, unsigned long offset, unsigned long data);
/*
zl_names_map *cl_label_define(zl_names_map **labels_table, char *name, unsigned long offset);
//Ìåòêè ïðè îïðåäåëåíèè çàíîñÿòñÿ âî âðåìåííóþ òàáëèöó (èìÿ ìåòêè è å¸
//îôñåò îòíîñèòåëüíî hardcode)
zl_names_map *cl_jump_define(zl_names_map **labels_table, char *name, unsigned long offset);
//ïî êîäó ôóíêöèÿ ðàâíà cl_label_define()
//Ïåðåõîäû íà ìåòêè çàíîñÿòñÿ â òàáëèöó àíàëîãè÷íî ñàìèì ìåòêàì (èìÿ ìåòêè
//è îôñåò íà ìåñòî â êîäå, ãäå íà íåé ññûëêà).
void cl_names_free(zl_names_map **labels_table);
void cl_names_free(zl_names_map **labels_table);
#define cl_jumps_free cl_names_free
#define cl_labels_free cl_names_free
unsigned long cl_jump_fix(unsigned char *hardcode, zl_names_map *jumps_table, zl_names_map *labels_table);
//Ïîñëå îêîí÷àíèÿ êîìïèëÿöèè âñå ìåñòà ïåðåõîäîâ íà ìåòêè êîððåêòèðóþòñÿ â
//ñîîòâåòñòâèè ñ òàáëèöåé ìåòîê.
zl_names_map *cl_label_new(zl_names_map **jumps_table, unsigned long offset);
//Ãåíåðèðóåò óíèêàëüíîå çíà÷åíèå ìåòêè è ïîìåùàåò åãî â òàáëèöó òèïà
//names_map. È âîçâðàùàåò óêàçàòåëü íà ñòðóêòóðó.
*/
void *cl_stack_push(cl_stack_buffer **cl_stack, void *data);
//Ïîìåùàåò çíà÷åíèå â ñòåê
void *cl_stack_pop(cl_stack_buffer **cl_stack);
//Âîçâðàùàåò çíà÷åíèå èç ñòåêà
void cl_stack_free(cl_stack_buffer **cl_stack);
/*
unsigned long cl_var_define(zl_names_map **vars_table, char *name, unsigned long flags, zl_names_map **out);
//Äîáàâëÿåò íîâóþ ïåðåìåííóþ â êàðòó ïàìÿòè.
//Ïîñëå êîìïèëÿöèè èç ýòîé òàáëèöû ñôîðìèðóåòñÿ êàðòà âíåøíèõ ïåðåìåííûõ
//è äâà çíà÷åíèÿ - êîëè÷åñòâî âíåøíèõ è âíóòðåííèõ ïåðåìåííûõ.
cl_var_node *cl_var_define(cl_var_node **vars_table, cl_var_node *var_node);
zl_names_map *cl_var_find(zl_names_map *vars_table, char *name, unsigned long *val);
zl_funcs_list *cl_func_find(zl_funcs_list *funcs, char *name, unsigned long *val);
unsigned long cl_find_var_or_func(zl_names_map *vars_table, zl_funcs_list *funcs, char *name);
*/
unsigned long cl_push(cl_parser_params *pp, unsigned char code);
unsigned long cl_push_dw(cl_parser_params *pp, unsigned long data);
unsigned long cl_push_op(cl_parser_params *pp, unsigned char code, zlval *arg1, zlval *arg2);
long cl_strtoul(char *text, unsigned long len);
unsigned long cl_hextoul(char *text, unsigned long len);
double cl_strtod(char *text, unsigned long len);
char *cl_strdup(char *text);
char *cl_strcat(char **text, char*text2);
char *cl_strndup(char *text, unsigned long len);
char *cl_sprintf(char *fmt, ...);
char *cl_strtime();
char *cl_strdate();
unsigned long cl_do_op(cl_parser_params *pp, unsigned char op, zlval *ss, zlval *s1, zlval *s2);
#endif // _ZL_COMPILER_H_