1 : /*
2 : * Copyright (c) 2012 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.
5 : */
6 :
7 : #include "native_client/src/trusted/validator/x86/decoder/ncop_exps.h"
8 :
9 : #include <stdio.h>
10 : #include <ctype.h>
11 : #include <string.h>
12 : #include <assert.h>
13 : #include <sys/stat.h>
14 :
15 : #include "native_client/src/include/portability.h"
16 : #include "native_client/src/shared/platform/nacl_log.h"
17 : #include "native_client/src/shared/utils/types.h"
18 : #include "native_client/src/trusted/validator/x86/decoder/gen/ncop_expr_node_flag_impl.h"
19 : #include "native_client/src/trusted/validator/x86/decoder/gen/ncop_expr_node_kind_impl.h"
20 : #include "native_client/src/trusted/validator/x86/decoder/nc_decode_tables_types.h"
21 : #include "native_client/src/trusted/validator/x86/decoder/nc_inst_state_internal.h"
22 :
23 : /* To turn on debugging of instruction decoding, change value of
24 : * DEBUGGING to 1.
25 : *
26 : * WARNING: Debugging messages inside of print messages must be sent to the
27 : * same gio stream as being printed, since they may be used by another
28 : * nacl log message that has locked the access to NaClLogGetGio().
29 : */
30 : #define DEBUGGING 0
31 :
32 : #include "native_client/src/shared/utils/debugging.h"
33 :
34 : #include "native_client/src/trusted/validator/x86/decoder/ncopcode_desc_inl.c"
35 : #include "native_client/src/trusted/validator/x86/decoder/ncop_exps_inl.c"
36 :
37 1 : void NaClPrintExpFlags(struct Gio* file, NaClExpFlags flags) {
38 1 : if (flags == 0) {
39 1 : gprintf(file, "0");
40 1 : } else {
41 : NaClExpFlag f;
42 1 : Bool is_first = TRUE;
43 1 : for (f = 0; f < NaClExpFlagEnumSize; f++) {
44 1 : if (flags & NACL_EFLAG(f)) {
45 1 : if (is_first) {
46 1 : is_first = FALSE;
47 1 : } else {
48 1 : gprintf(file, " | ");
49 : }
50 1 : gprintf(file, "%s", NaClExpFlagName(f));
51 : }
52 1 : }
53 : }
54 1 : }
55 :
56 : typedef struct NaClExpKindDescriptor {
57 : /* The name of the expression operator. */
58 : NaClExpKind name;
59 : /* The rank (i.e. number of children) the expression operator has. */
60 : const int rank;
61 : } NaClExpKindDescriptor;
62 :
63 : /* The print names of valid NaClExpKind values. */
64 : static const NaClExpKindDescriptor
65 : g_NaClExpKindDesc[NaClExpKindEnumSize + 1]= {
66 : {UndefinedExp, 0},
67 : {ExprRegister, 0},
68 : {OperandReference, 1},
69 : {ExprConstant, 0},
70 : {ExprSegmentAddress, 2},
71 : {ExprMemOffset, 4},
72 : {ExprNaClIllegal, 0},
73 : };
74 :
75 1 : int NaClExpKindRank(NaClExpKind kind) {
76 1 : assert(kind == g_NaClExpKindDesc[kind].name);
77 1 : return g_NaClExpKindDesc[kind].rank;
78 1 : }
79 :
80 : /* Returns the register defined by the given node. */
81 0 : NaClOpKind NaClGetExpRegister(NaClExp* node) {
82 0 : return NaClGetExpRegisterInline(node);
83 0 : }
84 :
85 : /* Returns the name of the register defined by the indexed node in the
86 : * vector of nodes.
87 : */
88 : NaClOpKind NaClGetExpVectorRegister(NaClExpVector* vector,
89 1 : int node) {
90 1 : return NaClGetExpRegisterInline(&vector->node[node]);
91 1 : }
92 :
93 : static int NaClPrintDisassembledExp(struct Gio* file,
94 : NaClInstState* state,
95 : uint32_t index);
96 :
97 : /* Print the characters in the given string using lower case. */
98 1 : static void NaClPrintLower(struct Gio* file, char* str) {
99 1 : while (*str) {
100 1 : gprintf(file, "%c", tolower(*str));
101 1 : ++str;
102 1 : }
103 1 : }
104 :
105 : /* Print out the given constant expression node to the given file. */
106 : static void NaClPrintDisassembledConst(
107 1 : struct Gio* file, NaClInstState* state, NaClExp* node) {
108 1 : assert(node->kind == ExprConstant);
109 1 : if (node->flags & NACL_EFLAG(ExprJumpTarget)) {
110 : NaClPcAddress target = NaClInstStatePrintableAddress(state)
111 1 : + state->bytes.length + (NaClPcNumber) NaClGetExprSignedValue(node);
112 1 : gprintf(file, "0x%"NACL_PRIxNaClPcAddress, target);
113 1 : }else if (node->flags & NACL_EFLAG(ExprUnsignedHex)) {
114 1 : gprintf(file, "0x%"NACL_PRIx64, NaClGetExprUnsignedValue(node));
115 1 : } else if (node->flags & NACL_EFLAG(ExprSignedHex)) {
116 1 : int64_t val = NaClGetExprSignedValue(node);
117 1 : if (val < 0) {
118 1 : val = -val;
119 1 : gprintf(file, "-0x%"NACL_PRIx64, val);
120 1 : } else {
121 1 : gprintf(file, "0x%"NACL_PRIx64, val);
122 1 : }
123 1 : } else if (node->flags & NACL_EFLAG(ExprUnsignedInt)) {
124 0 : gprintf(file, "%"NACL_PRIu64, NaClGetExprUnsignedValue(node));
125 0 : } else {
126 : /* Assume ExprSignedInt. */
127 1 : gprintf(file, "%"NACL_PRId64, NaClGetExprSignedValue(node));
128 : }
129 1 : }
130 :
131 : #define NACLOP_REG_PREFIX "Reg"
132 :
133 1 : size_t NaClOpRegName(NaClOpKind reg, char* buffer, size_t buffer_size) {
134 1 : const char* name = NaClOpKindName(reg);
135 : char* str;
136 : size_t index;
137 :
138 : /* Fail if no room to put register name. */
139 1 : if (buffer_size == 0) return 0;
140 1 : buffer[0] = '\0'; /* To be safe, in case we exit prematurely. */
141 :
142 : /* Get name for register. */
143 1 : name = NaClOpKindName(reg);
144 1 : if (NULL == name) return 0;
145 :
146 : /* Strip off 'Reg' prefix from register name, if it exists. */
147 1 : str = strstr(name, NACLOP_REG_PREFIX);
148 1 : if (str != name) return 0;
149 1 : str += strlen(NACLOP_REG_PREFIX);
150 :
151 : /* Copy the name, converting characters to lower case. */
152 1 : for (index = 0; (index + 1) < buffer_size; ++index) {
153 1 : char ch = tolower(str[index]);
154 1 : if ('\0' == ch) break;
155 1 : buffer[index] = tolower(str[index]);
156 1 : }
157 :
158 : /* Be sure to add null character at end. */
159 1 : buffer[index] = '\0';
160 1 : return index;
161 1 : }
162 :
163 : #define MAX_REGISTER_SIZE 256
164 :
165 : /* Print out the disassembled representation of the given register
166 : * to the given file.
167 : */
168 1 : static void NaClPrintDisassembledRegKind(struct Gio* file, NaClOpKind reg) {
169 : char buffer[MAX_REGISTER_SIZE];
170 1 : NaClOpRegName(reg, buffer, MAX_REGISTER_SIZE);
171 1 : gprintf(file, "%c%s", '%', buffer);
172 1 : }
173 :
174 1 : static INLINE void NaClPrintDisassembledReg(struct Gio* file, NaClExp* node) {
175 1 : NaClPrintDisassembledRegKind(file, NaClGetExpRegisterInline(node));
176 1 : }
177 :
178 1 : void NaClExpVectorPrint(struct Gio* file, NaClInstState* state) {
179 : uint32_t i;
180 1 : NaClExpVector* vector = NaClInstStateExpVector(state);
181 1 : gprintf(file, "NaClExpVector[%d] = {\n", vector->number_expr_nodes);
182 1 : for (i = 0; i < vector->number_expr_nodes; i++) {
183 1 : NaClExp* node = &vector->node[i];
184 : gprintf(file, " { %s[%d] , ",
185 : NaClExpKindName(node->kind),
186 1 : NaClExpKindRank(node->kind));
187 1 : switch (node->kind) {
188 : case ExprRegister:
189 1 : NaClPrintDisassembledReg(file, node);
190 1 : break;
191 : case ExprConstant:
192 1 : NaClPrintDisassembledConst(file, state, node);
193 1 : break;
194 : default:
195 1 : gprintf(file, "%"NACL_PRIu64, NaClGetExprUnsignedValue(node));
196 : break;
197 : }
198 1 : gprintf(file, ", ");
199 1 : NaClPrintExpFlags(file, node->flags);
200 1 : gprintf(file, " },\n");
201 1 : }
202 1 : gprintf(file, "};\n");
203 1 : }
204 :
205 : /* Print out the given (memory offset) expression node to the given file.
206 : * Returns the index of the node following the given (indexed) memory offset.
207 : */
208 : static int NaClPrintDisassembledMemOffset(struct Gio* file,
209 : NaClInstState *state,
210 1 : int index) {
211 1 : NaClExpVector* vector = NaClInstStateExpVector(state);
212 1 : int r1_index = index + 1;
213 1 : int r2_index = r1_index + NaClExpWidth(vector, r1_index);
214 1 : int scale_index = r2_index + NaClExpWidth(vector, r2_index);
215 1 : int disp_index = scale_index + NaClExpWidth(vector, scale_index);
216 1 : NaClOpKind r1 = NaClGetExpVectorRegister(vector, r1_index);
217 1 : NaClOpKind r2 = NaClGetExpVectorRegister(vector, r2_index);
218 1 : uint64_t scale = NaClGetExprUnsignedValue(&vector->node[scale_index]);
219 1 : int64_t disp = NaClGetExprSignedValue(&vector->node[disp_index]);
220 1 : assert(ExprMemOffset == vector->node[index].kind);
221 1 : gprintf(file,"[");
222 1 : if (r1 != RegUnknown) {
223 1 : NaClPrintDisassembledRegKind(file, r1);
224 : }
225 1 : if (r2 != RegUnknown) {
226 1 : if (r1 != RegUnknown) {
227 1 : gprintf(file, "+");
228 : }
229 1 : NaClPrintDisassembledRegKind(file, r2);
230 1 : gprintf(file, "*%d", (uint32_t) scale);
231 : }
232 1 : if (disp != 0) {
233 : if ((r1 != RegUnknown || r2 != RegUnknown) &&
234 1 : !NaClIsExpNegativeConstant(vector, disp_index)) {
235 1 : gprintf(file, "+");
236 : }
237 : /* Recurse to handle print using format flags. */
238 1 : NaClPrintDisassembledExp(file, state, disp_index);
239 1 : } else if (r1 == RegUnknown && r2 == RegUnknown) {
240 : /* be sure to generate case: [0x0]. */
241 1 : NaClPrintDisassembledExp(file, state, disp_index);
242 : }
243 1 : gprintf(file, "]");
244 1 : return disp_index + NaClExpWidth(vector, disp_index);
245 1 : }
246 :
247 : /* Retrurns true if the segment register of the indexed segment address is DS,
248 : * and DS has been marked (by the instruction) as the default register
249 : * for the segment address.
250 : */
251 : static Bool IsSegmentAddressDsRegPair(NaClInstState* state,
252 1 : int index) {
253 1 : NaClExpVector* vector = NaClInstStateExpVector(state);
254 1 : NaClExp* segment_address = &vector->node[index];
255 : NaClExp* segment_register =
256 1 : &vector->node[NaClGetExpKidIndex(vector, index, 0)];
257 : return NaClHasBit(segment_address->flags, NACL_EFLAG(ExprDSrCase)) &&
258 : (segment_register->kind == ExprRegister) &&
259 1 : (RegDS == NaClGetExpRegisterInline(segment_register));
260 1 : }
261 :
262 : /* Retrurns true if the segment register of the index segment address is ES,
263 : * and ES has been marked (by the instruction) as the default register
264 : * for the segment address.
265 : */
266 : static Bool IsSegmentAddressEsRegPair(NaClInstState* state,
267 1 : int index) {
268 1 : NaClExpVector* vector = NaClInstStateExpVector(state);
269 1 : NaClExp* segment_address = &vector->node[index];
270 : NaClExp* segment_register =
271 1 : &vector->node[NaClGetExpKidIndex(vector, index, 0)];
272 : return NaClHasBit(segment_address->flags, NACL_EFLAG(ExprESrCase)) &&
273 : (segment_register->kind == ExprRegister) &&
274 1 : (RegES == NaClGetExpRegisterInline(segment_register));
275 1 : }
276 :
277 : /* Print out the given (segment address) expression node to the
278 : * given file. Returns the index of the node following the
279 : * given (indexed) segment address.
280 : */
281 : static int NaClPrintDisassembledSegmentAddr(struct Gio* file,
282 : NaClInstState* state,
283 1 : int index) {
284 : int memory_address;
285 1 : NaClExpVector* vector = NaClInstStateExpVector(state);
286 : /* If segment register is default. If so, do not print. */
287 : if (IsSegmentAddressDsRegPair(state, index) ||
288 1 : IsSegmentAddressEsRegPair(state, index)) {
289 : /* Segment register matches default. Don't print. */
290 0 : } else {
291 : /* Print the segment register associated with the segment address. */
292 1 : NaClPrintDisassembledExp(file, state, index + 1);
293 1 : gprintf(file, ":");
294 : }
295 1 : memory_address = NaClGetExpKidIndex(vector, index, 1);
296 1 : if (vector->node[memory_address].kind == ExprRegister) {
297 : /* Special case segment address, where the register corresponds to
298 : * a memory address. Print out the register in '[]' brackets to
299 : * communicate that it is a memory reference.
300 : */
301 : int result;
302 0 : gprintf(file, "[");
303 0 : result = NaClPrintDisassembledExp(file, state, memory_address);
304 0 : gprintf(file, "]");
305 0 : return result;
306 : } else {
307 : /* print out memory address associated with segment address. */
308 1 : return NaClPrintDisassembledExp(file, state, memory_address);
309 : }
310 1 : }
311 :
312 : /* Print out the given expression node to the given file.
313 : * Returns the index of the node following the given indexed
314 : * expression.
315 : */
316 : static int NaClPrintDisassembledExp(struct Gio* file,
317 : NaClInstState* state,
318 1 : uint32_t index) {
319 : NaClExp* node;
320 1 : NaClExpVector* vector = NaClInstStateExpVector(state);
321 1 : assert(index < vector->number_expr_nodes);
322 1 : node = &vector->node[index];
323 1 : switch (node->kind) {
324 : default:
325 0 : gprintf(file, "undefined");
326 0 : return index + 1;
327 : case ExprRegister:
328 1 : NaClPrintDisassembledReg(file, node);
329 1 : return index + 1;
330 : case OperandReference:
331 1 : return NaClPrintDisassembledExp(file, state, index + 1);
332 : case ExprConstant:
333 1 : NaClPrintDisassembledConst(file, state, node);
334 1 : return index + 1;
335 : case ExprSegmentAddress:
336 1 : return NaClPrintDisassembledSegmentAddr(file, state, index);
337 : case ExprMemOffset:
338 1 : return NaClPrintDisassembledMemOffset(file, state, index);
339 : case ExprNaClIllegal:
340 1 : gprintf(file, "*NaClIllegal*");
341 1 : return index + 1;
342 : }
343 1 : }
344 :
345 : /* Returns true if there is a segment override in the segment address
346 : * node defined by vector[seg_addr_index].
347 : *
348 : * Parameters:
349 : * vector - The node expression tree associated with the instruction.
350 : * seg_addr_index - The index to the segment address node to check.
351 : * seg_eflag - The expr flag that must be associated with the
352 : * segment address node to be considered for an override.
353 : * seg_reg - The expected (i.e. default) segment register
354 : * to be associated with the segment address.
355 : */
356 : static Bool NaClHasSegmentOverride(NaClExpVector* vector,
357 : int seg_addr_index,
358 : NaClExpFlag seg_eflag,
359 1 : NaClOpKind seg_reg) {
360 1 : NaClExp* seg_node = &vector->node[seg_addr_index];
361 1 : if (seg_node->flags & NACL_EFLAG(seg_eflag)) {
362 1 : int seg_index = seg_addr_index + 1;
363 1 : NaClExp* node = &vector->node[seg_index];
364 : if ((ExprRegister == node->kind) &&
365 1 : (seg_reg != NaClGetExpRegisterInline(node))) {
366 0 : return TRUE;
367 : }
368 : }
369 1 : return FALSE;
370 1 : }
371 :
372 : /* Prints out the segment register associated with the segment
373 : * address node defined by vector[seg_addr_index].
374 : *
375 : * Parameters:
376 : * file - The Gio file to print the segment register to.
377 : * is_first - True if the first operand of the instruction.
378 : * vector - The node expression tree associated with the instruction.
379 : * seg_addr_index - The index to the segment address node to check.
380 : */
381 : static void NaClPrintSegmentOverride(struct Gio* file,
382 : Bool* is_first,
383 : NaClInstState* state,
384 : NaClExpVector* vector,
385 0 : int seg_addr_index) {
386 0 : int seg_index = seg_addr_index + 1;
387 0 : if (*is_first) {
388 0 : gprintf(file, " ");
389 0 : *is_first = FALSE;
390 0 : } else {
391 0 : gprintf(file, ", ");
392 : }
393 0 : NaClPrintDisassembledExp(file, state, seg_index);
394 0 : }
395 :
396 : /* Print the flag name if the flag is defined for the corresponding operand.
397 : * Used to print out set/use/zero extend information for partial instructions.
398 : */
399 : static void NaClPrintAddOperandFlag(struct Gio* f,
400 : const NaClOp* op,
401 : NaClOpFlag flag,
402 0 : const char* flag_name) {
403 0 : if (op->flags & NACL_OPFLAG(flag)) {
404 0 : gprintf(f, "%s", flag_name);
405 : }
406 0 : }
407 :
408 : /* Print the given instruction opcode of the give state, to the
409 : * given file.
410 : */
411 : static void NaClPrintDisassembled(struct Gio* file,
412 : NaClInstState* state,
413 1 : const NaClInst* inst) {
414 1 : uint32_t tree_index = 0;
415 1 : Bool is_first = TRUE;
416 1 : Bool not_printed_prefix_segment = TRUE;
417 : NaClExp* node;
418 1 : NaClExpVector* vector = NaClInstStateExpVector(state);
419 :
420 : /* Print the name of the instruction. */
421 1 : if (NaClHasBit(inst->flags, NACL_IFLAG(PartialInstruction))) {
422 : /* Instruction has been simplified. Print out corresponding
423 : * hints to the reader, so that they know that the instruction
424 : * has been simplified.
425 : */
426 0 : gprintf(file, "[P] ");
427 0 : NaClPrintLower(file, (char*) NaClMnemonicName(inst->name));
428 0 : if (NaClHasBit(inst->flags, NACL_IFLAG(NaClIllegal))) {
429 0 : gprintf(file, "(illegal)");
430 : }
431 0 : } else {
432 1 : NaClPrintLower(file, (char*) NaClMnemonicName(inst->name));
433 : }
434 :
435 : /* Use the generated expression tree to print out (non-implicit) operands
436 : * of the instruction.
437 : */
438 1 : while (tree_index < vector->number_expr_nodes) {
439 1 : node = &vector->node[tree_index];
440 : if (node->kind != OperandReference ||
441 1 : (NACL_EMPTY_EFLAGS == (node->flags & NACL_EFLAG(ExprImplicit)))) {
442 1 : if (is_first) {
443 1 : gprintf(file, " ");
444 1 : is_first = FALSE;
445 1 : } else {
446 1 : gprintf(file, ", ");
447 : }
448 1 : NaClPrintDisassembledExp(file, state, tree_index);
449 :
450 : /* If this is a partial instruction, add set/use information
451 : * so that that it is more clear what was matched.
452 : */
453 : if (NaClHasBit(inst->flags, NACL_IFLAG(PartialInstruction)) &&
454 1 : node->kind == OperandReference) {
455 : const NaClOp* op =
456 : NaClGetInstOperandInline(state->decoder_tables,
457 : inst,
458 0 : (uint8_t) NaClGetExprUnsignedValue(node));
459 : if (NaClHasBit(op->flags, (NACL_OPFLAG(OpSet) |
460 : NACL_OPFLAG(OpUse) |
461 0 : NACL_OPFLAG(OperandZeroExtends_v)))) {
462 0 : gprintf(file, " (");
463 0 : NaClPrintAddOperandFlag(file, op, OpSet, "s");
464 0 : NaClPrintAddOperandFlag(file, op, OpUse, "u");
465 0 : NaClPrintAddOperandFlag(file, op, OperandZeroExtends_v, "z");
466 0 : gprintf(file, ")");
467 : }
468 : }
469 1 : } else if (not_printed_prefix_segment &&
470 : (OperandReference == node->kind) &&
471 1 : (node->flags & NACL_EFLAG(ExprImplicit))) {
472 : /* Print out segment override of implicit segment address, if
473 : * applicable.
474 : */
475 1 : if (OperandReference == node->kind) {
476 1 : int seg_addr_index = tree_index + 1;
477 1 : if (ExprSegmentAddress == vector->node[seg_addr_index].kind) {
478 : if (NaClHasSegmentOverride(vector, seg_addr_index,
479 1 : ExprDSrCase, RegDS)) {
480 : NaClPrintSegmentOverride(file, &is_first, state, vector,
481 0 : seg_addr_index);
482 0 : } else if (NaClHasSegmentOverride(vector, seg_addr_index,
483 1 : ExprESrCase, RegES)) {
484 : NaClPrintSegmentOverride(file, &is_first, state, vector,
485 0 : seg_addr_index);
486 : }
487 : }
488 : }
489 : }
490 : /* Skip over expression to next expresssion. */
491 1 : tree_index += NaClExpWidth(vector, tree_index);
492 1 : }
493 1 : }
494 :
495 1 : void NaClInstStateInstPrint(struct Gio* file, NaClInstState* state) {
496 : int i;
497 : const NaClInst* inst;
498 :
499 : /* Print out the address and the inst bytes. */
500 1 : int length = NaClInstStateLength(state);
501 :
502 : DEBUG_OR_ERASE(
503 : NaClInstPrint(file, state->decoder_tables, NaClInstStateInst(state)));
504 1 : DEBUG(NaClExpVectorPrint(file, state));
505 : gprintf(file, "%"NACL_PRIxNaClPcAddressAll": ",
506 1 : NaClInstStatePrintableAddress(state));
507 1 : for (i = 0; i < length; ++i) {
508 1 : gprintf(file, "%02"NACL_PRIx8" ", NaClInstStateByte(state, i));
509 1 : }
510 1 : for (i = length; i < NACL_MAX_BYTES_PER_X86_INSTRUCTION; ++i) {
511 1 : gprintf(file, " ");
512 1 : }
513 :
514 : /* Print out the assembly instruction it disassembles to. */
515 1 : inst = NaClInstStateInst(state);
516 1 : NaClPrintDisassembled(file, state, inst);
517 1 : gprintf(file, "\n");
518 1 : }
519 :
520 : /* Defines a buffer size big enough to hold an instruction. */
521 : #define MAX_INST_TEXT_SIZE 256
522 :
523 0 : char* NaClInstStateInstructionToString(struct NaClInstState* state) {
524 : /* Print to a memory buffer, and then duplicate. */
525 : struct GioMemoryFile filemem;
526 0 : struct Gio *file = (struct Gio*) &filemem;
527 : char buffer[MAX_INST_TEXT_SIZE];
528 : char* result;
529 :
530 : /* Note: Be sure to leave an extra byte to add the null character to
531 : * the end of the string.
532 : */
533 0 : GioMemoryFileCtor(&filemem, buffer, MAX_INST_TEXT_SIZE - 1);
534 0 : NaClInstStateInstPrint(file, state);
535 : buffer[filemem.curpos < MAX_INST_TEXT_SIZE
536 0 : ? filemem.curpos : MAX_INST_TEXT_SIZE] ='\0';
537 0 : result = strdup(buffer);
538 0 : GioMemoryFileDtor(file);
539 0 : return result;
540 0 : }
541 :
542 1 : int NaClExpWidth(NaClExpVector* vector, int node) {
543 : int i;
544 1 : int count = 1;
545 1 : int num_kids = NaClExpKindRank(vector->node[node].kind);
546 1 : for (i = 0; i < num_kids; i++) {
547 1 : count += NaClExpWidth(vector, node + count);
548 1 : }
549 1 : return count;
550 1 : }
551 :
552 1 : int NaClGetExpKidIndex(NaClExpVector* vector, int node, int kid) {
553 1 : node++;
554 1 : while (kid-- > 0) {
555 1 : node += NaClExpWidth(vector, node);
556 1 : }
557 1 : return node;
558 1 : }
559 :
560 0 : int NaClGetExpParentIndex(NaClExpVector* vector, int index) {
561 : int node_rank;
562 0 : int num_kids = 1;
563 0 : while (index > 0) {
564 0 : --index;
565 0 : node_rank = NaClExpKindRank(vector->node[index].kind);
566 0 : if (node_rank >= num_kids) {
567 0 : return index;
568 : } else {
569 0 : num_kids -= (node_rank - 1);
570 : }
571 0 : }
572 0 : return -1;
573 0 : }
574 :
575 : int NaClGetNthExpKind(NaClExpVector* vector,
576 : NaClExpKind kind,
577 0 : int n) {
578 0 : if (n > 0) {
579 : uint32_t i;
580 0 : for (i = 0; i < vector->number_expr_nodes; ++i) {
581 0 : if (kind == vector->node[i].kind) {
582 0 : --n;
583 0 : if (n == 0) return i;
584 : }
585 0 : }
586 : }
587 0 : return -1;
588 0 : }
589 :
590 1 : Bool NaClIsExpNegativeConstant(NaClExpVector* vector, int index) {
591 1 : NaClExp* node = &vector->node[index];
592 1 : switch (node->kind) {
593 : case ExprConstant:
594 : if (node->flags & NACL_EFLAG(ExprUnsignedHex) ||
595 1 : node->flags & NACL_EFLAG(ExprUnsignedInt)) {
596 0 : return FALSE;
597 : } else {
598 : /* Assume signed value. */
599 1 : return NaClGetExprSignedValue(node) < 0;
600 : }
601 : break;
602 : default:
603 : break;
604 : }
605 0 : return FALSE;
606 1 : }
607 :
608 : /* Dummy routine to allow unreferenced NaClGetInstNumberOperandsInline
609 : * inline.
610 : */
611 0 : uint8_t NaClNcopExpsDummyNaClGetInstNumberOperands(const NaClInst* inst) {
612 0 : return NaClGetInstNumberOperandsInline(inst);
613 0 : }
|