1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
/*
* Imported NASM preprocessor - glue code
*
* Copyright (C) 2002-2007 Peter Johnson
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER 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 OR OTHER CONTRIBUTORS 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 <util.h>
#include <libyasm.h>
#include "nasm.h"
#include "nasmlib.h"
#include "nasm-pp.h"
#include "nasm-eval.h"
typedef struct yasm_preproc_nasm {
yasm_preproc_base preproc; /* Base structure */
FILE *in;
char *line;
char *file_name;
long prior_linnum;
int lineinc;
} yasm_preproc_nasm;
yasm_symtab *nasm_symtab;
static yasm_linemap *cur_lm;
static yasm_errwarns *cur_errwarns;
int tasm_compatible_mode = 0;
int tasm_locals;
const char *tasm_segment;
#include "nasm-version.c"
typedef struct preproc_dep {
STAILQ_ENTRY(preproc_dep) link;
char *name;
} preproc_dep;
static STAILQ_HEAD(preproc_dep_head, preproc_dep) *preproc_deps;
static int done_dep_preproc;
yasm_preproc_module yasm_nasm_LTX_preproc;
static void
nil_listgen_init(char *p, efunc e)
{
}
static void
nil_listgen_cleanup(void)
{
}
static void
nil_listgen_output(long v, const void *d, unsigned long v2)
{
}
static void
nil_listgen_line(int v, char *p)
{
}
static void
nil_listgen_uplevel(int v)
{
}
static void
nil_listgen_downlevel(int v)
{
}
static ListGen nil_list = {
nil_listgen_init,
nil_listgen_cleanup,
nil_listgen_output,
nil_listgen_line,
nil_listgen_uplevel,
nil_listgen_downlevel
};
static void
nasm_efunc(int severity, const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
switch (severity & ERR_MASK) {
case ERR_WARNING:
yasm_warn_set_va(YASM_WARN_PREPROC, fmt, va);
break;
case ERR_NONFATAL:
yasm_error_set_va(YASM_ERROR_GENERAL, fmt, va);
break;
case ERR_FATAL:
yasm_fatal(fmt, va);
/*@notreached@*/
break;
case ERR_PANIC:
yasm_internal_error(fmt); /* FIXME */
break;
case ERR_DEBUG:
break;
}
va_end(va);
yasm_errwarn_propagate(cur_errwarns,
yasm_linemap_poke(cur_lm, nasm_src_get_fname(),
(unsigned long)nasm_src_get_linnum()));
}
static yasm_preproc *
nasm_preproc_create(const char *in_filename, yasm_symtab *symtab,
yasm_linemap *lm, yasm_errwarns *errwarns)
{
FILE *f;
yasm_preproc_nasm *preproc_nasm = yasm_xmalloc(sizeof(yasm_preproc_nasm));
preproc_nasm->preproc.module = &yasm_nasm_LTX_preproc;
if (strcmp(in_filename, "-") != 0) {
f = fopen(in_filename, "r");
if (!f)
yasm__fatal( N_("Could not open input file") );
}
else
f = stdin;
preproc_nasm->in = f;
nasm_symtab = symtab;
cur_lm = lm;
cur_errwarns = errwarns;
preproc_deps = NULL;
done_dep_preproc = 0;
preproc_nasm->line = NULL;
preproc_nasm->file_name = NULL;
preproc_nasm->prior_linnum = 0;
preproc_nasm->lineinc = 0;
nasmpp.reset(f, in_filename, 2, nasm_efunc, nasm_evaluate, &nil_list);
pp_extra_stdmac(nasm_version_mac);
return (yasm_preproc *)preproc_nasm;
}
static void
nasm_preproc_destroy(yasm_preproc *preproc)
{
yasm_preproc_nasm *preproc_nasm = (yasm_preproc_nasm *)preproc;
nasmpp.cleanup(0);
if (preproc_nasm->line)
yasm_xfree(preproc_nasm->line);
if (preproc_nasm->file_name)
yasm_xfree(preproc_nasm->file_name);
yasm_xfree(preproc);
if (preproc_deps)
yasm_xfree(preproc_deps);
}
static char *
nasm_preproc_get_line(yasm_preproc *preproc)
{
yasm_preproc_nasm *preproc_nasm = (yasm_preproc_nasm *)preproc;
long linnum;
int altline;
char *line;
if (preproc_nasm->line) {
char *retval = preproc_nasm->line;
preproc_nasm->line = NULL;
return retval;
}
line = nasmpp.getline();
if (!line)
{
nasmpp.cleanup(1);
return NULL; /* EOF */
}
linnum = preproc_nasm->prior_linnum += preproc_nasm->lineinc;
altline = nasm_src_get(&linnum, &preproc_nasm->file_name);
if (altline != 0) {
preproc_nasm->lineinc =
(altline != -1 || preproc_nasm->lineinc != 1);
preproc_nasm->line = line;
line = yasm_xmalloc(40+strlen(preproc_nasm->file_name));
sprintf(line, "%%line %ld+%d %s", linnum,
preproc_nasm->lineinc, preproc_nasm->file_name);
preproc_nasm->prior_linnum = linnum;
}
return line;
}
void
nasm_preproc_add_dep(char *name)
{
preproc_dep *dep;
/* If not processing dependencies, simply return */
if (!preproc_deps)
return;
/* Save in preproc_deps */
dep = yasm_xmalloc(sizeof(preproc_dep));
dep->name = yasm__xstrdup(name);
STAILQ_INSERT_TAIL(preproc_deps, dep, link);
}
static size_t
nasm_preproc_get_included_file(yasm_preproc *preproc, /*@out@*/ char *buf,
size_t max_size)
{
if (!preproc_deps) {
preproc_deps = yasm_xmalloc(sizeof(struct preproc_dep_head));
STAILQ_INIT(preproc_deps);
}
for (;;) {
char *line;
/* Pull first dep out of preproc_deps and return it if there is one */
if (!STAILQ_EMPTY(preproc_deps)) {
char *name;
preproc_dep *dep = STAILQ_FIRST(preproc_deps);
STAILQ_REMOVE_HEAD(preproc_deps, link);
name = dep->name;
yasm_xfree(dep);
strncpy(buf, name, max_size);
buf[max_size-1] = '\0';
yasm_xfree(name);
return strlen(buf);
}
/* No more preprocessing to do */
if (done_dep_preproc) {
return 0;
}
/* Preprocess some more, throwing away the result */
line = nasmpp.getline();
if (line)
yasm_xfree(line);
else
done_dep_preproc = 1;
}
}
static void
nasm_preproc_add_include_file(yasm_preproc *preproc, const char *filename)
{
pp_pre_include(filename);
}
static void
nasm_preproc_predefine_macro(yasm_preproc *preproc, const char *macronameval)
{
char *mnv = yasm__xstrdup(macronameval);
pp_pre_define(mnv);
yasm_xfree(mnv);
}
static void
nasm_preproc_undefine_macro(yasm_preproc *preproc, const char *macroname)
{
char *mn = yasm__xstrdup(macroname);
pp_pre_undefine(mn);
yasm_xfree(mn);
}
static void
nasm_preproc_define_builtin(yasm_preproc *preproc, const char *macronameval)
{
char *mnv = yasm__xstrdup(macronameval);
pp_builtin_define(mnv);
yasm_xfree(mnv);
}
static void
nasm_preproc_add_standard(yasm_preproc *preproc, const char **macros)
{
pp_extra_stdmac(macros);
}
/* Define preproc structure -- see preproc.h for details */
yasm_preproc_module yasm_nasm_LTX_preproc = {
"Real NASM Preprocessor",
"nasm",
nasm_preproc_create,
nasm_preproc_destroy,
nasm_preproc_get_line,
nasm_preproc_get_included_file,
nasm_preproc_add_include_file,
nasm_preproc_predefine_macro,
nasm_preproc_undefine_macro,
nasm_preproc_define_builtin,
nasm_preproc_add_standard
};
static yasm_preproc *
tasm_preproc_create(const char *in_filename, yasm_symtab *symtab,
yasm_linemap *lm, yasm_errwarns *errwarns)
{
tasm_compatible_mode = 1;
return nasm_preproc_create(in_filename, symtab, lm, errwarns);
}
yasm_preproc_module yasm_tasm_LTX_preproc = {
"Real TASM Preprocessor",
"tasm",
tasm_preproc_create,
nasm_preproc_destroy,
nasm_preproc_get_line,
nasm_preproc_get_included_file,
nasm_preproc_add_include_file,
nasm_preproc_predefine_macro,
nasm_preproc_undefine_macro,
nasm_preproc_define_builtin,
nasm_preproc_add_standard
};
|