summaryrefslogtreecommitdiff
path: root/frontends/yasm/yasm.c
blob: 45e76049ca6e8d9c77f64261562876e0ed093e54 (plain)
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
/* $IdPath$
 * Program entry point, command line parsing
 *
 *  Copyright (C) 2001  Peter Johnson
 *
 *  This file is part of YASM.
 *
 *  YASM is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  YASM is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "util.h"

#include <stdio.h>

#ifdef STDC_HEADERS
# include <stdlib.h>
# include <string.h>
#endif

#include <libintl.h>
#define _(String)	gettext(String)
#ifdef gettext_noop
#define N_(String)	gettext_noop(String)
#else
#define N_(String)	(String)
#endif

#include "bitvect.h"

#include "globals.h"
#include "options.h"
#include "errwarn.h"
#include "symrec.h"

#include "bytecode.h"
#include "section.h"
#include "objfmt.h"
#include "preproc.h"
#include "parser.h"

#ifdef DMALLOC
# include <dmalloc.h>
#endif

RCSID("$IdPath$");

#ifndef countof
#define countof(x,y)	(sizeof(x)/sizeof(y))
#endif

static int files_open = 0;
static FILE *in;

/* Forward declarations: cmd line parser handlers */
int opt_option_handler(char *cmd, char *param, int extra);
int opt_format_handler(char *cmd, char *param, int extra);
/* Fake handlers: remove them */
int boo_boo_handler(char *cmd, char *param, int extra);
int b_handler(char *cmd, char *param, int extra);

/* values for asm_options */
#define OPT_SHOW_HELP 0x01

/* command line options */
opt_option options[] =
{
    { 'h', "help",    0, opt_option_handler, OPT_SHOW_HELP, "show help text" },
    { 'f', "oformat", 1, opt_format_handler, 0, "select output format", "<format>" },
    /* Fake handlers: remove them */
    { 'b', NULL,      0, b_handler, 0, "says boom!" },
    {  0,  "boo-boo", 0, boo_boo_handler, 0, "says boo-boo!" },
};

/* help messages */
char help_head[] = "yasm version " VERSION " compiled " __DATE__ "\n"
		   "copyright (c) 2001 Peter Johnson and " PACKAGE " developers\n"
		   "mailto: asm-devel@bilogic.org\n"
		   "\n"
		   "usage: yasm [options|files]+\n"
		   "where options are:\n";
char help_tail[] = "\n"
		   "   files are asm sources to be assembled\n"
		   "\n"
		   "sample invocation:\n"
		   "   yasm -b --boo-boo -f elf -o test.o impl.asm\n"
		   "\n";

/* main function */
int
main(int argc, char *argv[])
{
    sectionhead *sections;

    if (parse_cmdline(argc, argv, options, countof(options, opt_option)))
	return EXIT_FAILURE;

    if (asm_options & OPT_SHOW_HELP) {
	help_msg(help_head, help_tail, options, countof(options, opt_option));
	return EXIT_FAILURE;
    }

    /* Initialize BitVector (needed for floating point). */
    if (BitVector_Boot() != ErrCode_Ok) {
	ErrorNow(_("Could not initialize BitVector"));
	return EXIT_FAILURE;
    }

    /* if no files were specified, fallback to reading stdin */
    if (!files_open)
    {
	in = stdin;
	switch_filename("<STDIN>");
    }

    /* Get initial BITS setting from object format */
    mode_bits = dbg_objfmt.default_mode_bits;

    sections = nasm_parser.do_parse(&nasm_parser, &dbg_objfmt, in);

    if (OutputAllErrorWarning() > 0) {
	sections_delete(sections);
	symrec_delete_all();
	filename_delete_all();
	BitVector_Shutdown();
	return EXIT_FAILURE;
    }

    sections_print(sections);
    printf("\n***Symbol Table***\n");
    symrec_foreach((int (*) (symrec *))symrec_print);

    symrec_parser_finalize();
    sections_parser_finalize(sections);

    printf("Post-parser-finalization:\n");
    sections_print(sections);

    sections_delete(sections);
    symrec_delete_all();
    filename_delete_all();
    BitVector_Shutdown();
    return EXIT_SUCCESS;
}

/*
 *  Command line options handlers
 */
int
not_an_option_handler(char *param)
{
    if (files_open > 0) {
	WarningNow("can open only one input file, only latest file will be processed");
	fclose(in);
    }

    in = fopen(param, "rt");
    if (!in) {
	ErrorNow(_("could not open file `%s'"), param);
	return 1;
    }
    switch_filename(param);

    files_open++;
    return 0;
}

int
opt_option_handler(char *cmd, char *param, int extra)
{
    asm_options |= extra;
    return 0;
}

int
opt_format_handler(char *cmd, char *param, int extra)
{
    printf("selected format: %s\n", param);
    return 0;
}

/* Fake handlers: remove them */
int
boo_boo_handler(char *cmd, char *param, int extra)
{
    printf("boo-boo!\n");
    return 0;
}

int
b_handler(char *cmd, char *param, int extra)
{
    fprintf(stdout, "boom!\n");
    return 0;
}