Fixed building on GCC and some valgrind warnings. Read .dbg.natives table now.
6 #include "decompiler.h"
7 #include "platform_util.h"
9 sp_decomp_t *Sp_InitDecomp(const char *file, int *err)
14 if ((plugin = Sp_ReadPlugin(file, err)) == NULL)
20 memset(dc, 0, sizeof(sp_decomp_t));
22 dc->pcode_map = (cell_t *)malloc(dc->plugin->pcode_size);
23 memset(dc->pcode_map, 0, dc->plugin->pcode_size);
25 #define OPDEF(num,enm,str,prms) \
26 dc->opdef[num].name = str; \
27 dc->opdef[num].params = prms;
28 #include "opcodes.tbl"
31 if (plugin->debug.ntv != NULL)
33 assert(plugin->debug.ntv->num_entries == plugin->num_natives);
34 dc->natives = new FunctionInfo[plugin->num_natives];
35 memset(dc->natives, 0, sizeof(FunctionInfo) * plugin->num_natives);
37 /* Unfurl this mess. */
38 uint8_t *cursor = (uint8_t *)plugin->debug.ntv;
40 cursor += sizeof(sp_fdbg_ntvtab_t);
41 for (uint32_t i = 0; i < plugin->num_natives; i++)
43 sp_fdbg_native_t *native = (sp_fdbg_native_t *)cursor;
45 uint32_t idx = native->index;
47 assert(dc->natives[idx].name[0] == '\0');
49 Sp_Format(dc->natives[idx].name,
50 sizeof(dc->natives[idx].name),
52 plugin->debug.stringbase + native->name);
53 dc->natives[idx].tag = Sp_FindTag(plugin, native->tagid);
54 dc->natives[idx].num_known_args = native->nargs;
56 cursor += sizeof(sp_fdbg_native_t);
58 for (uint32_t j = 0; j < dc->natives[idx].num_known_args; j++)
60 sp_fdbg_ntvarg_t *arg = (sp_fdbg_ntvarg_t *)cursor;
61 dc->natives[idx].known_args[j].name =
62 plugin->debug.stringbase + arg->name;
63 dc->natives[idx].known_args[j].tag =
64 Sp_FindTag(plugin, arg->tagid);
65 dc->natives[idx].known_args[j].dimcount = arg->dimcount;
67 cursor += sizeof(sp_fdbg_ntvarg_t);
68 if (arg->dimcount != 0)
70 dc->natives[idx].known_args[j].dims = (sp_fdbg_arraydim_t *)cursor;
71 cursor += sizeof(sp_fdbg_arraydim_t) * arg->dimcount;
80 void Sp_FreeDecomp(sp_decomp_t *decomp)
82 FunctionInfo *info, *info_temp;
87 info_temp = info->next;
92 Sp_FreePlugin(decomp->plugin);
93 free(decomp->pcode_map);
94 delete [] decomp->natives;
98 PrintBuffer::PrintBuffer() : printbuf(NULL), printbuf_alloc(0), printbuf_pos(0)
102 PrintBuffer::~PrintBuffer()
107 void PrintBuffer::Grow(size_t len)
109 if (printbuf_pos + len > printbuf_alloc)
111 if (printbuf_alloc == 0)
113 printbuf_alloc = len * 8;
117 printbuf_alloc += len;
121 printbuf = (char *)realloc(printbuf, printbuf_alloc);
125 void PrintBuffer::Append(const char *fmt, ...)
132 len = Sp_FormatArgs(buffer, sizeof(buffer), fmt, ap);
137 strcpy(&printbuf[printbuf_pos], buffer);
141 void PrintBuffer::Set(const char *fmt, ...)
148 len = Sp_FormatArgs(buffer, sizeof(buffer), fmt, ap);
154 printbuf = (char *)realloc(printbuf, printbuf_alloc);
155 strcpy(&printbuf[printbuf_pos], buffer);
159 void PrintBuffer::Prepend(const char *fmt, ...)
166 len = Sp_FormatArgs(buffer, sizeof(buffer), fmt, ap);
171 /* Move old data forward. Move one extra byte for the null terminator. */
172 memmove(&printbuf[len], printbuf, printbuf_pos + 1);
174 /* Move the new data in. */
175 memcpy(printbuf, buffer, len);
178 void PrintBuffer::Reset()
183 void PrintBuffer::Dump(FILE *fp)
185 fprintf(fp, "%s", printbuf);