Rizin
unix-like reverse engineering framework and cli tools
z80asm.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2002-2007 Bas Wijnen <wijnen@debian.org>
2 // SPDX-FileCopyrightText: 2005 Jan Wilmans <jw@dds.nl>
3 // SPDX-License-Identifier: GPL-3.0-or-later
4 
5 /* Z80 assembler by shevek
6 
7  Copyright (C) 2002-2007 Bas Wijnen <shevek@fmf.nl>
8  Copyright (C) 2005 Jan Wilmans <jw@dds.nl>
9 
10  This file is part of z80asm.
11 
12  Z80asm is free software; you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 3 of the License, or
15  (at your option) any later version.
16 
17  Z80asm is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program. If not, see <http://www.gnu.org/licenses/>.
24 */
25 
26 #ifndef Z80ASM_H
27 #define Z80ASM_H
28 
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <ctype.h>
34 #include <stdarg.h>
35 
36 /* defines which are not function-specific */
37 #ifndef BUFLEN
38 #define BUFLEN 300 /* size of readbuffer for file i/o */
39 #endif
40 
41 #ifndef MAX_INCLUDE
42 #define MAX_INCLUDE 200 /* stack size for include command and macros */
43 #endif
44 
45 /* types */
46 /* mnemonics. THESE MUST BE IN THE SAME ORDER AS const char *mnemonic[]! */
48 {
62 };
63 
64 /* types of reference */
65 enum reftype
66 {
67  TYPE_BSR, /* bit value (0-7) for bit, set and res */
68  TYPE_DS, /* ds reference (byte count and value) */
69  TYPE_RST, /* rst reference: val & 0x38 == val */
70  TYPE_ABSW, /* absolute word (2 bytes) */
71  TYPE_ABSB, /* absolute byte */
72  TYPE_RELB, /* relative byte */
73  TYPE_LABEL /* equ expression */
74 };
75 
76 /* filetypes that can appear on the input. object files are on the todo list */
78 {
80 };
81 
82 /* labels (will be malloced) */
83 struct label
84 {
85  struct label *next, *prev; /* linked list */
86  int value; /* value */
87  int valid; /* if it is valid, or not yet computed */
88  int busy; /* if it is currently being computed */
89  struct reference *ref; /* mallocced memory to value for computation */
90  char name[1]; /* space with name in it */
91 };
92 
93 /* files that were given on the commandline */
94 struct infile
95 {
96  const char *name;
97  enum filetype type;
98 };
99 
100 /* filenames must be remembered for references */
101 struct name
102 {
103  struct name *next, *prev;
104  char name[1];
105 };
106 
107 /* the include path */
109 {
110  struct includedir *next;
111  char name[1];
112 };
113 
114 /* macro stuff */
115 struct macro_arg
116 {
117  struct macro_arg *next;
118  unsigned pos;
119  unsigned which;
120 };
121 
123 {
124  struct macro_line *next;
125  char *line;
126  struct macro_arg *args;
127 };
128 
129 struct macro
130 {
131  struct macro *next;
132  char *name;
133  unsigned numargs;
134  char **args;
135  struct macro_line *lines;
136 };
137 
138 /* elements on the context stack */
139 struct stack
140 {
141  const char *name; /* filename (for errors). may be malloced */
142  struct includedir *dir; /* directory where it comes from, if any */
143  FILE *file; /* the handle */
144  int line; /* the current line number (for errors) */
145  int shouldclose; /* if this file should be closed when done */
146  struct label *labels; /* local labels for this stack level */
147  /* if file is NULL, this is a macro entry */
148  struct macro *macro;
150  char **macro_args; /* arguments given to the macro */
151 };
152 
153 /* these structs will be malloced for each reference */
154 struct reference
155 {
156  struct reference *next, *prev;
157  enum reftype type; /* type of reference */
158  long oseekpos; /* position in outfile for data */
159  long lseekpos; /* position in listfile for data */
160  char delimiter; /* delimiter for parser */
161  int addr, line; /* address and line of reference */
162  int baseaddr; /* address at start of line of reference */
163  int comma; /* comma when reference was set */
164  int count; /* only for ds: number of items */
165  int infile; /* index in infile[], current infile */
166  int done; /* if this reference has been computed */
167  int computed_value; /* value (only valid if done = true) */
168  int level; /* maximum stack level of labels to use */
169  struct includedir *dir; /* dirname of file (for error reporting) */
170  char *file; /* filename (for error reporting) */
171  char input[1]; /* variable size buffer containing formula */
172 };
173 
174 /* print an error message, including current line and file */
175 static void printerr (int error, const char *fmt, ...);
176 
177 /* skip over spaces in string */
178 static const char *delspc (const char *ptr);
179 
180 static int rd_expr (const char **p, char delimiter, int *valid, int level,
181  int print_errors);
182 static int rd_label (const char **p, int *exists, struct label **previous, int level,
183  int print_errors);
184 static int rd_character (const char **p, int *valid, int print_errors);
185 
186 static int compute_ref (struct reference *ref, int allow_invalid);
187 
188 #endif
void * p
Definition: libc.cpp:67
string FILE
Definition: benchmark.py:21
struct includedir * next
Definition: z80asm.h:110
Definition: z80asm.h:95
enum filetype type
Definition: z80asm.h:97
const char * name
Definition: z80asm.h:96
Definition: dis.h:35
struct reference * ref
Definition: z80asm.h:89
int busy
Definition: z80asm.h:88
struct label * next
Definition: z80asm.h:85
struct label * prev
Definition: z80asm.h:85
int value
Definition: z80asm.h:86
int valid
Definition: z80asm.h:87
unsigned pos
Definition: z80asm.h:118
unsigned which
Definition: z80asm.h:119
struct macro_arg * next
Definition: z80asm.h:117
struct macro_arg * args
Definition: z80asm.h:126
char * line
Definition: z80asm.h:125
struct macro_line * next
Definition: z80asm.h:124
Definition: z80asm.h:130
char * name
Definition: z80asm.h:132
struct macro_line * lines
Definition: z80asm.h:135
unsigned numargs
Definition: z80asm.h:133
struct macro * next
Definition: z80asm.h:131
char ** args
Definition: z80asm.h:134
Definition: z80asm.h:102
struct name * prev
Definition: z80asm.h:103
struct name * next
Definition: z80asm.h:103
char input[1]
Definition: z80asm.h:171
int baseaddr
Definition: z80asm.h:162
long lseekpos
Definition: z80asm.h:159
struct reference * next
Definition: z80asm.h:156
enum reftype type
Definition: z80asm.h:157
long oseekpos
Definition: z80asm.h:158
char delimiter
Definition: z80asm.h:160
int done
Definition: z80asm.h:166
int level
Definition: z80asm.h:168
int count
Definition: z80asm.h:164
struct reference * prev
Definition: z80asm.h:156
int computed_value
Definition: z80asm.h:167
struct includedir * dir
Definition: z80asm.h:169
int comma
Definition: z80asm.h:163
int infile
Definition: z80asm.h:165
int line
Definition: z80asm.h:161
int addr
Definition: z80asm.h:161
char * file
Definition: z80asm.h:170
Definition: z80asm.h:140
struct macro_line * macro_line
Definition: z80asm.h:149
const char * name
Definition: z80asm.h:141
struct macro * macro
Definition: z80asm.h:148
struct includedir * dir
Definition: z80asm.h:142
FILE * file
Definition: z80asm.h:143
struct label * labels
Definition: z80asm.h:146
int shouldclose
Definition: z80asm.h:145
char ** macro_args
Definition: z80asm.h:150
int line
Definition: z80asm.h:144
bool valid
Definition: core.c:77
void error(const char *msg)
Definition: untgz.c:593
static int level
Definition: vmenus.c:2424
static int compute_ref(struct reference *ref, int allow_invalid)
static const char * delspc(const char *ptr)
static void printerr(int error, const char *fmt,...)
reftype
Definition: z80asm.h:66
@ TYPE_LABEL
Definition: z80asm.h:73
@ TYPE_RELB
Definition: z80asm.h:72
@ TYPE_RST
Definition: z80asm.h:69
@ TYPE_BSR
Definition: z80asm.h:67
@ TYPE_ABSB
Definition: z80asm.h:71
@ TYPE_ABSW
Definition: z80asm.h:70
@ TYPE_DS
Definition: z80asm.h:68
mnemonic
Definition: z80asm.h:48
@ Z80_RET
Definition: z80asm.h:55
@ Z80_DI
Definition: z80asm.h:58
@ Z80_CCF
Definition: z80asm.h:53
@ Z80_RRCA
Definition: z80asm.h:51
@ Z80_CPIR
Definition: z80asm.h:49
@ Z80_SEEK
Definition: z80asm.h:61
@ Z80_RETN
Definition: z80asm.h:51
@ Z80_RRD
Definition: z80asm.h:56
@ Z80_DJNZ
Definition: z80asm.h:49
@ Z80_CPI
Definition: z80asm.h:53
@ Z80_RLA
Definition: z80asm.h:56
@ Z80_DM
Definition: z80asm.h:60
@ Z80_POP
Definition: z80asm.h:55
@ Z80_ELSE
Definition: z80asm.h:61
@ Z80_OTIR
Definition: z80asm.h:50
@ Z80_IN
Definition: z80asm.h:59
@ Z80_CPDR
Definition: z80asm.h:49
@ Z80_NEG
Definition: z80asm.h:55
@ Z80_RST
Definition: z80asm.h:56
@ Z80_SRA
Definition: z80asm.h:57
@ Z80_RLD
Definition: z80asm.h:56
@ Z80_OUTI
Definition: z80asm.h:51
@ Z80_EX
Definition: z80asm.h:59
@ Z80_RLC
Definition: z80asm.h:56
@ Z80_CPL
Definition: z80asm.h:53
@ Z80_DEC
Definition: z80asm.h:54
@ Z80_ORG
Definition: z80asm.h:58
@ Z80_INC
Definition: z80asm.h:54
@ Z80_EI
Definition: z80asm.h:58
@ Z80_DB
Definition: z80asm.h:60
@ Z80_SLA
Definition: z80asm.h:57
@ Z80_RRC
Definition: z80asm.h:56
@ Z80_OR
Definition: z80asm.h:59
@ Z80_DEFS
Definition: z80asm.h:52
@ Z80_SLI
Definition: z80asm.h:57
@ Z80_HALT
Definition: z80asm.h:49
@ Z80_LD
Definition: z80asm.h:59
@ Z80_INIR
Definition: z80asm.h:50
@ Z80_RETI
Definition: z80asm.h:51
@ Z80_RL
Definition: z80asm.h:59
@ Z80_DEFW
Definition: z80asm.h:52
@ Z80_SRL
Definition: z80asm.h:58
@ Z80_CP
Definition: z80asm.h:58
@ Z80_ADC
Definition: z80asm.h:52
@ Z80_ENDM
Definition: z80asm.h:61
@ Z80_DW
Definition: z80asm.h:60
@ Z80_XOR
Definition: z80asm.h:58
@ Z80_LDDR
Definition: z80asm.h:50
@ Z80_LDD
Definition: z80asm.h:54
@ Z80_RR
Definition: z80asm.h:60
@ Z80_SLL
Definition: z80asm.h:57
@ Z80_BIT
Definition: z80asm.h:53
@ Z80_DEFM
Definition: z80asm.h:52
@ Z80_SCF
Definition: z80asm.h:57
@ Z80_EQU
Definition: z80asm.h:54
@ Z80_SUB
Definition: z80asm.h:58
@ Z80_INDR
Definition: z80asm.h:49
@ Z80_AND
Definition: z80asm.h:53
@ Z80_RRA
Definition: z80asm.h:56
@ Z80_EXX
Definition: z80asm.h:54
@ Z80_OUT
Definition: z80asm.h:55
@ Z80_CALL
Definition: z80asm.h:49
@ Z80_CPD
Definition: z80asm.h:53
@ Z80_ADD
Definition: z80asm.h:52
@ Z80_LDIR
Definition: z80asm.h:50
@ Z80_DEFB
Definition: z80asm.h:52
@ Z80_PUSH
Definition: z80asm.h:51
@ Z80_ENDIF
Definition: z80asm.h:61
@ Z80_OUTD
Definition: z80asm.h:50
@ Z80_JP
Definition: z80asm.h:59
@ Z80_IM
Definition: z80asm.h:59
@ Z80_DS
Definition: z80asm.h:60
@ Z80_IND
Definition: z80asm.h:54
@ Z80_INI
Definition: z80asm.h:54
@ Z80_LDI
Definition: z80asm.h:55
@ Z80_IF
Definition: z80asm.h:61
@ Z80_JR
Definition: z80asm.h:59
@ Z80_SET
Definition: z80asm.h:57
@ Z80_RLCA
Definition: z80asm.h:51
@ Z80_SBC
Definition: z80asm.h:57
@ Z80_INCBIN
Definition: z80asm.h:60
@ Z80_END
Definition: z80asm.h:61
@ Z80_OTDR
Definition: z80asm.h:50
@ Z80_RES
Definition: z80asm.h:55
@ Z80_MACRO
Definition: z80asm.h:61
@ Z80_DAA
Definition: z80asm.h:53
@ Z80_INCLUDE
Definition: z80asm.h:60
@ Z80_NOP
Definition: z80asm.h:55
static int rd_expr(const char **p, char delimiter, int *valid, int level, int print_errors)
static int rd_label(const char **p, int *exists, struct label **previous, int level, int print_errors)
filetype
Definition: z80asm.h:78
@ FILETYPE_ASM
Definition: z80asm.h:79
static int rd_character(const char **p, int *valid, int print_errors)