1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 1996-2020 The NASM Authors - All Rights Reserved
4  *   See the file AUTHORS included with the NASM distribution for
5  *   the specific copyright holders.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following
9  *   conditions are met:
10  *
11  *   * Redistributions of source code must retain the above copyright
12  *     notice, this list of conditions and the following disclaimer.
13  *   * Redistributions in binary form must reproduce the above
14  *     copyright notice, this list of conditions and the following
15  *     disclaimer in the documentation and/or other materials provided
16  *     with the distribution.
17  *
18  *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19  *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23  *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * ----------------------------------------------------------------------- */
33 
34 /*
35  * listing.h   header file for listing.c
36  */
37 
38 #ifndef NASM_LISTING_H
39 #define NASM_LISTING_H
40 
41 #include "nasm.h"
42 
43 /*
44  * List-file generators should look like this:
45  */
46 struct lfmt {
47     /*
48      * Called to initialize the listing file generator. Before this
49      * is called, the other routines will silently do nothing when
50      * called. The `char *' parameter is the file name to write the
51      * listing to.
52      */
53     void (*init)(const char *fname);
54 
55     /*
56      * Called to clear stuff up and close the listing file.
57      */
58     void (*cleanup)(void);
59 
60     /*
61      * Called to output binary data. Parameters are: the offset;
62      * the data; the data type. Data types are similar to the
63      * output-format interface, only OUT_ADDRESS will _always_ be
64      * displayed as if it's relocatable, so ensure that any non-
65      * relocatable address has been converted to OUT_RAWDATA by
66      * then.
67      */
68     void (*output)(const struct out_data *data);
69 
70     /*
71      * Called to send a text line to the listing generator. The
72      * `int' parameter is LIST_READ or LIST_MACRO depending on
73      * whether the line came directly from an input file or is the
74      * result of a multi-line macro expansion.
75      *
76      * If a line number is provided, print it; if the line number is
77      * -1 then use the same line number as the previous call.
78      */
79     void (*line)(int type, int32_t lineno, const char *line);
80 
81     /*
82      * Called to change one of the various levelled mechanisms in the
83      * listing generator. LIST_INCLUDE and LIST_MACRO can be used to
84      * increase the nesting level of include files and macro
85      * expansions; LIST_TIMES and LIST_INCBIN switch on the two
86      * binary-output-suppression mechanisms for large-scale
87      * pseudo-instructions; the size argument prints the size or
88      * repetiiton count.
89      *
90      * LIST_MACRO_NOLIST is synonymous with LIST_MACRO except that
91      * it indicates the beginning of the expansion of a `nolist'
92      * macro, so anything under that level won't be expanded unless
93      * it includes another file.
94      */
95     void (*uplevel)(int type, int64_t size);
96 
97     /*
98      * Reverse the effects of uplevel.
99      */
100     void (*downlevel)(int type);
101 
102     /*
103      * Called on a warning or error, with the error message.
104      */
105     void printf_func_ptr(2, 3) (*error)(errflags severity, const char *fmt, ...);
106 
107     /*
108      * Update the current offset.  Used to give the listing generator
109      * an offset to work with when doing things like
110      * uplevel(LIST_TIMES) or uplevel(LIST_INCBIN); see
111      * list_set_offset();
112      */
113     void (*set_offset)(uint64_t offset);
114 };
115 
116 extern const struct lfmt *lfmt;
117 extern bool user_nolist;
118 
119 /*
120  * list_options are the requested options; active_list_options gets
121  * set when a pass starts.
122  *
123  * These are simple bitmasks of ASCII-64 mapping directly to option
124  * letters.
125  */
126 extern uint64_t list_options, active_list_options;
127 
128 /*
129  * This maps the characters a-z, A-Z and 0-9 onto a 64-bit bitmask
130  * (with two bits left over for future use! This isn't particularly
131  * efficient code, but just about every instance of it should be
132  * fed a constant, so the entire function can be precomputed at
133  * compile time. The only cases where the full computation is needed
134  * is when parsing the -L option or %pragma list options, neither of
135  * which is in any way performance critical.
136  *
137  * The character + represents ALL listing options except -Lw (flush
138  * after every line.)
139  *
140  * This returns 0 for invalid values, so that no bit is accessed
141  * for unsupported characters.
142  */
list_option_mask_val(unsigned char x)143 static inline const_func uint64_t list_option_mask_val(unsigned char x)
144 {
145     if (x >= 'a') {
146         if (x > 'z')
147             return 0;
148         x = x - 'a';
149     } else if (x >= 'A') {
150         if (x > 'Z')
151             return 0;
152         x = x - 'A' + 26;
153     } else if (x >= '0') {
154         if (x > '9')
155             return 0;
156         x = x - '0' + 26*2;
157     } else {
158         return 0;
159     }
160 
161     return UINT64_C(1) << x;
162 }
163 
list_option_mask(unsigned char x)164 static inline const_func uint64_t list_option_mask(unsigned char x)
165 {
166     if (x == '+')
167         return ~list_option_mask_val('w');
168     else
169         return list_option_mask_val(x);
170 }
171 
list_option(unsigned char x)172 static inline pure_func bool list_option(unsigned char x)
173 {
174     return unlikely(active_list_options & list_option_mask(x));
175 }
176 
177 /* We can't test this using active_list_options for obvious reasons... */
list_on_every_pass(void)178 static inline pure_func bool list_on_every_pass(void)
179 {
180     return unlikely(list_options & list_option_mask('p'));
181 }
182 
183 /* Pragma handler */
184 enum directive_result list_pragma(const struct pragma *);
185 
186 #endif
187