libc8
CHIP-8 utility library
Loading...
Searching...
No Matches
symbol.c File Reference
#include "symbol.h"
#include "../encode.h"
#include "../defs.h"
#include "exception.h"
#include "util.h"
#include <ctype.h>
#include <string.h>

Functions

int build_instruction (instruction_t *ins, symbol_list_t *symbols, int idx)
 Build an instruction from symbols beginning at idx.
 
int is_comment (const char *s)
 Check if the given string is a comment.
 
int is_db (const char *s)
 Check if given string is a DB identifier.
 
int is_ds (const char *s)
 Check if given string is a DS identifier.
 
int is_dw (const char *s)
 Check if given string is a DW identifier.
 
int is_instruction (const char *s)
 Check if the given string is an instruction.
 
int is_label_definition (const char *s)
 Check if the given string is a label definition.
 
int is_label (const char *s, const label_list_t *labels)
 Check if given string is a label reference.
 
int is_register (const char *s)
 Check if the given string represents a V register.
 
int is_reserved_identifier (const char *s)
 Check if given string is a reserved identifier.
 
symbol_tnext_symbol (symbol_list_t *symbols)
 Get the next symbol.
 
int populate_labels (label_list_t *labels)
 Populate label list from lines.
 
int resolve_labels (symbol_list_t *symbols, label_list_t *labels)
 Get byte indexes of label definitions from completed symbol table.
 
int substitute_labels (symbol_list_t *symbols, label_list_t *labels)
 Substitute label symbols with their corresponding int value.
 
int shift (uint16_t fmt)
 Find the bits needed to shift to OR a parameter into an instruction.
 

Variables

const char * c8_identifierStrings []
 
const char * c8_instructionStrings []
 
instruction_format_t formats []
 

Detailed Description

Note
NOT EXPORTED

Stuff for parsing symbols and instructions for encoding CHIP-8 "assembly".

Function Documentation

◆ build_instruction()

int build_instruction ( instruction_t ins,
symbol_list_t symbols,
int  idx 
)

Build an instruction from symbols beginning at idx.

This function builds an instruction from a completely parsed set of symbols (with labels expanded).

Parameters
insinstruction_t to store instruction contents
symbolssymbol list
idxsymbols index of start of instruction
Returns
instruction bytecode

◆ is_comment()

int is_comment ( const char *  s)

Check if the given string is a comment.

Parameters
sthe string to check
Returns
1 if true, 0 if false

◆ is_db()

int is_db ( const char *  s)

Check if given string is a DB identifier.

Returns
1 if true, 0 if false

◆ is_ds()

int is_ds ( const char *  s)

Check if given string is a DS identifier.

Returns
1 if true, 0 if false

◆ is_dw()

int is_dw ( const char *  s)

Check if given string is a DW identifier.

Returns
1 if true, 0 if false

◆ is_instruction()

int is_instruction ( const char *  s)

Check if the given string is an instruction.

Parameters
sthe string to check
Returns
instruction enumerator if true, -1 if false

◆ is_label()

int is_label ( const char *  s,
const label_list_t labels 
)

Check if given string is a label reference.

This function checks if the given string is a label reference by comparing it against the label list. It returns the index of the label in the label list if it is found, or -1 if it is not.

This function assumes that the label list has been populated (e.g. populate_labels() has been called).

Parameters
sstring to check
labelslabel list to check from
Returns
label index if true, -1 otherwise

◆ is_label_definition()

int is_label_definition ( const char *  s)

Check if the given string is a label definition.

Parameters
sthe string to check
Returns
1 if true, 0 if false

◆ is_register()

int is_register ( const char *  s)

Check if the given string represents a V register.

This function checks if the given string starts with 'V' or 'v' and is followed by a valid hexadecimal digit (0-9, A-F, a-f). It returns the register number if it is a valid V register, or -1 if it is not.

Parameters
sstring to check
Returns
V register number if true, -1 otherwise

◆ is_reserved_identifier()

int is_reserved_identifier ( const char *  s)

Check if given string is a reserved identifier.

