| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- /**
- * @file NCDInterpProcess.c
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the author nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include <stdint.h>
- #include <limits.h>
- #include <string.h>
- #include <stdlib.h>
- #include <misc/balloc.h>
- #include <misc/hashfun.h>
- #include <misc/maxalign.h>
- #include <misc/strdup.h>
- #include <base/BLog.h>
- #include <ncd/make_name_indices.h>
- #include "NCDInterpProcess.h"
- #include <generated/blog_channel_ncd.h>
- #include "NCDInterpProcess_trie.h"
- #include <structure/CStringTrie_impl.h>
- static int compute_prealloc (NCDInterpProcess *o)
- {
- int size = 0;
-
- for (int i = 0; i < o->num_stmts; i++) {
- int mod = size % BMAX_ALIGN;
- int align_size = (mod == 0 ? 0 : BMAX_ALIGN - mod);
-
- if (align_size + o->stmts[i].alloc_size > INT_MAX - size) {
- return 0;
- }
-
- o->stmts[i].prealloc_offset = size + align_size;
- size += align_size + o->stmts[i].alloc_size;
- }
-
- ASSERT(size >= 0)
-
- o->prealloc_size = size;
-
- return 1;
- }
- static int convert_value_recurser (NCDPlaceholderDb *pdb, NCDValue *value, NCDValMem *mem, NCDValRef *out)
- {
- ASSERT(pdb)
- ASSERT((NCDValue_Type(value), 1))
- ASSERT(mem)
- ASSERT(out)
-
- switch (NCDValue_Type(value)) {
- case NCDVALUE_STRING: {
- *out = NCDVal_NewStringBin(mem, (const uint8_t *)NCDValue_StringValue(value), NCDValue_StringLength(value));
- if (NCDVal_IsInvalid(*out)) {
- goto fail;
- }
- } break;
-
- case NCDVALUE_LIST: {
- *out = NCDVal_NewList(mem, NCDValue_ListCount(value));
- if (NCDVal_IsInvalid(*out)) {
- goto fail;
- }
-
- for (NCDValue *e = NCDValue_ListFirst(value); e; e = NCDValue_ListNext(value, e)) {
- NCDValRef vval;
- if (!convert_value_recurser(pdb, e, mem, &vval)) {
- goto fail;
- }
-
- NCDVal_ListAppend(*out, vval);
- }
- } break;
-
- case NCDVALUE_MAP: {
- *out = NCDVal_NewMap(mem, NCDValue_MapCount(value));
- if (NCDVal_IsInvalid(*out)) {
- goto fail;
- }
-
- for (NCDValue *ekey = NCDValue_MapFirstKey(value); ekey; ekey = NCDValue_MapNextKey(value, ekey)) {
- NCDValue *eval = NCDValue_MapKeyValue(value, ekey);
-
- NCDValRef vkey;
- NCDValRef vval;
- if (!convert_value_recurser(pdb, ekey, mem, &vkey) ||
- !convert_value_recurser(pdb, eval, mem, &vval)
- ) {
- goto fail;
- }
-
- if (!NCDVal_MapInsert(*out, vkey, vval)) {
- BLog(BLOG_ERROR, "duplicate key in map");
- goto fail;
- }
- }
- } break;
-
- case NCDVALUE_VAR: {
- int plid;
- if (!NCDPlaceholderDb_AddVariable(pdb, NCDValue_VarName(value), &plid)) {
- goto fail;
- }
-
- if (NCDVAL_MINIDX + plid >= -1) {
- goto fail;
- }
-
- *out = NCDVal_NewPlaceholder(mem, plid);
- } break;
-
- default:
- goto fail;
- }
-
- return 1;
-
- fail:
- return 0;
- }
- int NCDInterpProcess_Init (NCDInterpProcess *o, NCDProcess *process, NCDStringIndex *string_index, NCDPlaceholderDb *pdb, NCDModuleIndex *module_index, NCDMethodIndex *method_index)
- {
- ASSERT(process)
- ASSERT(string_index)
- ASSERT(pdb)
- ASSERT(module_index)
- ASSERT(method_index)
-
- NCDBlock *block = NCDProcess_Block(process);
-
- if (NCDBlock_NumStatements(block) > INT_MAX) {
- BLog(BLOG_ERROR, "too many statements");
- goto fail0;
- }
- int num_stmts = NCDBlock_NumStatements(block);
-
- if (!(o->stmts = BAllocArray(num_stmts, sizeof(o->stmts[0])))) {
- BLog(BLOG_ERROR, "BAllocArray failed");
- goto fail0;
- }
-
- if (!NCDInterpProcess__Trie_Init(&o->trie)) {
- BLog(BLOG_ERROR, "NCDInterpProcess__Trie_Init failed");
- goto fail1;
- }
-
- if (!(o->name = b_strdup(NCDProcess_Name(process)))) {
- BLog(BLOG_ERROR, "b_strdup failed");
- goto fail2;
- }
-
- o->num_stmts = 0;
- o->prealloc_size = -1;
- o->is_template = NCDProcess_IsTemplate(process);
-
- for (NCDStatement *s = NCDBlock_FirstStatement(block); s; s = NCDBlock_NextStatement(block, s)) {
- ASSERT(NCDStatement_Type(s) == NCDSTATEMENT_REG)
- struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
-
- e->name = NULL;
- e->cmdname = NULL;
- e->objnames = NULL;
- e->num_objnames = 0;
- e->alloc_size = 0;
-
- if (NCDStatement_Name(s) && !(e->name = b_strdup(NCDStatement_Name(s)))) {
- BLog(BLOG_ERROR, "b_strdup failed");
- goto loop_fail0;
- }
-
- if (!(e->cmdname = b_strdup(NCDStatement_RegCmdName(s)))) {
- BLog(BLOG_ERROR, "b_strdup failed");
- goto loop_fail0;
- }
-
- NCDValMem mem;
- NCDValMem_Init(&mem);
-
- NCDValRef val;
- if (!convert_value_recurser(pdb, NCDStatement_RegArgs(s), &mem, &val)) {
- BLog(BLOG_ERROR, "convert_value_recurser failed");
- NCDValMem_Free(&mem);
- goto loop_fail0;
- }
-
- e->arg_ref = NCDVal_ToSafe(val);
-
- if (!NCDValReplaceProg_Init(&e->arg_prog, val)) {
- BLog(BLOG_ERROR, "NCDValReplaceProg_Init failed");
- NCDValMem_Free(&mem);
- goto loop_fail0;
- }
-
- if (!NCDValMem_FreeExport(&mem, &e->arg_data, &e->arg_len)) {
- BLog(BLOG_ERROR, "NCDValMem_FreeExport failed");
- NCDValMem_Free(&mem);
- goto loop_fail1;
- }
-
- if (NCDStatement_RegObjName(s)) {
- if (!ncd_make_name_indices(string_index, NCDStatement_RegObjName(s), &e->objnames, &e->num_objnames)) {
- BLog(BLOG_ERROR, "ncd_make_name_indices failed");
- goto loop_fail2;
- }
-
- e->binding.method_name_id = NCDMethodIndex_GetMethodNameId(method_index, NCDStatement_RegCmdName(s));
- if (e->binding.method_name_id == -1) {
- BLog(BLOG_ERROR, "NCDMethodIndex_GetMethodNameId failed");
- goto loop_fail3;
- }
- } else {
- e->binding.simple_module = NCDModuleIndex_FindModule(module_index, NCDStatement_RegCmdName(s));
- }
-
- if (e->name) {
- int next_idx = NCDInterpProcess__Trie_Get(&o->trie, e->name);
- ASSERT(next_idx >= -1)
- ASSERT(next_idx < o->num_stmts)
-
- e->trie_next = next_idx;
-
- if (!NCDInterpProcess__Trie_Set(&o->trie, e->name, o->num_stmts)) {
- BLog(BLOG_ERROR, "NCDInterpProcess__Trie_Set failed");
- goto loop_fail3;
- }
- }
-
- o->num_stmts++;
- continue;
-
- loop_fail3:
- BFree(e->objnames);
- loop_fail2:
- BFree(e->arg_data);
- loop_fail1:
- NCDValReplaceProg_Free(&e->arg_prog);
- loop_fail0:
- free(e->cmdname);
- free(e->name);
- goto fail3;
- }
-
- ASSERT(o->num_stmts == num_stmts)
-
- DebugObject_Init(&o->d_obj);
- return 1;
-
- fail3:
- while (o->num_stmts-- > 0) {
- struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
- BFree(e->objnames);
- BFree(e->arg_data);
- NCDValReplaceProg_Free(&e->arg_prog);
- free(e->cmdname);
- free(e->name);
- }
- free(o->name);
- fail2:
- NCDInterpProcess__Trie_Free(&o->trie);
- fail1:
- BFree(o->stmts);
- fail0:
- return 0;
- }
- void NCDInterpProcess_Free (NCDInterpProcess *o)
- {
- DebugObject_Free(&o->d_obj);
-
- while (o->num_stmts-- > 0) {
- struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
- BFree(e->objnames);
- BFree(e->arg_data);
- NCDValReplaceProg_Free(&e->arg_prog);
- free(e->cmdname);
- free(e->name);
- }
-
- free(o->name);
- NCDInterpProcess__Trie_Free(&o->trie);
- BFree(o->stmts);
- }
- int NCDInterpProcess_FindStatement (NCDInterpProcess *o, int from_index, const char *name)
- {
- DebugObject_Access(&o->d_obj);
- ASSERT(from_index >= 0)
- ASSERT(from_index <= o->num_stmts)
- ASSERT(name)
-
- int stmt_idx = NCDInterpProcess__Trie_Get(&o->trie, name);
- ASSERT(stmt_idx >= -1)
- ASSERT(stmt_idx < o->num_stmts)
-
- while (stmt_idx >= 0) {
- struct NCDInterpProcess__stmt *e = &o->stmts[stmt_idx];
- ASSERT(e->name)
-
- if (!strcmp(e->name, name) && stmt_idx < from_index) {
- return stmt_idx;
- }
-
- stmt_idx = e->trie_next;
- ASSERT(stmt_idx >= -1)
- ASSERT(stmt_idx < o->num_stmts)
- }
-
- return -1;
- }
- const char * NCDInterpProcess_StatementCmdName (NCDInterpProcess *o, int i)
- {
- DebugObject_Access(&o->d_obj);
- ASSERT(i >= 0)
- ASSERT(i < o->num_stmts)
- ASSERT(o->stmts[i].cmdname)
-
- return o->stmts[i].cmdname;
- }
- void NCDInterpProcess_StatementObjNames (NCDInterpProcess *o, int i, const NCD_string_id_t **out_objnames, size_t *out_num_objnames)
- {
- DebugObject_Access(&o->d_obj);
- ASSERT(i >= 0)
- ASSERT(i < o->num_stmts)
- ASSERT(out_objnames)
- ASSERT(out_num_objnames)
-
- *out_objnames = o->stmts[i].objnames;
- *out_num_objnames = o->stmts[i].num_objnames;
- }
- const struct NCDModule * NCDInterpProcess_StatementGetSimpleModule (NCDInterpProcess *o, int i)
- {
- DebugObject_Access(&o->d_obj);
- ASSERT(i >= 0)
- ASSERT(i < o->num_stmts)
- ASSERT(!o->stmts[i].objnames)
-
- return o->stmts[i].binding.simple_module;
- }
- const struct NCDModule * NCDInterpProcess_StatementGetMethodModule (NCDInterpProcess *o, int i, const char *obj_type, NCDMethodIndex *method_index)
- {
- DebugObject_Access(&o->d_obj);
- ASSERT(i >= 0)
- ASSERT(i < o->num_stmts)
- ASSERT(o->stmts[i].objnames)
- ASSERT(obj_type)
- ASSERT(method_index)
-
- return NCDMethodIndex_GetMethodModule(method_index, obj_type, o->stmts[i].binding.method_name_id);
- }
- int NCDInterpProcess_CopyStatementArgs (NCDInterpProcess *o, int i, NCDValMem *out_valmem, NCDValRef *out_val, NCDValReplaceProg *out_prog)
- {
- DebugObject_Access(&o->d_obj);
- ASSERT(i >= 0)
- ASSERT(i < o->num_stmts)
- ASSERT(out_valmem)
- ASSERT(out_val)
- ASSERT(out_prog)
-
- struct NCDInterpProcess__stmt *e = &o->stmts[i];
-
- if (!NCDValMem_InitImport(out_valmem, e->arg_data, e->arg_len)) {
- return 0;
- }
-
- *out_val = NCDVal_FromSafe(out_valmem, e->arg_ref);
- *out_prog = e->arg_prog;
- return 1;
- }
- void NCDInterpProcess_StatementBumpAllocSize (NCDInterpProcess *o, int i, int alloc_size)
- {
- DebugObject_Access(&o->d_obj);
- ASSERT(i >= 0)
- ASSERT(i < o->num_stmts)
- ASSERT(alloc_size >= 0)
-
- if (alloc_size > o->stmts[i].alloc_size) {
- o->stmts[i].alloc_size = alloc_size;
- o->prealloc_size = -1;
- }
- }
- int NCDInterpProcess_PreallocSize (NCDInterpProcess *o)
- {
- DebugObject_Access(&o->d_obj);
- ASSERT(o->prealloc_size == -1 || o->prealloc_size >= 0)
-
- if (o->prealloc_size < 0 && !compute_prealloc(o)) {
- return -1;
- }
-
- return o->prealloc_size;
- }
- int NCDInterpProcess_StatementPreallocSize (NCDInterpProcess *o, int i)
- {
- DebugObject_Access(&o->d_obj);
- ASSERT(i >= 0)
- ASSERT(i < o->num_stmts)
- ASSERT(o->prealloc_size >= 0)
-
- return o->stmts[i].alloc_size;
- }
- int NCDInterpProcess_StatementPreallocOffset (NCDInterpProcess *o, int i)
- {
- DebugObject_Access(&o->d_obj);
- ASSERT(i >= 0)
- ASSERT(i < o->num_stmts)
- ASSERT(o->prealloc_size >= 0)
-
- return o->stmts[i].prealloc_offset;
- }
- const char * NCDInterpProcess_Name (NCDInterpProcess *o)
- {
- DebugObject_Access(&o->d_obj);
-
- return o->name;
- }
- int NCDInterpProcess_IsTemplate (NCDInterpProcess *o)
- {
- DebugObject_Access(&o->d_obj);
-
- return o->is_template;
- }
- int NCDInterpProcess_NumStatements (NCDInterpProcess *o)
- {
- DebugObject_Access(&o->d_obj);
-
- return o->num_stmts;
- }
|