s502 assembler
A very simple assembler for the 6502 line of processors written in C
Public Member Functions | Data Fields
State Class Reference

Compiler pseudo-global state. More...

#include <state.h>

Public Member Functions

State * state_new ()
 Create a new State object. More...
 
int state_load_instr (State *s, char *fname)
 load instructions from a file More...
 
void state_free (State *s)
 free a State object and all associated memory More...
 
int state_parse_commandline (State *s, int argc, char **argv)
 parse command line arguments and update state More...
 

Data Fields

Map * defines
 defined constants More...
 
Map * labels
 label locations More...
 
TokensList * tokens
 tokens More...
 
Instruction * instr
 instruction data More...
 
int PC
 PC (starts at 0) More...
 
char infile [STATE_MAX_STRING_LEN]
 input file name More...
 
char outfile [STATE_MAX_STRING_LEN]
 output file name More...
 

Detailed Description

Compiler pseudo-global state.

Definition at line 32 of file state.h.

Member Function Documentation

◆ state_free()

void state_free ( State *  s)

free a State object and all associated memory

Parameters
sstate to free

Pointer should be NULLed after this!

Definition at line 48 of file state.c.

48  {
49  if (!s) return;
50  map_free(s->defines);
52  map_free(s->labels);
54  free(s);
55 }

Referenced by main(), and state_new().

◆ state_load_instr()

int state_load_instr ( State *  s,
char *  fname 
)

load instructions from a file

Parameters
fnamefile path relative to CWD
sstate to load into
Returns
0 on success, -1 on error

Definition at line 41 of file state.c.

41  {
42  s->instr = instruction_load(fname);
43  if (s->instr == NULL) return -1;
44  return 0;
45 }

Referenced by main().

◆ state_new()

State * state_new ( )

Create a new State object.

Returns
the new object or NULL

Definition at line 16 of file state.c.

16  {
17  State* ret = (State*)malloc(sizeof(State));
18 
19  if (ret == NULL) {
20  ERROR("Memory allocation error in state_new()");
21  return NULL;
22  }
23 
24  if ((ret->defines = map_new()) == NULL) goto ERR;
25  if ((ret->labels = map_new()) == NULL) goto ERR;
26 
27  ret->instr = NULL;
28  ret->tokens = NULL;
29  ret->PC = 0;
30  ret->infile[0] = 0;
31  strcpy(ret->outfile, "out.bin");
32  return ret;
33 
34 ERR:
35  FAIL("state_new() failed!\n");
36  state_free(ret);
37  return 0;
38 }

Referenced by main().

◆ state_parse_commandline()

int state_parse_commandline ( State *  s,
int  argc,
char **  argv 
)

parse command line arguments and update state

Parameters
sstate to update
argcas in main()
argvas in main()
Returns
0 on success, -1 on error

Undestands: -o –out -d –define -l –log

last parameter is input file

prints usage if has a problem

Definition at line 81 of file state.c.

81  {
82  for (int i = 1; i < argc; i++) {
83  if (!strcmp(argv[i], "-o") || !strcmp(argv[i], "--out")) {
84  // Output argument
85  if (i == argc - 1) {
86  ERROR("Argument '%s' excepts parameter(s)!\n", argv[i]);
88  return -1;
89  }
90  char* param = argv[++i];
91  if (strlen(param) > STATE_MAX_STRING_LEN - 1) {
92  ERROR("Out parameter is too long! (max: %d chars)\n", STATE_MAX_STRING_LEN - 1);
93  return -1;
94  }
95  if (param[0] == '-') {
96  ERROR("Output file name can not start with a dash ('-')!\n");
97  return -1;
98  }
99  strncpy(s->outfile, param, STATE_MAX_STRING_LEN);
100  } else if (!strcmp(argv[i], "-l") || !strcmp(argv[i], "--log")) {
101  // Log argument
102  if (i == argc - 1) {
103  ERROR("Argument '%s' excepts parameter(s)!\n", argv[i]);
105  return -1;
106  }
107  char* param = argv[++i];
108  int level;
109  if (sscanf(param, "%d", &level) != 1) {
110  ERROR("Can not parse log level!\n");
111  return -1;
112  }
113  logging_level(level);
114  } else if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--define")) {
115  // Define argument
116  if (i == argc - 2 || i == argc - 1) {
117  ERROR("Argument '%s' excepts parameter(s)!\n", argv[i]);
119  return -1;
120  }
121  char* name = argv[++i];
122  char* value = argv[++i];
123  int num;
124  if (sscanf(value, "%d", &num) != 1) {
125  ERROR("Can not parse value of define '%s': '%s'!\n", name, value);
126  return -1;
127  }
128  if (strlen(name) > MAP_MAX_KEY_LEN - 1) {
129  ERROR("Define constant name is too long! (max: %d chars)\n", MAP_MAX_KEY_LEN - 1);
130  return -1;
131  }
132  map_set(s->defines, name, num);
133  } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
134  // help
135  print_help(argv[0]);
136  return -1;
137  } else {
138  // rest can only be inputfile
139  if (strlen(argv[i]) > STATE_MAX_STRING_LEN - 1) {
140  ERROR("Input file name is too long! (max: %d chars)\n", STATE_MAX_STRING_LEN - 1);
141  return -1;
142  }
143  if (argv[i][0] == '-') {
144  ERROR("Input file name ('%s') can not start with a dash ('-')!\n", argv[i]);
145  return -1;
146  }
147  strncpy(s->infile, argv[i], STATE_MAX_STRING_LEN);
148  }
149  }
150  if (s->infile[0] == 0) {
151  ERROR("No input file was given!\n");
153  return -1;
154  }
155  return 0;
156 }

