Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Functions for manipulating expressions designed to be executed on the agent |
61baf725 | 2 | Copyright (C) 1998-2017 Free Software Foundation, Inc. |
c906108c | 3 | |
c5aa993b | 4 | This file is part of GDB. |
c906108c | 5 | |
c5aa993b JM |
6 | This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 8 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 9 | (at your option) any later version. |
c906108c | 10 | |
c5aa993b JM |
11 | This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
c906108c | 15 | |
c5aa993b | 16 | You should have received a copy of the GNU General Public License |
a9762ec7 | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c | 18 | |
c906108c SS |
19 | /* Despite what the above comment says about this file being part of |
20 | GDB, we would like to keep these functions free of GDB | |
21 | dependencies, since we want to be able to use them in contexts | |
22 | outside of GDB (test suites, the stub, etc.) */ | |
23 | ||
24 | #include "defs.h" | |
25 | #include "ax.h" | |
26 | ||
7a292a7a | 27 | #include "value.h" |
175ff332 HZ |
28 | #include "user-regs.h" |
29 | ||
a14ed312 | 30 | static void grow_expr (struct agent_expr *x, int n); |
392a587b | 31 | |
a14ed312 | 32 | static void append_const (struct agent_expr *x, LONGEST val, int n); |
392a587b | 33 | |
a14ed312 | 34 | static LONGEST read_const (struct agent_expr *x, int o, int n); |
392a587b | 35 | |
a14ed312 | 36 | static void generic_ext (struct agent_expr *x, enum agent_op op, int n); |
c906108c SS |
37 | \f |
38 | /* Functions for building expressions. */ | |
39 | ||
833177a4 | 40 | agent_expr::agent_expr (struct gdbarch *gdbarch, CORE_ADDR scope) |
c906108c | 41 | { |
833177a4 PA |
42 | this->len = 0; |
43 | this->size = 1; /* Change this to a larger value once | |
c906108c | 44 | reallocation code is tested. */ |
833177a4 | 45 | this->buf = (unsigned char *) xmalloc (this->size); |
35c9c7ba | 46 | |
833177a4 PA |
47 | this->gdbarch = gdbarch; |
48 | this->scope = scope; | |
c906108c | 49 | |
35c9c7ba | 50 | /* Bit vector for registers used. */ |
833177a4 PA |
51 | this->reg_mask_len = 1; |
52 | this->reg_mask = XCNEWVEC (unsigned char, this->reg_mask_len); | |
92bc6a20 | 53 | |
833177a4 PA |
54 | this->tracing = 0; |
55 | this->trace_string = 0; | |
c906108c SS |
56 | } |
57 | ||
833177a4 | 58 | agent_expr::~agent_expr () |
f23d52e0 | 59 | { |
833177a4 PA |
60 | xfree (this->buf); |
61 | xfree (this->reg_mask); | |
f23d52e0 AC |
62 | } |
63 | ||
c906108c SS |
64 | /* Make sure that X has room for at least N more bytes. This doesn't |
65 | affect the length, just the allocated size. */ | |
66 | static void | |
fba45db2 | 67 | grow_expr (struct agent_expr *x, int n) |
c906108c SS |
68 | { |
69 | if (x->len + n > x->size) | |
70 | { | |
71 | x->size *= 2; | |
72 | if (x->size < x->len + n) | |
73 | x->size = x->len + n + 10; | |
224c3ddb | 74 | x->buf = (unsigned char *) xrealloc (x->buf, x->size); |
c906108c SS |
75 | } |
76 | } | |
77 | ||
78 | ||
79 | /* Append the low N bytes of VAL as an N-byte integer to the | |
80 | expression X, in big-endian order. */ | |
81 | static void | |
fba45db2 | 82 | append_const (struct agent_expr *x, LONGEST val, int n) |
c906108c SS |
83 | { |
84 | int i; | |
85 | ||
86 | grow_expr (x, n); | |
87 | for (i = n - 1; i >= 0; i--) | |
88 | { | |
89 | x->buf[x->len + i] = val & 0xff; | |
90 | val >>= 8; | |
91 | } | |
92 | x->len += n; | |
93 | } | |
94 | ||
95 | ||
96 | /* Extract an N-byte big-endian unsigned integer from expression X at | |
97 | offset O. */ | |
98 | static LONGEST | |
fba45db2 | 99 | read_const (struct agent_expr *x, int o, int n) |
c906108c SS |
100 | { |
101 | int i; | |
102 | LONGEST accum = 0; | |
103 | ||
104 | /* Make sure we're not reading off the end of the expression. */ | |
105 | if (o + n > x->len) | |
3d263c1d | 106 | error (_("GDB bug: ax-general.c (read_const): incomplete constant")); |
c906108c SS |
107 | |
108 | for (i = 0; i < n; i++) | |
109 | accum = (accum << 8) | x->buf[o + i]; | |
c5aa993b | 110 | |
c906108c SS |
111 | return accum; |
112 | } | |
113 | ||
70b8286a SM |
114 | /* See ax.h. */ |
115 | ||
116 | void | |
117 | ax_raw_byte (struct agent_expr *x, gdb_byte byte) | |
118 | { | |
119 | grow_expr (x, 1); | |
120 | x->buf[x->len++] = byte; | |
121 | } | |
c906108c SS |
122 | |
123 | /* Append a simple operator OP to EXPR. */ | |
124 | void | |
fba45db2 | 125 | ax_simple (struct agent_expr *x, enum agent_op op) |
c906108c | 126 | { |
70b8286a | 127 | ax_raw_byte (x, op); |
c906108c SS |
128 | } |
129 | ||
c7f96d2b TT |
130 | /* Append a pick operator to EXPR. DEPTH is the stack item to pick, |
131 | with 0 being top of stack. */ | |
2b52013f | 132 | |
c7f96d2b TT |
133 | void |
134 | ax_pick (struct agent_expr *x, int depth) | |
135 | { | |
136 | if (depth < 0 || depth > 255) | |
137 | error (_("GDB bug: ax-general.c (ax_pick): stack depth out of range")); | |
138 | ax_simple (x, aop_pick); | |
139 | append_const (x, 1, depth); | |
140 | } | |
141 | ||
c906108c SS |
142 | |
143 | /* Append a sign-extension or zero-extension instruction to EXPR, to | |
144 | extend an N-bit value. */ | |
145 | static void | |
fba45db2 | 146 | generic_ext (struct agent_expr *x, enum agent_op op, int n) |
c906108c SS |
147 | { |
148 | /* N must fit in a byte. */ | |
149 | if (n < 0 || n > 255) | |
3d263c1d | 150 | error (_("GDB bug: ax-general.c (generic_ext): bit count out of range")); |
c906108c SS |
151 | /* That had better be enough range. */ |
152 | if (sizeof (LONGEST) * 8 > 255) | |
3e43a32a MS |
153 | error (_("GDB bug: ax-general.c (generic_ext): " |
154 | "opcode has inadequate range")); | |
c906108c SS |
155 | |
156 | grow_expr (x, 2); | |
157 | x->buf[x->len++] = op; | |
158 | x->buf[x->len++] = n; | |
159 | } | |
160 | ||
161 | ||
162 | /* Append a sign-extension instruction to EXPR, to extend an N-bit value. */ | |
163 | void | |
fba45db2 | 164 | ax_ext (struct agent_expr *x, int n) |
c906108c SS |
165 | { |
166 | generic_ext (x, aop_ext, n); | |
167 | } | |
168 | ||
169 | ||
170 | /* Append a zero-extension instruction to EXPR, to extend an N-bit value. */ | |
171 | void | |
fba45db2 | 172 | ax_zero_ext (struct agent_expr *x, int n) |
c906108c SS |
173 | { |
174 | generic_ext (x, aop_zero_ext, n); | |
175 | } | |
176 | ||
177 | ||
178 | /* Append a trace_quick instruction to EXPR, to record N bytes. */ | |
179 | void | |
fba45db2 | 180 | ax_trace_quick (struct agent_expr *x, int n) |
c906108c SS |
181 | { |
182 | /* N must fit in a byte. */ | |
183 | if (n < 0 || n > 255) | |
3e43a32a MS |
184 | error (_("GDB bug: ax-general.c (ax_trace_quick): " |
185 | "size out of range for trace_quick")); | |
c906108c SS |
186 | |
187 | grow_expr (x, 2); | |
188 | x->buf[x->len++] = aop_trace_quick; | |
189 | x->buf[x->len++] = n; | |
190 | } | |
191 | ||
192 | ||
193 | /* Append a goto op to EXPR. OP is the actual op (must be aop_goto or | |
194 | aop_if_goto). We assume we don't know the target offset yet, | |
195 | because it's probably a forward branch, so we leave space in EXPR | |
196 | for the target, and return the offset in EXPR of that space, so we | |
197 | can backpatch it once we do know the target offset. Use ax_label | |
198 | to do the backpatching. */ | |
c5aa993b | 199 | int |
fba45db2 | 200 | ax_goto (struct agent_expr *x, enum agent_op op) |
c906108c SS |
201 | { |
202 | grow_expr (x, 3); | |
203 | x->buf[x->len + 0] = op; | |
204 | x->buf[x->len + 1] = 0xff; | |
205 | x->buf[x->len + 2] = 0xff; | |
206 | x->len += 3; | |
207 | return x->len - 2; | |
208 | } | |
209 | ||
210 | /* Suppose a given call to ax_goto returns some value PATCH. When you | |
211 | know the offset TARGET that goto should jump to, call | |
c5aa993b | 212 | ax_label (EXPR, PATCH, TARGET) |
c906108c SS |
213 | to patch TARGET into the ax_goto instruction. */ |
214 | void | |
fba45db2 | 215 | ax_label (struct agent_expr *x, int patch, int target) |
c906108c SS |
216 | { |
217 | /* Make sure the value is in range. Don't accept 0xffff as an | |
218 | offset; that's our magic sentinel value for unpatched branches. */ | |
219 | if (target < 0 || target >= 0xffff) | |
3d263c1d | 220 | error (_("GDB bug: ax-general.c (ax_label): label target out of range")); |
c5aa993b | 221 | |
c906108c SS |
222 | x->buf[patch] = (target >> 8) & 0xff; |
223 | x->buf[patch + 1] = target & 0xff; | |
224 | } | |
225 | ||
226 | ||
227 | /* Assemble code to push a constant on the stack. */ | |
228 | void | |
fba45db2 | 229 | ax_const_l (struct agent_expr *x, LONGEST l) |
c906108c SS |
230 | { |
231 | static enum agent_op ops[] | |
c5aa993b JM |
232 | = |
233 | {aop_const8, aop_const16, aop_const32, aop_const64}; | |
c906108c SS |
234 | int size; |
235 | int op; | |
236 | ||
237 | /* How big is the number? 'op' keeps track of which opcode to use. | |
238 | Notice that we don't really care whether the original number was | |
239 | signed or unsigned; we always reproduce the value exactly, and | |
240 | use the shortest representation. */ | |
241 | for (op = 0, size = 8; size < 64; size *= 2, op++) | |
44a81774 | 242 | { |
cf3e25ca | 243 | LONGEST lim = ((LONGEST) 1) << (size - 1); |
44a81774 JB |
244 | |
245 | if (-lim <= l && l <= lim - 1) | |
246 | break; | |
247 | } | |
c906108c | 248 | |
0e2de366 | 249 | /* Emit the right opcode... */ |
c906108c SS |
250 | ax_simple (x, ops[op]); |
251 | ||
252 | /* Emit the low SIZE bytes as an unsigned number. We know that | |
253 | sign-extending this will yield l. */ | |
254 | append_const (x, l, size / 8); | |
255 | ||
256 | /* Now, if it was negative, and not full-sized, sign-extend it. */ | |
257 | if (l < 0 && size < 64) | |
258 | ax_ext (x, size); | |
259 | } | |
260 | ||
261 | ||
262 | void | |
fba45db2 | 263 | ax_const_d (struct agent_expr *x, LONGEST d) |
c906108c SS |
264 | { |
265 | /* FIXME: floating-point support not present yet. */ | |
3e43a32a MS |
266 | error (_("GDB bug: ax-general.c (ax_const_d): " |
267 | "floating point not supported yet")); | |
c906108c SS |
268 | } |
269 | ||
270 | ||
271 | /* Assemble code to push the value of register number REG on the | |
272 | stack. */ | |
c5aa993b | 273 | void |
fba45db2 | 274 | ax_reg (struct agent_expr *x, int reg) |
c906108c | 275 | { |
175ff332 HZ |
276 | if (reg >= gdbarch_num_regs (x->gdbarch)) |
277 | { | |
278 | /* This is a pseudo-register. */ | |
279 | if (!gdbarch_ax_pseudo_register_push_stack_p (x->gdbarch)) | |
280 | error (_("'%s' is a pseudo-register; " | |
281 | "GDB cannot yet trace its contents."), | |
282 | user_reg_map_regnum_to_name (x->gdbarch, reg)); | |
283 | if (gdbarch_ax_pseudo_register_push_stack (x->gdbarch, x, reg)) | |
284 | error (_("Trace '%s' failed."), | |
285 | user_reg_map_regnum_to_name (x->gdbarch, reg)); | |
286 | } | |
287 | else | |
288 | { | |
1eb7c2d8 AT |
289 | /* Get the remote register number. */ |
290 | reg = gdbarch_remote_register_number (x->gdbarch, reg); | |
291 | ||
175ff332 HZ |
292 | /* Make sure the register number is in range. */ |
293 | if (reg < 0 || reg > 0xffff) | |
3e43a32a MS |
294 | error (_("GDB bug: ax-general.c (ax_reg): " |
295 | "register number out of range")); | |
175ff332 HZ |
296 | grow_expr (x, 3); |
297 | x->buf[x->len] = aop_reg; | |
298 | x->buf[x->len + 1] = (reg >> 8) & 0xff; | |
299 | x->buf[x->len + 2] = (reg) & 0xff; | |
300 | x->len += 3; | |
301 | } | |
c906108c | 302 | } |
f61e138d SS |
303 | |
304 | /* Assemble code to operate on a trace state variable. */ | |
305 | ||
306 | void | |
307 | ax_tsv (struct agent_expr *x, enum agent_op op, int num) | |
308 | { | |
309 | /* Make sure the tsv number is in range. */ | |
310 | if (num < 0 || num > 0xffff) | |
3e43a32a MS |
311 | internal_error (__FILE__, __LINE__, |
312 | _("ax-general.c (ax_tsv): variable " | |
313 | "number is %d, out of range"), num); | |
f61e138d SS |
314 | |
315 | grow_expr (x, 3); | |
316 | x->buf[x->len] = op; | |
317 | x->buf[x->len + 1] = (num >> 8) & 0xff; | |
318 | x->buf[x->len + 2] = (num) & 0xff; | |
319 | x->len += 3; | |
320 | } | |
d3ce09f5 SS |
321 | |
322 | /* Append a string to the expression. Note that the string is going | |
323 | into the bytecodes directly, not on the stack. As a precaution, | |
324 | include both length as prefix, and terminate with a NUL. (The NUL | |
325 | is counted in the length.) */ | |
326 | ||
327 | void | |
741d92cf | 328 | ax_string (struct agent_expr *x, const char *str, int slen) |
d3ce09f5 SS |
329 | { |
330 | int i; | |
331 | ||
332 | /* Make sure the string length is reasonable. */ | |
333 | if (slen < 0 || slen > 0xffff) | |
334 | internal_error (__FILE__, __LINE__, | |
335 | _("ax-general.c (ax_string): string " | |
336 | "length is %d, out of allowed range"), slen); | |
337 | ||
338 | grow_expr (x, 2 + slen + 1); | |
339 | x->buf[x->len++] = ((slen + 1) >> 8) & 0xff; | |
340 | x->buf[x->len++] = (slen + 1) & 0xff; | |
341 | for (i = 0; i < slen; ++i) | |
342 | x->buf[x->len++] = str[i]; | |
343 | x->buf[x->len++] = '\0'; | |
344 | } | |
c5aa993b | 345 | \f |
c906108c SS |
346 | |
347 | ||
c906108c SS |
348 | /* Functions for disassembling agent expressions, and otherwise |
349 | debugging the expression compiler. */ | |
350 | ||
c5aa993b JM |
351 | struct aop_map aop_map[] = |
352 | { | |
94d5e490 TT |
353 | {0, 0, 0, 0, 0} |
354 | #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \ | |
355 | , { # NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED } | |
356 | #include "ax.def" | |
357 | #undef DEFOP | |
c906108c SS |
358 | }; |
359 | ||
360 | ||
361 | /* Disassemble the expression EXPR, writing to F. */ | |
362 | void | |
fba45db2 | 363 | ax_print (struct ui_file *f, struct agent_expr *x) |
c906108c SS |
364 | { |
365 | int i; | |
c906108c | 366 | |
35c9c7ba SS |
367 | fprintf_filtered (f, _("Scope: %s\n"), paddress (x->gdbarch, x->scope)); |
368 | fprintf_filtered (f, _("Reg mask:")); | |
369 | for (i = 0; i < x->reg_mask_len; ++i) | |
370 | fprintf_filtered (f, _(" %02x"), x->reg_mask[i]); | |
371 | fprintf_filtered (f, _("\n")); | |
372 | ||
c906108c SS |
373 | /* Check the size of the name array against the number of entries in |
374 | the enum, to catch additions that people didn't sync. */ | |
375 | if ((sizeof (aop_map) / sizeof (aop_map[0])) | |
376 | != aop_last) | |
3d263c1d | 377 | error (_("GDB bug: ax-general.c (ax_print): opcode map out of sync")); |
c5aa993b JM |
378 | |
379 | for (i = 0; i < x->len;) | |
c906108c | 380 | { |
aead7601 | 381 | enum agent_op op = (enum agent_op) x->buf[i]; |
c906108c SS |
382 | |
383 | if (op >= (sizeof (aop_map) / sizeof (aop_map[0])) | |
c5aa993b | 384 | || !aop_map[op].name) |
c906108c | 385 | { |
3d263c1d | 386 | fprintf_filtered (f, _("%3d <bad opcode %02x>\n"), i, op); |
c906108c SS |
387 | i++; |
388 | continue; | |
389 | } | |
a04b0428 | 390 | if (i + 1 + aop_map[op].op_size > x->len) |
c906108c | 391 | { |
3d263c1d | 392 | fprintf_filtered (f, _("%3d <incomplete opcode %s>\n"), |
c906108c SS |
393 | i, aop_map[op].name); |
394 | break; | |
395 | } | |
396 | ||
397 | fprintf_filtered (f, "%3d %s", i, aop_map[op].name); | |
a04b0428 | 398 | if (aop_map[op].op_size > 0) |
c906108c SS |
399 | { |
400 | fputs_filtered (" ", f); | |
c5aa993b | 401 | |
c906108c | 402 | print_longest (f, 'd', 0, |
a04b0428 | 403 | read_const (x, i + 1, aop_map[op].op_size)); |
c906108c | 404 | } |
d3ce09f5 SS |
405 | /* Handle the complicated printf arguments specially. */ |
406 | else if (op == aop_printf) | |
407 | { | |
408 | int slen, nargs; | |
409 | ||
410 | i++; | |
411 | nargs = x->buf[i++]; | |
412 | slen = x->buf[i++]; | |
413 | slen = slen * 256 + x->buf[i++]; | |
414 | fprintf_filtered (f, _(" \"%s\", %d args"), | |
415 | &(x->buf[i]), nargs); | |
416 | i += slen - 1; | |
417 | } | |
c906108c | 418 | fprintf_filtered (f, "\n"); |
a04b0428 | 419 | i += 1 + aop_map[op].op_size; |
c906108c SS |
420 | } |
421 | } | |
422 | ||
35c9c7ba SS |
423 | /* Add register REG to the register mask for expression AX. */ |
424 | void | |
425 | ax_reg_mask (struct agent_expr *ax, int reg) | |
426 | { | |
175ff332 | 427 | if (reg >= gdbarch_num_regs (ax->gdbarch)) |
35c9c7ba | 428 | { |
175ff332 HZ |
429 | /* This is a pseudo-register. */ |
430 | if (!gdbarch_ax_pseudo_register_collect_p (ax->gdbarch)) | |
431 | error (_("'%s' is a pseudo-register; " | |
432 | "GDB cannot yet trace its contents."), | |
433 | user_reg_map_regnum_to_name (ax->gdbarch, reg)); | |
434 | if (gdbarch_ax_pseudo_register_collect (ax->gdbarch, ax, reg)) | |
435 | error (_("Trace '%s' failed."), | |
436 | user_reg_map_regnum_to_name (ax->gdbarch, reg)); | |
437 | } | |
438 | else | |
439 | { | |
1eb7c2d8 AT |
440 | int byte; |
441 | ||
442 | /* Get the remote register number. */ | |
443 | reg = gdbarch_remote_register_number (ax->gdbarch, reg); | |
444 | byte = reg / 8; | |
175ff332 HZ |
445 | |
446 | /* Grow the bit mask if necessary. */ | |
447 | if (byte >= ax->reg_mask_len) | |
448 | { | |
449 | /* It's not appropriate to double here. This isn't a | |
450 | string buffer. */ | |
451 | int new_len = byte + 1; | |
224c3ddb SM |
452 | unsigned char *new_reg_mask |
453 | = XRESIZEVEC (unsigned char, ax->reg_mask, new_len); | |
454 | ||
175ff332 HZ |
455 | memset (new_reg_mask + ax->reg_mask_len, 0, |
456 | (new_len - ax->reg_mask_len) * sizeof (ax->reg_mask[0])); | |
457 | ax->reg_mask_len = new_len; | |
458 | ax->reg_mask = new_reg_mask; | |
459 | } | |
460 | ||
461 | ax->reg_mask[byte] |= 1 << (reg % 8); | |
35c9c7ba | 462 | } |
35c9c7ba SS |
463 | } |
464 | ||
465 | /* Given an agent expression AX, fill in requirements and other descriptive | |
466 | bits. */ | |
c906108c | 467 | void |
35c9c7ba | 468 | ax_reqs (struct agent_expr *ax) |
c906108c SS |
469 | { |
470 | int i; | |
471 | int height; | |
472 | ||
3d269a59 JB |
473 | /* Jump target table. targets[i] is non-zero iff we have found a |
474 | jump to offset i. */ | |
c906108c SS |
475 | char *targets = (char *) alloca (ax->len * sizeof (targets[0])); |
476 | ||
3d269a59 JB |
477 | /* Instruction boundary table. boundary[i] is non-zero iff our scan |
478 | has reached an instruction starting at offset i. */ | |
c906108c SS |
479 | char *boundary = (char *) alloca (ax->len * sizeof (boundary[0])); |
480 | ||
3d269a59 | 481 | /* Stack height record. If either targets[i] or boundary[i] is |
c906108c SS |
482 | non-zero, heights[i] is the height the stack should have before |
483 | executing the bytecode at that point. */ | |
484 | int *heights = (int *) alloca (ax->len * sizeof (heights[0])); | |
485 | ||
486 | /* Pointer to a description of the present op. */ | |
487 | struct aop_map *op; | |
488 | ||
c906108c SS |
489 | memset (targets, 0, ax->len * sizeof (targets[0])); |
490 | memset (boundary, 0, ax->len * sizeof (boundary[0])); | |
491 | ||
35c9c7ba SS |
492 | ax->max_height = ax->min_height = height = 0; |
493 | ax->flaw = agent_flaw_none; | |
494 | ax->max_data_size = 0; | |
c906108c | 495 | |
a04b0428 | 496 | for (i = 0; i < ax->len; i += 1 + op->op_size) |
c906108c SS |
497 | { |
498 | if (ax->buf[i] > (sizeof (aop_map) / sizeof (aop_map[0]))) | |
499 | { | |
35c9c7ba | 500 | ax->flaw = agent_flaw_bad_instruction; |
c906108c SS |
501 | return; |
502 | } | |
503 | ||
504 | op = &aop_map[ax->buf[i]]; | |
505 | ||
c5aa993b | 506 | if (!op->name) |
c906108c | 507 | { |
35c9c7ba | 508 | ax->flaw = agent_flaw_bad_instruction; |
c906108c SS |
509 | return; |
510 | } | |
c5aa993b | 511 | |
a04b0428 | 512 | if (i + 1 + op->op_size > ax->len) |
c906108c | 513 | { |
35c9c7ba | 514 | ax->flaw = agent_flaw_incomplete_instruction; |
c906108c SS |
515 | return; |
516 | } | |
517 | ||
3d269a59 JB |
518 | /* If this instruction is a forward jump target, does the |
519 | current stack height match the stack height at the jump | |
520 | source? */ | |
c906108c SS |
521 | if (targets[i] && (heights[i] != height)) |
522 | { | |
35c9c7ba | 523 | ax->flaw = agent_flaw_height_mismatch; |
c906108c SS |
524 | return; |
525 | } | |
526 | ||
527 | boundary[i] = 1; | |
528 | heights[i] = height; | |
529 | ||
a04b0428 | 530 | height -= op->consumed; |
35c9c7ba SS |
531 | if (height < ax->min_height) |
532 | ax->min_height = height; | |
c906108c | 533 | height += op->produced; |
35c9c7ba SS |
534 | if (height > ax->max_height) |
535 | ax->max_height = height; | |
c906108c | 536 | |
35c9c7ba SS |
537 | if (op->data_size > ax->max_data_size) |
538 | ax->max_data_size = op->data_size; | |
c906108c SS |
539 | |
540 | /* For jump instructions, check that the target is a valid | |
c5aa993b JM |
541 | offset. If it is, record the fact that that location is a |
542 | jump target, and record the height we expect there. */ | |
c906108c SS |
543 | if (aop_goto == op - aop_map |
544 | || aop_if_goto == op - aop_map) | |
545 | { | |
546 | int target = read_const (ax, i + 1, 2); | |
547 | if (target < 0 || target >= ax->len) | |
548 | { | |
35c9c7ba | 549 | ax->flaw = agent_flaw_bad_jump; |
c906108c SS |
550 | return; |
551 | } | |
3d269a59 JB |
552 | |
553 | /* Do we have any information about what the stack height | |
554 | should be at the target? */ | |
555 | if (targets[target] || boundary[target]) | |
c906108c | 556 | { |
3d269a59 | 557 | if (heights[target] != height) |
c906108c | 558 | { |
35c9c7ba | 559 | ax->flaw = agent_flaw_height_mismatch; |
c906108c SS |
560 | return; |
561 | } | |
562 | } | |
3d269a59 JB |
563 | |
564 | /* Record the target, along with the stack height we expect. */ | |
565 | targets[target] = 1; | |
566 | heights[target] = height; | |
c906108c | 567 | } |
c5aa993b | 568 | |
c906108c SS |
569 | /* For unconditional jumps with a successor, check that the |
570 | successor is a target, and pick up its stack height. */ | |
571 | if (aop_goto == op - aop_map | |
572 | && i + 3 < ax->len) | |
573 | { | |
c5aa993b | 574 | if (!targets[i + 3]) |
c906108c | 575 | { |
35c9c7ba | 576 | ax->flaw = agent_flaw_hole; |
c906108c SS |
577 | return; |
578 | } | |
579 | ||
580 | height = heights[i + 3]; | |
581 | } | |
582 | ||
583 | /* For reg instructions, record the register in the bit mask. */ | |
584 | if (aop_reg == op - aop_map) | |
585 | { | |
586 | int reg = read_const (ax, i + 1, 2); | |
c906108c | 587 | |
35c9c7ba | 588 | ax_reg_mask (ax, reg); |
c906108c SS |
589 | } |
590 | } | |
591 | ||
592 | /* Check that all the targets are on boundaries. */ | |
593 | for (i = 0; i < ax->len; i++) | |
594 | if (targets[i] && !boundary[i]) | |
595 | { | |
35c9c7ba | 596 | ax->flaw = agent_flaw_bad_jump; |
c906108c SS |
597 | return; |
598 | } | |
599 | ||
35c9c7ba | 600 | ax->final_height = height; |
c906108c | 601 | } |