aboutsummaryrefslogtreecommitdiff
path: root/pu2
diff options
context:
space:
mode:
authorGravatar Moon Flower <moon@coffin.ir> 2025-08-23 22:29:30 +0330
committerGravatar Moon Flower <moon@coffin.ir> 2025-08-23 22:29:30 +0330
commitcffbb15757defee44b14286dd5af40f95e341471 (patch)
tree5138318bd44d15acb480a6727802f282ad25b923 /pu2
parentnew flag (diff)
formatting + changed static to malloc
Diffstat (limited to 'pu2')
-rw-r--r--pu2/pu2.c430
-rw-r--r--pu2/pu2.out.c14
2 files changed, 247 insertions, 197 deletions
diff --git a/pu2/pu2.c b/pu2/pu2.c
index 7fdad50..0e3076c 100644
--- a/pu2/pu2.c
+++ b/pu2/pu2.c
@@ -14,7 +14,7 @@ typedef char *string;
string read_file(string path, int *out_size) {
FILE *f = fopen(path, "rb");
string buf;
- int size;
+ int size;
if (!f) return NULL;
fseek(f, 0, SEEK_END);
size = ftell(f);
@@ -182,7 +182,7 @@ const bool false = 0;
// use this instead of strdup'ing and allocating extra storage!
typedef struct {
- int len;
+ int len;
string ref;
} slice_t;
@@ -193,18 +193,18 @@ typedef struct {
typedef struct {
slice_t name;
- int location;
+ int location;
} label_t;
typedef struct {
entry_t data;
- byte arg_count;
+ byte arg_count;
} macro_t;
typedef struct {
slice_t name;
- int* replace_location; // where to put the resolved address
- string init_I;
+ int *replace_location; // where to put the resolved address
+ string init_I;
} label_delegated_t;
clean_slice_ref(slice_t *val) { free(val->ref); }
@@ -234,57 +234,58 @@ clean_slice_ref(slice_t *val) { free(val->ref); }
VAR; \
})
#define memslice_t slice_t __attribute__((cleanup(clean_slice_ref)))
-#define readd() ((used == new.len) ? (new.ref = realloc(new.ref, new.len *= 2)) : 0)
+#define readd() ((used == new.len) ? (new.ref = realloc(new.ref, new.len *= 2)) : 0)
#define scanvalue() \
({ /* value scanner! numbers, characters, strings, variable derefs. */ \
- int value; \
- if (isdigit(*I)) { \
- value = strtol(I, &I, 10); \
- } else if (*I == '\'') { \
- I++; \
- value = *I++; \
- checkskip('\'', "couldn't find closing quote on character"); \
- } else if (*I == '"') { \
- string str_start = ++I; \
- bool T = false; \
- int str_len = 0; \
- while (*I != '"' || T) { \
- if (*I == '\\') { \
- T = !T; \
- if (!T && I[ 1 ] == '"') str_len--; \
- } \
- str_len++; \
- I++; \
- } \
- I++; \
- str_len++; \
- int *dest_start = Data -= str_len; \
- int y; \
- for (y = 0; y < str_len - 1; y++) { dest_start[ y ] = str_start[ y ]; } \
- Data[ -1 ] = 0; \
- value = Data - m; \
- } else if (*I == '&') { \
- I++; \
- string starting_I = I; \
- slice_t name = scanword(); \
- value = __ir2_get_var_loc(name, variables, starting_I, init_I); \
- } else { \
- run_err(I, "expected compile-time value (number, character, string, dereference)"); \
- } \
- value; \
+ int value; \
+ if (isdigit(*I)) { \
+ value = strtol(I, &I, 10); \
+ } else if (*I == '\'') { \
+ I++; \
+ value = *I++; \
+ checkskip('\'', "couldn't find closing quote on character"); \
+ } else if (*I == '"') { \
+ string str_start = ++I; \
+ bool T = false; \
+ int str_len = 0; \
+ while (*I != '"' || T) { \
+ if (*I == '\\') { \
+ T = !T; \
+ if (!T && I[ 1 ] == '"') str_len--; \
+ } \
+ str_len++; \
+ I++; \
+ } \
+ I++; \
+ str_len++; \
+ int *dest_start = Data -= str_len; \
+ int y; \
+ for (y = 0; y < str_len - 1; y++) { dest_start[ y ] = str_start[ y ]; } \
+ Data[ -1 ] = 0; \
+ value = Data - m; \
+ } else if (*I == '&') { \
+ I++; \
+ string starting_I = I; \
+ slice_t name = scanword(); \
+ value = __ir2_get_var_loc(name, variables, starting_I, init_I); \
+ } else { \
+ run_err(I, "expected compile-time value (number, character, string, dereference)"); \
+ } \
+ value; \
})
#define scanvar() \
({ /* memory scanner! finds @10, var_name */ \
- int value; \
- if (*I == '@') { \
- if (!isdigit(*++I)) run_err(I - 1, "expected memory index"); \
- value = strtol(I, &I, 10); \
- } else if (isalpha(*I) || *I == '_') { \
- string starting_I = I; \
- slice_t name = scanword(); \
- value = __ir2_get_var_loc(name, variables, starting_I, init_I); \
- } else run_err(I, "expected memory index or variable"); \
- value; \
+ int value; \
+ if (*I == '@') { \
+ if (!isdigit(*++I)) run_err(I - 1, "expected memory index"); \
+ value = strtol(I, &I, 10); \
+ } else if (isalpha(*I) || *I == '_') { \
+ string starting_I = I; \
+ slice_t name = scanword(); \
+ value = __ir2_get_var_loc(name, variables, starting_I, init_I); \
+ } else \
+ run_err(I, "expected memory index or variable"); \
+ value; \
})
#define varqual(F) ((F) == '@' || isalpha(F) || (F) == '_')
#define litqual(F) (isdigit(F) || (F) == '\'' || (F) == '"' || (F) == '&')
@@ -303,7 +304,7 @@ clean_slice_ref(slice_t *val) { free(val->ref); }
mem_in_a(mem2_index); \
swap(); \
mem_in_a(mem_index); \
- op(operation); \
+ op(operation); \
swap(); \
imm(mem_index); \
store(); \
@@ -328,8 +329,8 @@ int ir2_depth = 0;
#define space skip_space(&I)
-bool has_jump_warned = false;
-bool has_useless_if_warned = false;
+bool has_jump_warned = false;
+bool has_useless_if_warned = false;
bool has_false_while_warned = false;
__ir2_get_var_loc(slice_t name, label_t variables[ 1 << 11 ], string error_I, string init_I) {
@@ -338,16 +339,19 @@ __ir2_get_var_loc(slice_t name, label_t variables[ 1 << 11 ], string error_I, st
label_t cur = variables[ j ];
if (cur.name.ref == NULL) break;
if (cur.name.len != name.len) continue;
- if (strncmp(cur.name.ref, name.ref, name.len) == 0) {
- return cur.location;
- }
+ if (strncmp(cur.name.ref, name.ref, name.len) == 0) { return cur.location; }
}
run_err(error_I, "unknown variable name \"%.*s\"", name.len, name.ref);
}
-string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label_delegated_t forward_refs[ 1 << 10 ], label_t variables[ 1 << 11 ]) {
- char T;
+string _ir2(
+ string I,
+ macro_t macros[ 1 << 9 ],
+ label_t labels[ 1 << 10 ],
+ label_delegated_t forward_refs[ 1 << 10 ],
+ label_t variables[ 1 << 11 ]) {
+ char T;
string init_I = I;
ERR = "all good";
@@ -372,11 +376,11 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
continue;
}
- if (T == '/' && I[1] == '*') { // multi-line comment
+ if (T == '/' && I[ 1 ] == '*') { // multi-line comment
string old_I = I;
- I+= 2;
- while (*I != '*' || I[1] != '/') {
- if (I[1] == '\0') {
+ I += 2;
+ while (*I != '*' || I[ 1 ] != '/') {
+ if (I[ 1 ] == '\0') {
run_err(old_I, "reached EOF whilst looking for closing comment delimeter");
}
@@ -387,34 +391,36 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
}
// define variable
- if (strncmp("let", I, 3) == 0 && isspace(I[3])) {
- I += 3; space;
- string backname_I = I;
- slice_t name = scanword(); space;
- int *m_index = --Data;
- int start_index = m_index - m;
+ if (strncmp("let", I, 3) == 0 && isspace(I[ 3 ])) {
+ I += 3;
+ space;
+ string backname_I = I;
+ slice_t name = scanword();
+ space;
+ int *m_index = --Data;
+ int start_index = m_index - m;
- // comment out this flag to initialize values at runtime instead of compile-time
- #define IR2_INIT_COMPILE_TIME
+// comment out this flag to initialize values at runtime instead of compile-time
+#define IR2_INIT_COMPILE_TIME
if (*I == ';') {
// no starter value, just initialize it to zero
I++;
- #ifdef IR2_INIT_COMPILE_TIME
- *m_index = 0;
- #else
- imm_in_mem(start_index, 0);
- #endif
+#ifdef IR2_INIT_COMPILE_TIME
+ *m_index = 0;
+#else
+ imm_in_mem(start_index, 0);
+#endif
} else {
checkskip('=', "expected semicolon or assignment in variable declaration");
space;
if (litqual(*I)) { // literal
- #ifdef IR2_INIT_COMPILE_TIME
- *m_index = scanvalue();
- #else
- imm_in_mem(start_index, scanvalue());
- #endif
+#ifdef IR2_INIT_COMPILE_TIME
+ *m_index = scanvalue();
+#else
+ imm_in_mem(start_index, scanvalue());
+#endif
} else if (varqual(*I)) { // memory address
I++;
int mem_index = scanvar();
@@ -432,7 +438,7 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
label_t cur = variables[ j ];
if (cur.name.ref == NULL) {
- variables[ j ].name = name;
+ variables[ j ].name = name;
variables[ j ].location = start_index;
goto VAR_DONE;
}
@@ -444,7 +450,7 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
run_err(backname_I, "variables count exceeded 1 << 11 (literally how)");
- VAR_DONE:
+ VAR_DONE:
continue;
}
@@ -457,8 +463,8 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
if (!isalpha(*I) && *I != '_') {
run_err(I - 1, "expected letter or underscore at macro name");
}
- string old_I = I;
- slice_t name = scanword();
+ string old_I = I;
+ slice_t name = scanword();
space;
@@ -488,7 +494,8 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
for (x = 0; x < (1 << 9); x++) {
macro_t g = macros[ x ];
if (g.data.key.ref == NULL) goto DONE;
- if (g.data.key.len == name.len && strncmp(g.data.key.ref, name.ref, name.len) == 0) {
+ if (g.data.key.len == name.len
+ && strncmp(g.data.key.ref, name.ref, name.len) == 0) {
run_err(old_I, "redefinition of macro");
}
}
@@ -498,7 +505,7 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
DONE:;
space; // might aswell skip whitespace for funsies.
- macro_t *g = &macros[ x ];
+ macro_t *g = &macros[ x ];
g->data.key = name;
g->data.value.ref = I;
g->data.value.len = 0;
@@ -561,18 +568,18 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
continue;
}
- if (strncmp("jump", I, 4) == 0 && !isalnum(I[4])) {
+ if (strncmp("jump", I, 4) == 0 && !isalnum(I[ 4 ])) {
I += 4;
goto PARSE_JUMP_BLOB;
}
- if (strncmp("jmp", I, 3) == 0 && !isalnum(I[3])) {
+ if (strncmp("jmp", I, 3) == 0 && !isalnum(I[ 3 ])) {
I += 3;
- PARSE_JUMP_BLOB:
+ PARSE_JUMP_BLOB:
space;
checkskip('(', "expected open paren on jump blob");
space;
- if(!isalpha(*I)) run_err(I, "expected a label name for jump blob argument");
+ if (!isalpha(*I)) run_err(I, "expected a label name for jump blob argument");
slice_t label = scanword();
space;
checkskip(')', "expected closing paren after label name in jump blob");
@@ -594,32 +601,36 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
// find delegations slot
label_delegated_t *del;
- int j;
+ int j;
for (j = 0; j < 1 << 10; j++) {
- if (forward_refs[j].name.ref == NULL) {
+ if (forward_refs[ j ].name.ref == NULL) {
// found one!
- del = &forward_refs[j];
+ del = &forward_refs[ j ];
goto WRITE_DEL;
}
}
- run_err(label.ref, "couldn't reserve a spot for this label as the forward references table is full (how?)");
+ run_err(
+ label.ref,
+ "couldn't reserve a spot for this label as the forward references table is full "
+ "(how?)");
- WRITE_DEL:
+ WRITE_DEL:
- imm_in_mem(7, 0xDEAD10C4); // DEADLOCK - placeholder for the resolver
+ imm_in_mem(7, 0xDEAD10C4); // DEADLOCK - placeholder for the resolver
del->replace_location = Code - 1; // place of imm in code
- del->name = label;
- del->init_I = init_I;
+ del->name = label;
+ del->init_I = init_I;
- FINISH_JMP:
+ FINISH_JMP:
continue;
}
- if ((strncmp("jnz", I, 3) == 0 && !isalnum(I[3])) || (strncmp("jz", I, 2) == 0 && !isalnum(I[2]))) {
- bool was_jnz = !!strncmp("jz", I, 2);
- string name = was_jnz ? "jnz" : "jz";
-
+ if ((strncmp("jnz", I, 3) == 0 && !isalnum(I[ 3 ]))
+ || (strncmp("jz", I, 2) == 0 && !isalnum(I[ 2 ]))) {
+ bool was_jnz = !!strncmp("jz", I, 2);
+ string name = was_jnz ? "jnz" : "jz";
+
I += was_jnz ? 3 : 2;
space;
checkskip('(', "expected open paren on %s blob", name);
@@ -627,14 +638,28 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
int target_mem = scanvar();
space;
- checkskip(',', "expected comma (second argument) in %s blob (example: `%s(@5, label_name);`)", name, name);
-
+ checkskip(
+ ',',
+ "expected comma (second argument) in %s blob (example: `%s(@5, label_name);`)",
+ name,
+ name);
+
space;
- if(!isalpha(*I)) run_err(I, "expected a label name for %s blob's second argument (example: `%s(@5, label_name);`)", name, name);
+ if (!isalpha(*I))
+ run_err(
+ I,
+ "expected a label name for %s blob's second argument (example: `%s(@5, "
+ "label_name);`)",
+ name,
+ name);
slice_t label = scanword();
space;
- checkskip(')', "expected closing paren after label name in %s blob (do i really need to explain this to you again?)", name);
+ checkskip(
+ ')',
+ "expected closing paren after label name in %s blob (do i really need to explain "
+ "this to you again?)",
+ name);
space;
checkskip(';', "excpected semicolon after %s blob", name);
@@ -652,7 +677,7 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
swap();
imm(7);
op(str);
-
+
goto FINISH_JNZ;
}
}
@@ -661,40 +686,45 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
// find delegations slot
label_delegated_t *del;
- int j;
+ int j;
for (j = 0; j < 1 << 10; j++) {
- if (forward_refs[j].name.ref == NULL) {
+ if (forward_refs[ j ].name.ref == NULL) {
// found one!
- del = &forward_refs[j];
+ del = &forward_refs[ j ];
goto WRITE_DEL_JNZ;
}
}
- run_err(label.ref, "couldn't reserve a spot for this label as the forward references table is full (how?)");
+ run_err(
+ label.ref,
+ "couldn't reserve a spot for this label as the forward references table is full "
+ "(how?)");
- WRITE_DEL_JNZ:
+ WRITE_DEL_JNZ:
// emit j(n)z
mem_in_a(target_mem);
if (!was_jnz) op(not);
op(a2u);
- imm(0xDEAD10C4); // DEADLOCK - placeholder for the resolver
+ imm(0xDEAD10C4); // DEADLOCK - placeholder for the resolver
del->replace_location = Code - 1; // place of imm in code
swap();
imm(7);
op(str);
-
- del->name = label;
+
+ del->name = label;
del->init_I = init_I;
- FINISH_JNZ:
+ FINISH_JNZ:
continue;
}
- if (strncmp("if", I, 2) == 0 && !isalnum(I[2])) {
+ if (strncmp("if", I, 2) == 0 && !isalnum(I[ 2 ])) {
// if statement
I += 2;
- space; checkskip('(', "expected open paren for if statement"); space;
+ space;
+ checkskip('(', "expected open paren for if statement");
+ space;
ir2_depth++;
@@ -705,9 +735,12 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
}
// redundant if statement: always true / always false
- int value = strtol(I, &I, 10); space;
- checkskip(')', "expected closing paren after if statement argument"); space;
- checkskip('{', "expected opening bracket for if statement body"); space;
+ int value = strtol(I, &I, 10);
+ space;
+ checkskip(')', "expected closing paren after if statement argument");
+ space;
+ checkskip('{', "expected opening bracket for if statement body");
+ space;
if (!value) {
// just skip over everything inside
@@ -716,20 +749,20 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
int *Data_B = Data;
// completely eliminate code generated in useless if statement
- int m2[S/4];
+ int m2[ S / 4 ];
Data = Code = m2;
label_t variables_G[ 1 << 5 ];
// get new I location
string new_I = _ir2(I, macros, labels, forward_refs, variables_G);
- I = new_I;
+ I = new_I;
// restore values
Code = Code_B;
Data = Data_B;
} else {
// just... i mean, i guess run the code..?
string new_I = _ir2(I, macros, labels, forward_refs, variables);
- I = new_I;
+ I = new_I;
}
continue;
@@ -738,46 +771,55 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
bool was_negated = false;
if (*I == '!') {
was_negated = true;
- I++; space;
+ I++;
+ space;
}
- int mem_index = scanvar(); space;
- checkskip(')', "expected closing paren after if statement argument"); space;
- checkskip('{', "expected opening bracket for if statement body"); space;
-
+ int mem_index = scanvar();
+ space;
+ checkskip(')', "expected closing paren after if statement argument");
+ space;
+ checkskip('{', "expected opening bracket for if statement body");
+ space;
+
mem_in_a(mem_index);
if (!was_negated) op(not);
op(a2u);
imm(0xDEAD10C4); // DEADLOCK - placeholder for the patchback
- int* patchback = Code - 1;
+ int *patchback = Code - 1;
swap();
imm(7);
op(str);
// handle the code stuff
string new_I = _ir2(I, macros, labels, forward_refs, variables);
- I = new_I;
+ I = new_I;
*patchback = Code - m; // patch the jump instruction
continue;
}
- if (strncmp("while", I, 5) == 0 && !isalnum(I[5])) {
+ if (strncmp("while", I, 5) == 0 && !isalnum(I[ 5 ])) {
I += 5;
- space; checkskip('(', "expected open paren for while loop"); space;
+ space;
+ checkskip('(', "expected open paren for while loop");
+ space;
ir2_depth++;
if (isdigit(*I)) {
- int value = strtol(I, &I, 10); space;
+ int value = strtol(I, &I, 10);
+ space;
- checkskip(')', "expected closing paren after while loop argument"); space;
- checkskip('{', "expected opening bracket for while loop body"); space;
+ checkskip(')', "expected closing paren after while loop argument");
+ space;
+ checkskip('{', "expected opening bracket for while loop body");
+ space;
if (!value) {
// non-running while loop
- if(!has_false_while_warned) {
+ if (!has_false_while_warned) {
has_false_while_warned = true;
printf("warn: dead code inside while loop\n");
}
@@ -788,13 +830,13 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
int *Data_B = Data;
// completely eliminate code generated in useless while loop
- int m2[S/4];
+ int m2[ S / 4 ];
Data = Code = m2;
label_t variables_G[ 1 << 5 ];
// get new I location
string new_I = _ir2(I, macros, labels, forward_refs, variables_G);
- I = new_I;
+ I = new_I;
// restore values
Code = Code_B;
Data = Data_B;
@@ -802,9 +844,9 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
// run forever!
int start = Code - m;
-
+
string new_I = _ir2(I, macros, labels, forward_refs, variables);
- I = new_I;
+ I = new_I;
imm_in_mem(7, start); // always jump back to the start
}
@@ -815,18 +857,22 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
bool was_negated = false;
if (*I == '!') {
was_negated = true;
- I++; space;
+ I++;
+ space;
}
- int mem_index = scanvar(); space;
- checkskip(')', "expected closing paren after while loop condition"); space;
- checkskip('{', "expected opening bracket for while loop body"); space;
-
+ int mem_index = scanvar();
+ space;
+ checkskip(')', "expected closing paren after while loop condition");
+ space;
+ checkskip('{', "expected opening bracket for while loop body");
+ space;
+
mem_in_a(mem_index);
if (!was_negated) op(not);
op(a2u);
imm(0xDEAD10C4); // DEADLOCK - placeholder for the patchback
- int* patchback = Code - 1;
+ int *patchback = Code - 1;
swap();
imm(7);
op(str);
@@ -835,7 +881,7 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
// handle the code stuff
string new_I = _ir2(I, macros, labels, forward_refs, variables);
- I = new_I;
+ I = new_I;
*patchback = Code - m; // patch the jump instruction
@@ -853,31 +899,32 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
// macro call, maybe?
if (isalpha(*I)) {
- string old_I = I;
- slice_t name = scanword();
+ string old_I = I;
+ slice_t name = scanword();
space;
- if(*I == ':') {
+ if (*I == ':') {
I++;
// this is a label!
label_t *slot;
- int j;
+ int j;
for (j = 0; j < 1 << 10; j++) {
- if (labels[j].name.ref != NULL) {
- if (labels[j].name.len == name.len && strncmp(labels[j].name.ref, name.ref, name.len) == 0) {
+ if (labels[ j ].name.ref != NULL) {
+ if (labels[ j ].name.len == name.len
+ && strncmp(labels[ j ].name.ref, name.ref, name.len) == 0) {
run_err(old_I, "redefinition of label");
}
continue;
}
- slot = &labels[j];
+ slot = &labels[ j ];
goto FOUND_SLOT;
}
- run_err(old_I, "registered label count exceeded 1<<10 (how?)");
+ run_err(old_I, "registered label count exceeded 1<<10 (how?)");
- FOUND_SLOT:
- slot->name = name;
+ FOUND_SLOT:
+ slot->name = name;
slot->location = Code - m;
continue;
@@ -890,8 +937,8 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
checkskip('(', "expected open paren after alphanumeric word (assuming this is a blob)");
space;
- slice_t arguments[ 1 << 7 ];
- int arg_count = 0;
+ slice_t arguments[ 1 << 7 ];
+ int arg_count = 0;
string fail_I = I;
@@ -956,12 +1003,12 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
macros[ i ].arg_count,
arg_count);
}
- slice_t target = macros[ i ].data.value;
- string N = target.ref;
- string oldN = N;
- memslice_t new = { target.len, malloc(target.len) };
- int used = 0;
- bool needs_scan = true;
+ slice_t target = macros[ i ].data.value;
+ string N = target.ref;
+ string oldN = N;
+ memslice_t new = { target.len, malloc(target.len) };
+ int used = 0;
+ bool needs_scan = true;
while (needs_scan) {
needs_scan = false;
@@ -972,7 +1019,7 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
if (*N == '/' && isdigit(N[ 1 ])) {
N++;
string old = N - 1;
- int index = strtol(N, &N, 10);
+ int index = strtol(N, &N, 10);
if (index > arg_count) {
N--;
@@ -988,9 +1035,9 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
goto END;
}
- needs_scan = true;
- slice_t rep = arguments[ index - 1 ];
- string val = rep.ref;
+ needs_scan = true;
+ slice_t rep = arguments[ index - 1 ];
+ string val = rep.ref;
for (i = 0; i < rep.len; i++) {
new.ref[ used++ ] = *val++;
readd();
@@ -1011,16 +1058,16 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
continue;
}
-
- REST_AFTER_MACRO_CALL:
+
+ REST_AFTER_MACRO_CALL:
// handle assignments!
if (varqual(T)) {
string starting_qual = I;
- int mem_index = scanvar();
+ int mem_index = scanvar();
space;
- if(*I == '-' && I[1] == '-') { // decrement
+ if (*I == '-' && I[ 1 ] == '-') { // decrement
I += 2;
space;
checkskip(';', "expected semicolon");
@@ -1035,7 +1082,7 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
continue;
}
- if(*I == '+' && I[1] == '+') { // increment
+ if (*I == '+' && I[ 1 ] == '+') { // increment
I += 2;
space;
checkskip(';', "expected semicolon");
@@ -1129,9 +1176,9 @@ string _ir2(string I, macro_t macros[ 1 << 9 ], label_t labels[ 1 << 10 ], label
}
ir2(string I) {
- macro_t macros[ 1 << 9 ] = { 0 };
- label_t labels[ 1 << 10 ] = { 0 };
- label_t variables[ 1 << 11 ] = { 0 };
+ macro_t macros[ 1 << 9 ] = { 0 };
+ label_t labels[ 1 << 10 ] = { 0 };
+ label_t variables[ 1 << 11 ] = { 0 };
label_delegated_t forward_refs[ 1 << 10 ] = { 0 };
_ir2(I, macros, labels, forward_refs, variables);
@@ -1143,8 +1190,9 @@ ir2(string I) {
// find label
int j;
for (j = 0; j < static_len(labels); j++) {
- label_t clab = labels[j];
- if (clab.name.len == cur.name.len && strncmp(clab.name.ref, cur.name.ref, cur.name.len) == 0) {
+ label_t clab = labels[ j ];
+ if (clab.name.len == cur.name.len
+ && strncmp(clab.name.ref, cur.name.ref, cur.name.len) == 0) {
// found it!
*cur.replace_location = clab.location;
goto REP_DONE;
@@ -1154,7 +1202,7 @@ ir2(string I) {
string init_I = cur.init_I;
run_err(cur.name.ref, "could not find label %i %s", cur.name.len, cur.name.ref);
- REP_DONE:;
+ REP_DONE:;
}
}
@@ -1200,9 +1248,9 @@ double bench_cycle(int instr) {
c = 0;
clock_t start = ntime();
- int wstart = rdtsc();
+ int wstart = rdtsc();
pu2();
- int wend = rdtsc();
+ int wend = rdtsc();
clock_t end = ntime();
double nanoseconds = (end - start);
@@ -1354,16 +1402,16 @@ main(int argc, char **argv) {
}
char *filename = argv[ 1 ];
- int file_size;
+ int file_size;
string contents = read_file(filename, &file_size);
if (contents == NULL) {
perror("unable to open file");
exit(1);
}
- int o_size;
- long before = ntime();
- long cycles_bef = rdtsc();
+ int o_size;
+ long before = ntime();
+ long cycles_bef = rdtsc();
ir2(contents);
long cycles_aft = rdtsc();
long after = ntime();
@@ -1375,7 +1423,7 @@ main(int argc, char **argv) {
j++;
}*/
- //printf("assembly took %i milliseconds\n", (after - before)/1000);
+ // printf("assembly took %i milliseconds\n", (after - before)/1000);
free(contents);
diff --git a/pu2/pu2.out.c b/pu2/pu2.out.c
index baae8f2..eb6badb 100644
--- a/pu2/pu2.out.c
+++ b/pu2/pu2.out.c
@@ -10,7 +10,7 @@
// clang-format on
/* -- BEGIN COMPILE TIME FLAGS -- */
-// #define PU2_NO_MMAP
+#define PU2_NO_MMAP
// #define PU2_NO_ASM
// #define PU2_NO_INTRINSICS
// #define PU2_BAD_CLOCK
@@ -97,12 +97,8 @@
#define likely(JJ) (JJ)
#endif
- #ifndef PU2_NO_MMAP
__attribute__((aligned(64))) int *m;
- #else
-__attribute__((aligned(64))) int m[ S ];
- #endif
-int a, b, u, c;
+int a, b, u, c;
typedef struct {
int c;
int v;
@@ -145,6 +141,12 @@ void init_pu2() {
page_count);
exit(1);
}
+ #else
+ m = malloc(S * sizeof(int));
+ if(m == NULL) {
+ perror("[error] failed to allocate pu2 memory");
+ exit(1);
+ }
#endif
// size_t i;