This function checks if the given string is one of the reserved identifiers defined in c8_identifierStrings. It returns the index of the identifier if it is found, or -1 if it is not.

Parameters
sstring to check
Returns
type of identifier if true, -1 otherwise

◆ next_symbol()

symbol_t * next_symbol ( symbol_list_t symbols)

Get the next symbol.

This function retrieves the next available symbol in the symbol list. If the symbol list is empty, it initializes the first symbol. If the symbol list is full, it reallocates the symbol list to accommodate more symbols.

If symbols is NULL or the symbol list is NULL, it returns NULL.

Parameters
symbolssymbol list to get next symbol from
Returns
first empty symbol in symbol table

◆ populate_labels()

int populate_labels ( label_list_t labels)

Populate label list from lines.

This function scans through the lines of code and identifies label definitions. It checks each line for a label definition (ending with a colon) and adds it to the label list.

If a duplicate label definition is found, it throws a DUPLICATE_LABEL_EXCEPTION.

If too many labels are defined, it throws a TOO_MANY_LABELS_EXCEPTION.

Parameters
lineslines to search
lineCountnumber of lines to search
labelslabel list to populate
Returns
1 if success, 0 if failure

◆ resolve_labels()

int resolve_labels ( symbol_list_t symbols,
label_list_t labels 
)

Get byte indexes of label definitions from completed symbol table.

This function resolves label definitions in the symbol list and populates the label list with the byte index of each label definition. It iterates through the symbols, keeping track of the current byte position in the program. When it encounters a label definition, it records the current byte position in the label list. It also increments the byte position based on the type of symbol encountered (e.g., SYM_DB increments by 1, SYM_INSTRUCTION and SYM_DW increment by 2).

This function assumes that the symbol list has been populated and that the symbols have been parsed correctly. It also assumes that the label list has been initialized.

This function will return 1 if all labels were resolved successfully, or 0 if there was a failure (e.g., if the label list is not fully populated).

This function will throw a NULL_ARGUMENT_EXCEPTION if either symbols or labels is NULL.

Parameters
symbolslist of symbols
labelslist of labels
Returns
1 if success, 0 if failure

◆ shift()

int shift ( uint16_t  fmt)

Find the bits needed to shift to OR a parameter into an instruction.

This function finds the bits needed to shift to OR a parameter into an instruction. It is used to determine the position of parameters in the instruction bytecode.

This is a workaround for the fact that the instruction formats are not defined in a way that allows for easy bit manipulation.

Parameters
fmtinstruction_format_t pformat to check
Returns
number of bits to shift

◆ substitute_labels()

int substitute_labels ( symbol_list_t symbols,
label_list_t labels 
)

Substitute label symbols with their corresponding int value.

This function replaces all symbols of type SYM_LABEL in the symbol list with their corresponding byte value from the label list. It checks if the label exists and throws an INVALID_SYMBOL_EXCEPTION if it does not.

This function assumes that the label list has been populated and that their byte values have been set correctly.

Parameters
symbolssymbols to search
labelslabels to search

Variable Documentation

◆ c8_identifierStrings

const char* c8_identifierStrings[]
Initial value:
= {
"",
"DT" ,
"ST" ,
"I" ,
"[I]" ,
"K" ,
"F" ,
"B" ,
".DB" ,
".DW" ,
".DS" ,
"HF" ,
"R" ,
NULL,
}

Reserved identifier strings. Has to match Symbol.

◆ c8_instructionStrings

const char* c8_instructionStrings[]
Initial value:
= {
"CLS" ,
"RET" ,
"JP" ,
"CALL" ,
"SE" ,
"SNE" ,
"LD" ,
"ADD" ,
"OR" ,
"AND" ,
"SUB" ,
"SHR" ,
"SUBN" ,
"SHL" ,
"RND" ,
"DRW" ,
"SKP" ,
"SKNP" ,
"XOR" ,
"SCD" ,
"SCR" ,
"SCL" ,
"EXIT" ,
"LOW" ,
"HIGH" ,
"JP V0," ,
NULL,
}

Instruction strings. Has to match Instruction.

◆ formats

All valid instruction formats