libc8
CHIP-8 utility library
Loading...
Searching...
No Matches
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#define S_JP_V0 "JP V0,"
46
47/* Reserved identifier strings */
48#define S_K "K"
49#define S_F "F"
50#define S_B "B"
51#define S_DT "DT"
52#define S_ST "ST"
53#define S_I "I"
54#define S_IP "[I]"
55#define S_DB ".DB"
56#define S_DW ".DW"
57#define S_DS ".DS"
58#define S_HF "HF"
59#define S_R "R"
60
69typedef enum {
70 I_NULL = -1,
98
108typedef enum {
131} Symbol;
132
146typedef struct {
148 uint16_t base;
150 Symbol ptype[3];
151 uint16_t pmask[3];
153
168typedef struct {
169 int line;
172 Symbol ptype[3];
173 int p[3];
176
186typedef struct {
187 char identifier[LABEL_IDENTIFIER_SIZE];
188 int byte;
189} label_t;
190
199typedef struct {
201 int len;
202 int ceil;
204
213typedef struct {
215 uint16_t value;
216 int ln;
217} symbol_t;
218
227typedef struct {
229 int len;
230 int ceil;
232
233extern const char* c8_instructionStrings[];
234extern const char* c8_identifierStrings[];
236
238int is_comment(const char*);
239int is_db(const char*);
240int is_ds(const char*);
241int is_dw(const char*);
242int is_instruction(const char*);
243int is_label_definition(const char*);
244int is_label(const char*, const label_list_t*);
245int is_register(const char*);
246int is_reserved_identifier(const char*);
250int shift(uint16_t);
252
253#endif
Represents a valid instruction format.
Definition: symbol.h:146
Instruction cmd
Definition: symbol.h:147
int pcount
Definition: symbol.h:149
uint16_t base
Definition: symbol.h:148
Represents an instruction.
Definition: symbol.h:168
Instruction cmd
Definition: symbol.h:170
int line
Definition: symbol.h:169
int pcount
Definition: symbol.h:171
instruction_format_t * format
Definition: symbol.h:174
Represents a list of labels.
Definition: symbol.h:199
label_t * l
Definition: symbol.h:200
int ceil
Definition: symbol.h:202
int len
Definition: symbol.h:201
Represents a label.
Definition: symbol.h:186
int byte
Definition: symbol.h:188
Represents a symbol with a type, value, and line number.
Definition: symbol.h:227
int ceil
Definition: symbol.h:230
symbol_t * s
Definition: symbol.h:228
int len
Definition: symbol.h:229
Represents a symbol with a type, value, and line number.
Definition: symbol.h:213
uint16_t value
Definition: symbol.h:215
int ln
Definition: symbol.h:216
Symbol type
Definition: symbol.h:214
int is_db(const char *)
Check if given string is a DB identifier.
Definition: symbol.c:165
int is_label(const char *, const label_list_t *)
Check if given string is a label reference.
Definition: symbol.c:233
int is_instruction(const char *)
Check if the given string is an instruction.
Definition: symbol.c:193
int is_label_definition(const char *)
Check if the given string is a label definition.
Definition: symbol.c:209
int build_instruction(instruction_t *, symbol_list_t *, int)
Build an instruction from symbols beginning at idx.
Definition: symbol.c:140
int is_ds(const char *)
Check if given string is a DS identifier.
Definition: symbol.c:174
int resolve_labels(symbol_list_t *, label_list_t *)
Get byte indexes of label definitions from completed symbol table.
Definition: symbol.c:393
int is_dw(const char *)
Check if given string is a DW identifier.
Definition: symbol.c:183
const char * c8_identifierStrings[]
Definition: symbol.c:21
Symbol
Represents symbol types.
Definition: symbol.h:108
@ SYM_DW
Definition: symbol.h:118
@ SYM_B
Definition: symbol.h:116
@ SYM_LABEL
Definition: symbol.h:122
@ SYM_STRING
Definition: symbol.h:127
@ SYM_K
Definition: symbol.h:114
@ SYM_INT12
Definition: symbol.h:126
@ SYM_DT
Definition: symbol.h:110
@ SYM_V
Definition: symbol.h:128
@ SYM_F
Definition: symbol.h:115
@ SYM_R
Definition: symbol.h:121
@ SYM_INT
Definition: symbol.h:123
@ SYM_DB
Definition: symbol.h:117
@ SYM_HF
Definition: symbol.h:120
@ SYM_NULL
Definition: symbol.h:109
@ SYM_IP
Definition: symbol.h:113
@ SYM_INT4
Definition: symbol.h:124
@ SYM_LABEL_DEFINITION
Definition: symbol.h:130
@ SYM_I
Definition: symbol.h:112
@ SYM_ST
Definition: symbol.h:111
@ SYM_INT8
Definition: symbol.h:125
@ SYM_INSTRUCTION
Definition: symbol.h:129
@ SYM_DS
Definition: symbol.h:119
int shift(uint16_t)
Find the bits needed to shift to OR a parameter into an instruction.
Definition: symbol.c:619
instruction_format_t formats[]
Definition: symbol.c:74
int is_register(const char *)
Check if the given string represents a V register.
Definition: symbol.c:258
const char * c8_instructionStrings[]
Definition: symbol.c:41
int substitute_labels(symbol_list_t *, label_list_t *)
Substitute label symbols with their corresponding int value.
Definition: symbol.c:432
Instruction
Represents instruction types.
Definition: symbol.h:69
@ I_XOR
Definition: symbol.h:89
@ I_CALL
Definition: symbol.h:74
@ I_ADD
Definition: symbol.h:78
@ I_AND
Definition: symbol.h:80
@ I_SKP
Definition: symbol.h:87
@ I_SCL
Definition: symbol.h:92
@ I_JP_V0
Definition: symbol.h:96
@ I_CLS
Definition: symbol.h:71
@ I_HIGH
Definition: symbol.h:95
@ I_JP
Definition: symbol.h:73
@ I_SCD
Definition: symbol.h:90
@ I_SHL
Definition: symbol.h:84
@ I_RND
Definition: symbol.h:85
@ I_DRW
Definition: symbol.h:86
@ I_NULL
Definition: symbol.h:70
@ I_OR
Definition: symbol.h:79
@ I_SHR
Definition: symbol.h:82
@ I_RET
Definition: symbol.h:72
@ I_LD
Definition: symbol.h:77
@ I_SE
Definition: symbol.h:75
@ I_SUBN
Definition: symbol.h:83
@ I_SNE
Definition: symbol.h:76
@ I_SCR
Definition: symbol.h:91
@ I_EXIT
Definition: symbol.h:93
@ I_SUB
Definition: symbol.h:81
@ I_LOW
Definition: symbol.h:94
@ I_SKNP
Definition: symbol.h:88
symbol_t * next_symbol(symbol_list_t *)
Get the next symbol.
Definition: symbol.c:296
int is_comment(const char *)
Check if the given string is a comment.
Definition: symbol.c:156
int populate_labels(label_list_t *)
Populate label list from lines.
Definition: symbol.c:330
int is_reserved_identifier(const char *)
Check if given string is a reserved identifier.
Definition: symbol.c:272
#define LABEL_IDENTIFIER_SIZE
Definition: symbol.h:16