Referenced by main().

Field Documentation

◆ defines

Map* State::defines

defined constants

Definition at line 34 of file state.h.

Referenced by main(), number_get_raw(), state_free(), state_new(), and state_parse_commandline().

◆ infile

char State::infile[STATE_MAX_STRING_LEN]

input file name

Definition at line 44 of file state.h.

Referenced by main(), state_new(), and state_parse_commandline().

◆ instr

Instruction* State::instr

instruction data

Definition at line 40 of file state.h.

Referenced by state_free(), state_load_instr(), state_new(), and Token::token_link_instruction().

◆ labels

Map* State::labels

label locations

Definition at line 36 of file state.h.

Referenced by main(), number_get_raw(), pass_one(), state_free(), and state_new().

◆ outfile

char State::outfile[STATE_MAX_STRING_LEN]

output file name

Definition at line 46 of file state.h.

Referenced by state_new(), state_parse_commandline(), and write_data().

◆ PC

int State::PC

PC (starts at 0)

Definition at line 42 of file state.h.

Referenced by pass_one(), and state_new().

◆ tokens

TokensList* State::tokens

tokens

Definition at line 38 of file state.h.

Referenced by concat_bin(), main(), pass_one(), pass_two(), state_free(), and state_new().


The documentation for this class was generated from the following file:
State::labels
Map * labels
label locations
Definition: state.h:36
State::infile
char infile[STATE_MAX_STRING_LEN]
input file name
Definition: state.h:44
State::instr
Instruction * instr
instruction data
Definition: state.h:40
logging_level
int logging_level(int level)
pseudo-global accessor
Definition: logging.c:6
State::tokens
TokensList * tokens
tokens
Definition: state.h:38
State
Compiler pseudo-global state.
Definition: state.h:32
State::defines
Map * defines
defined constants
Definition: state.h:34
print_short_help
void print_short_help()
print the "use --help" text
Definition: state.c:76
State::outfile
char outfile[STATE_MAX_STRING_LEN]
output file name
Definition: state.h:46
State::state_free
void state_free(State *s)
free a State object and all associated memory
Definition: state.c:48
map_set
int map_set(Map *map, char *name, int value)
Definition: map.c:43
MapEntry::name
char name[MAP_MAX_KEY_LEN]
key of the entry
Definition: map.h:22
instruction_load
Instruction * instruction_load(char *fname)
Definition: instructions.c:18
MAP_MAX_KEY_LEN
@ MAP_MAX_KEY_LEN
Key buffer size for Map.
Definition: map.h:14
STATE_MAX_STRING_LEN
@ STATE_MAX_STRING_LEN
max string length for input/output files
Definition: state.h:15
State::PC
int PC
PC (starts at 0)
Definition: state.h:42
map_free
void map_free(Map *map)
Definition: map.c:62
print_help
void print_help(char *pname)
print some help on the useage of the program
Definition: state.c:61
map_new
Map * map_new()
Definition: map.c:17
tokenslist_free
void tokenslist_free(TokensList *list)
Definition: tokenslist.c:78
instruction_free
void instruction_free(Instruction *list)
Definition: instructions.c:121
FAIL
#define FAIL(...)
Fancy-print a fail (failed step). Works like printf.
Definition: logging.h:45
ERROR
#define ERROR(...)
Fancy-print an error (cause of faliure). Works like printf.
Definition: logging.h:40
MapEntry::value
int value
the value the entry holds
Definition: map.h:20