2 * Copyright (c) 2011 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
8 * ncdecode_st.c - Implements nested symbol tables.
10 * Note: We use linear lists
13 #ifndef NACL_TRUSTED_BUT_NOT_TCB
14 #error("This file is not meant for use in the TCB")
21 #include "native_client/src/trusted/validator/x86/decoder/generator/ncdecode_st.h"
23 #include "native_client/src/include/nacl_macros.h"
24 #include "native_client/src/include/portability.h"
25 #include "native_client/src/shared/platform/nacl_log.h"
26 #include "native_client/src/trusted/validator/x86/decoder/generator/ncdecode_tablegen.h"
28 /* To turn on debugging of instruction decoding, change value of
33 #include "native_client/src/shared/utils/debugging.h"
35 const char* NaClStValueKindName(NaClStValueKind kind) {
37 * See https://code.google.com/p/nativeclient/issues/detail?id=3750
49 return "???"; /* better not be DCE'd */
52 void NaClStValueAssign(
55 memcpy(lhs, rhs, sizeof(NaClStValue));
58 void NaClStValuePrint(struct Gio* g, NaClStValue* value) {
59 gprintf(g, "<%s : ", NaClStValueKindName(value->kind));
60 switch (value->kind) {
62 gprintf(g, "0x%02"NACL_PRIx8, value->value.byte_value);
65 gprintf(g, "%d", value->value.int_value);
68 gprintf(g, "'%s'", value->value.text_value);
71 gprintf(g, "<defop>");
80 /* Element in the symbol table. */
81 typedef struct NaClSymbolTablePair {
82 const char* name; /* The name of the symbol. */
83 NaClStValue value; /* The value associated with the symbol. */
84 } NaClSymbolTablePair;
86 /* Simple implementation of a symbol table using an array of size capacity. */
87 typedef struct NaClSymbolTable {
88 /* The calling context defines the outer scope for processing
91 struct NaClSymbolTable* calling_context;
92 /* The current size of the symbol table. */
94 /* The maximum size of the symbol table. */
96 /* The array holding the contents of the symbol table. */
97 NaClSymbolTablePair* values;
100 NaClSymbolTable* NaClSymbolTableCreate(
102 NaClSymbolTable* calling_context) {
103 NaClSymbolTable* st = (NaClSymbolTable*) malloc(sizeof(NaClSymbolTable));
105 st->calling_context = calling_context;
107 st->capacity = capacity;
108 st->values = (NaClSymbolTablePair*)
109 calloc(capacity, sizeof(NaClSymbolTablePair));
110 assert(NULL != st->values);
111 DEBUG(NaClLog(LOG_INFO,
112 "NaClSymbolTableCreate(%"NACL_PRIdS") = %p\n",
113 capacity, (void*) st));
117 void NaClSymbolTableDestroy(NaClSymbolTable* st) {
118 DEBUG(NaClLog(LOG_INFO, "NaClSymbolTableDestroy(%p)\n", (void*) st));
122 void NaClSymbolTablePut(
124 struct NaClStValue* value,
125 NaClSymbolTable* st) {
127 DEBUG(NaClLog(LOG_INFO,
128 "NaClSymbolTablePut('%s', ", name);
129 NaClStValuePrint(NaClLogGetGio(), value);
130 gprintf(NaClLogGetGio(), ", %p)\n", (void*) st));
131 /* First see if already in the symbol table. */
132 for (i = 0; i < st->size; ++i) {
133 if (0 == strcmp(name, st->values[i].name)) {
134 NaClStValueAssign(&(st->values[i].value), value);
138 /* If reached, not in symbol table, add. */
139 assert(st->size < st->capacity);
140 st->values[st->size].name = strdup(name);
141 assert(NULL != st->values[st->size].name);
142 NaClStValueAssign(&(st->values[st->size++].value), value);
145 void NaClSymbolTablePutByte(const char* name,
147 struct NaClSymbolTable* st) {
149 value.kind = nacl_byte;
150 value.value.byte_value = byte;
151 NaClSymbolTablePut(name, &value, st);
154 void NaClSymbolTablePutText(const char* name,
156 struct NaClSymbolTable* st) {
158 value.kind = nacl_text;
159 value.value.text_value = text;
160 NaClSymbolTablePut(name, &value, st);
163 void NaClSymbolTablePutInt(const char* name,
165 struct NaClSymbolTable* st) {
167 value.kind = nacl_int;
168 value.value.int_value = ival;
169 NaClSymbolTablePut(name, &value, st);
172 struct NaClStValue* NaClSymbolTableGet(
174 struct NaClSymbolTable* st) {
177 DEBUG(NaClLog(LOG_INFO,
178 "-> NaClSymbolTableGet('%s', %p):\n", name, (void*) st));
180 /* First see if already in the symbol table. */
181 for (i = 0; i < st->size; ++i) {
182 if (0 == strcmp(name, st->values[i].name)) {
183 result = &(st->values[i].value);
184 DEBUG(NaClLog(LOG_INFO, "<- NaClSymbolTableGet = ");
185 NaClStValuePrint(NaClLogGetGio(), result);
186 gprintf(NaClLogGetGio(), ")\n"));
190 /* If reached, not in this symbol table. Try calling context. */
191 st = st->calling_context;
193 /* If reached, not defined in any calling context. */
194 DEBUG(NaClLog(LOG_INFO, "<- NaClSymbolTableGet = NULL\n"));