*/
#include <string.h>
#include <math.h>
+#include <pthread.h>
+ #include "parser.h"
- #include "gmqcc.h"
- #include "lexer.h"
- #include "ast.h"
-
- /* beginning of locals */
#define PARSER_HT_LOCALS 2
-
- #define PARSER_HT_SIZE 128
- #define TYPEDEF_HT_SIZE 16
-
- typedef struct parser_s {
- lex_file *lex;
- int tok;
-
- bool ast_cleaned;
-
- ast_expression **globals;
- ast_expression **fields;
- ast_function **functions;
- ast_value **imm_float;
- ast_value **imm_string;
- ast_value **imm_vector;
- size_t translated;
-
- ht ht_imm_string;
- ht ht_imm_string_dotranslate;
-
- /* must be deleted first, they reference immediates and values */
- ast_value **accessors;
-
- ast_value *imm_float_zero;
- ast_value *imm_float_one;
- ast_value *imm_float_neg_one;
-
- ast_value *imm_vector_zero;
-
- ast_value *nil;
- ast_value *reserved_version;
-
- size_t crc_globals;
- size_t crc_fields;
-
- ast_function *function;
- ht aliases;
-
- /* All the labels the function defined...
- * Should they be in ast_function instead?
- */
- ast_label **labels;
- ast_goto **gotos;
- const char **breaks;
- const char **continues;
-
- /* A list of hashtables for each scope */
- ht *variables;
- ht htfields;
- ht htglobals;
- ht *typedefs;
-
- /* same as above but for the spelling corrector */
- correct_trie_t **correct_variables;
- size_t ***correct_variables_score; /* vector of vector of size_t* */
-
- /* not to be used directly, we use the hash table */
- ast_expression **_locals;
- size_t *_blocklocals;
- ast_value **_typedefs;
- size_t *_blocktypedefs;
- lex_ctx *_block_ctx;
-
- /* we store the '=' operator info */
- const oper_info *assign_op;
-
- /* magic values */
- ast_value *const_vec[3];
-
- /* pragma flags */
- bool noref;
-
- /* collected information */
- size_t max_param_count;
- } parser_t;
-
- static ast_expression * const intrinsic_debug_typestring = (ast_expression*)0x1;
+ #define PARSER_HT_SIZE 512
+ #define TYPEDEF_HT_SIZE 512
static void parser_enterblock(parser_t *parser);
static bool parser_leaveblock(parser_t *parser);
mem_d(parser);
}
+static void function_finalize_worker_do(ast_function **list, size_t count, volatile uint32_t *counter, bool *failure)
+{
+ do {
+ uint32_t idx = util_atomic_xadd32(counter, 1);
+ if (idx >= count) {
+ *counter = count;
+ return;
+ }
+ if (!ir_function_finalize(list[idx]->ir_func)) {
+ con_out("failed to finalize function %s\n", list[idx]->name);
+ *failure = true;
+ return;
+ }
+ } while (true);
+}
+
+typedef struct {
+ ast_function **list;
+ size_t count;
+ volatile uint32_t *counter;
+ bool *failure;
+} function_finalize_worker_data;
+static void* function_finalize_worker(void *_d) {
+ function_finalize_worker_data *d = (function_finalize_worker_data*)_d;
+ function_finalize_worker_do(d->list, d->count, d->counter, d->failure);
+ return NULL;
+}
+
+static bool function_finalize_all_threaded(ast_function **list, size_t count)
+{
+ volatile uint32_t counter = 0;
+ function_finalize_worker_data wd;
+
+ size_t poolsize = OPTS_OPTION_U32(OPTION_J);
+ bool failure = false;
+ size_t i, j;
+
+ pthread_t *threads;
+
+ if (!list || !count)
+ return true;
+
+ wd.list = list;
+ wd.count = count;
+ wd.counter = &counter;
+ wd.failure = &failure;
+
+ threads = (pthread_t*)mem_a(poolsize*sizeof(pthread_t));
+
+ for (i = 0; i < poolsize; ++i) {
+ if (pthread_create(threads+i, NULL, &function_finalize_worker, &wd) != 0)
+ break;
+ }
+ if (i < poolsize) {
+ con_out("failed to spawn worker threads\n");
+ for (j = 0; j <= i; ++j)
+ pthread_join(threads[j], NULL);
+ return false;
+ }
+ for (i = 0; i < poolsize; ++i)
+ pthread_join(threads[i], NULL);
+ return !failure;
+}
+
++bool function_finalize_all(ast_function **list, size_t count);
+bool function_finalize_all(ast_function **list, size_t count)
+{
+ size_t i;
+ if (OPTS_OPTION_U32(OPTION_J) > 1)
+ return function_finalize_all_threaded(list, count);
+
+ for (i = 0; i < count; ++i) {
+ if (!ir_function_finalize(list[i]->ir_func)) {
+ con_out("failed to finalize function %s\n", list[i]->name);
+ return false;
+ }
+ }
+ return true;
+}
+
bool parser_finish(parser_t *parser, const char *output)
{
size_t i;