libc8
CHIP-8 utility library
symbol.h
Go to the documentation of this file.
1
8#ifndef CHIP8_SYMBOL_H
9#define CHIP8_SYMBOL_H
10
11#include <stdint.h>
12#include <stdlib.h>
13
14#define INSTRUCTION_COUNT 64
15#define LABEL_CEILING 64
16#define LABEL_IDENTIFIER_SIZE 20
17#define SYMBOL_CEILING 64
18
19/* Instruction strings */
20#define S_CLS "CLS"
21#define S_RET "RET"
22#define S_JP "JP"
23#define S_CALL "CALL"
24#define S_SE "SE"
25#define S_SNE "SNE"
26#define S_LD "LD"
27#define S_ADD "ADD"
28#define S_OR "OR"
29#define S_AND "AND"
30#define S_SUB "SUB"
31#define S_SHR "SHR"
32#define S_SUBN "SUBN"
33#define S_SHL "SHL"
34#define S_RND "RND"
35#define S_DRW "DRW"
36#define S_SKP "SKP"
37#define S_SKNP "SKNP"
38#define S_XOR "XOR"
39#define S_SCD "SCD"
40#define S_SCR "SCR"
41#define S_SCL "SCL"
42#define S_EXIT "EXIT"
43#define S_LOW "LOW"
44#define S_HIGH "HIGH"
45
46/* Reserved identifier strings */
47#define S_K "K"
48#define S_F "F"
49#define S_B "B"
50#define S_DT "DT"
51#define S_ST "ST"
52#define S_I "I"
53#define S_IP "[I]"
54#define S_DB ".DB"
55#define S_DW ".DW"
56#define S_DS ".DS"
57#define S_HF "HF"
58#define S_R "R"
59
68typedef enum {
69 I_NULL = -1,
96
106typedef enum {
129} Symbol;
130
144typedef struct {
146 uint16_t base;
148 Symbol ptype[3];
149 uint16_t pmask[3];
151
166typedef struct {
167 int line;
170 Symbol ptype[3];
171 int p[3];
174
184typedef struct {
185 char identifier[LABEL_IDENTIFIER_SIZE];
186 int byte;
187} label_t;
188
197typedef struct {
199 int len;
200 int ceil;
202
211typedef struct {
213 uint16_t value;
214 int ln;
215} symbol_t;
216
225typedef struct {
227 int len;
228 int ceil;
230
231extern const char *instructionStrings[];
232extern const char *identifierStrings[];
234
236int is_comment(const char *);
237int is_db(const char *);
238int is_dw(const char *);
239int is_instruction(const char *);
240int is_label_definition(const char *);
241int is_label(const char *, label_list_t *);
242int is_register(const char *);
243int is_reserved_identifier(const char *);
245int populate_labels(char **, int, label_list_t *);
248
249#endif
Represents a valid instruction format.
Definition: symbol.h:144
Instruction cmd
Definition: symbol.h:145
int pcount
Definition: symbol.h:147
uint16_t base
Definition: symbol.h:146
Represents an instruction.
Definition: symbol.h:166
Instruction cmd
Definition: symbol.h:168
int line
Definition: symbol.h:167
int pcount
Definition: symbol.h:169
instruction_format_t * format
Definition: symbol.h:172
Represents a list of labels.
Definition: symbol.h:197
label_t * l
Definition: symbol.h:198
int ceil
Definition: symbol.h:200
int len
Definition: symbol.h:199
Represents a label.
Definition: symbol.h:184
int byte
Definition: symbol.h:186
Represents a symbol with a type, value, and line number.
Definition: symbol.h:225
int ceil
Definition: symbol.h:228
symbol_t * s
Definition: symbol.h:226
int len
Definition: symbol.h:227
Represents a symbol with a type, value, and line number.
Definition: symbol.h:211
uint16_t value
Definition: symbol.h:213
int ln
Definition: symbol.h:214
Symbol type
Definition: symbol.h:212
int is_db(const char *)
Check if given string is a DB identifier.
Definition: symbol.c:216
const char * identifierStrings[]
int is_instruction(const char *)
Check if the given string is an instruction.
Definition: symbol.c:237
int is_label_definition(const char *)
Check if the given string is a label definition.
Definition: symbol.c:255
int populate_labels(char **, int, label_list_t *)
Populate label list from lines.
Definition: symbol.c:350
int build_instruction(instruction_t *, symbol_list_t *, int)
Build an instruction from symbols beginning at idx.
Definition: symbol.c:138
int resolve_labels(symbol_list_t *, label_list_t *)
Get byte indexes of label definitions from completed symbol table.
Definition: symbol.c:397
int is_dw(const char *)
Check if given string is a DW identifier.
Definition: symbol.c:226
Symbol
Represents symbol types.
Definition: symbol.h:106
@ SYM_DW
Definition: symbol.h:116
@ SYM_B
Definition: symbol.h:114
@ SYM_LABEL
Definition: symbol.h:120
@ SYM_STRING
Definition: symbol.h:125
@ SYM_K
Definition: symbol.h:112
@ SYM_INT12
Definition: symbol.h:124
@ SYM_DT
Definition: symbol.h:108
@ SYM_V
Definition: symbol.h:126
@ SYM_F
Definition: symbol.h:113
@ SYM_R
Definition: symbol.h:119
@ SYM_INT
Definition: symbol.h:121
@ SYM_DB
Definition: symbol.h:115
@ SYM_HF
Definition: symbol.h:118
@ SYM_NULL
Definition: symbol.h:107
@ SYM_IP
Definition: symbol.h:111
@ SYM_INT4
Definition: symbol.h:122
@ SYM_LABEL_DEFINITION
Definition: symbol.h:128
@ SYM_I
Definition: symbol.h:110
@ SYM_ST
Definition: symbol.h:109
@ SYM_INT8
Definition: symbol.h:123
@ SYM_INSTRUCTION
Definition: symbol.h:127
@ SYM_DS
Definition: symbol.h:117
instruction_format_t formats[]
Definition: symbol.c:73
int is_register(const char *)
Check if the given string represents a V register.
Definition: symbol.c:295
int substitute_labels(symbol_list_t *, label_list_t *)
Substitute label symbols with their corresponding int value.
Definition: symbol.c:431
const char * instructionStrings[]
Instruction
Represents instruction types.
Definition: symbol.h:68
@ I_XOR
Definition: symbol.h:88
@ I_CALL
Definition: symbol.h:73
@ I_ADD
Definition: symbol.h:77
@ I_AND
Definition: symbol.h:79
@ I_SKP
Definition: symbol.h:86
@ I_SCL
Definition: symbol.h:91
@ I_CLS
Definition: symbol.h:70
@ I_HIGH
Definition: symbol.h:94
@ I_JP
Definition: symbol.h:72
@ I_SCD
Definition: symbol.h:89
@ I_SHL
Definition: symbol.h:83
@ I_RND
Definition: symbol.h:84
@ I_DRW
Definition: symbol.h:85
@ I_NULL
Definition: symbol.h:69
@ I_OR
Definition: symbol.h:78
@ I_SHR
Definition: symbol.h:81
@ I_RET
Definition: symbol.h:71
@ I_LD
Definition: symbol.h:76
@ I_SE
Definition: symbol.h:74
@ I_SUBN
Definition: symbol.h:82
@ I_SNE
Definition: symbol.h:75
@ I_SCR
Definition: symbol.h:90
@ I_EXIT
Definition: symbol.h:92
@ I_SUB
Definition: symbol.h:80
@ I_LOW
Definition: symbol.h:93
@ I_SKNP
Definition: symbol.h:87
symbol_t * next_symbol(symbol_list_t *)
Get the next symbol.
Definition: symbol.c:324
int is_comment(const char *)
Check if the given string is a comment.
Definition: symbol.c:206
int is_reserved_identifier(const char *)
Check if given string is a reserved identifier.
Definition: symbol.c:307
#define LABEL_IDENTIFIER_SIZE
Definition: symbol.h:16
int is_label(const char *, label_list_t *)
Check if given string is a label reference.
Definition: symbol.c:273