s502 assembler
A very simple assembler for the 6502 line of processors written in C
Enumerations | Functions
directive.h File Reference

step 1 and 3 processing for directive tokens More...

#include "tokenslist.h"
#include "state.h"

Go to the source code of this file.

Enumerations

enum  DIRCommand {
  DIR_STOP = -1, DIR_NOP = 0, DIR_IF_TRUE, DIR_IF_FALSE,
  DIR_ENDIF
}
 Internal command type for directives. More...
 

Functions

enum DIRCommand do_directive_token (State *s, TokensListElement *ptr, int skip)
 process a directive token More...
 
int directive_compile (State *s, Token *t, char **dataptr)
 Compile a directive into binary data. More...
 

Detailed Description

step 1 and 3 processing for directive tokens

Public interface for directive.h

Definition in file directive.h.

Enumeration Type Documentation

◆ DIRCommand

enum DIRCommand

Internal command type for directives.

Token processor functions return these to signal different commands to pass one

Enumerator
DIR_STOP 

An error occured, stop compilation.

DIR_NOP 

Nothing special, do nothing.

DIR_IF_TRUE 

A conditional evaluated to true.

DIR_IF_FALSE 

A conditional evaluated to false.

DIR_ENDIF 

An endif was encountered, should pop from state stack.

Definition at line 19 of file directive.h.

19  {
21  DIR_STOP = -1,
23  DIR_NOP = 0,
29  DIR_ENDIF,
30 };

Function Documentation

◆ directive_compile()

int directive_compile ( State s,
Token t,
char **  dataptr 
)

Compile a directive into binary data.

Parameters
dataptrreturn buffer for data
sassembler state
ttoken to compile
Returns
number of bytes in buffer or -1 on error

Simply relays it to compile_pad or compile_data

Definition at line 498 of file directive.c.

498  {
499  // data or pad
500  if (util_match_string(t->stripped, ".data", strlen(".data")) == 0)
501  return compile_data(s, t, dataptr);
502  if (util_match_string(t->stripped, ".pad", strlen(".pad")) == 0)
503  return compile_pad(s, t, dataptr);
504  return -1;
505 }

References compile_data(), compile_pad(), Token::stripped, and util_match_string().

Referenced by token_compile().

◆ do_directive_token()

enum DIRCommand do_directive_token ( State s,
TokensListElement ptr,
int  skip 
)

process a directive token

Parameters
sstate of the compiler
ptrptr to the current token in the list
skipdisable compilation flag (1=disabled)
Returns
DIRCommand to pass 1

Identifies directive type, and runs a processor function on it.

Definition at line 1 of file directive.c.

362  {
363  char* directive = ptr->token->stripped + 1;
364 
365  if (skip) {
366  for (int i = 0; i < sizeof(skipProcessors) / sizeof(skipProcessors[0]); i++) {
367  if (util_match_string(directive, skipProcessors[i].name, strlen(skipProcessors[i].name)) == 0) {
368  return skipProcessors[i].ret;
369  }
370  }
371  return DIR_NOP;
372  }
373 
374  for (int i = 0; i < sizeof(processors) / sizeof(processors[0]); i++) {
375  if (util_match_string(directive, processors[i].name, strlen(processors[i].name)) == 0) {
376  return processors[i].p(s, ptr);
377  }
378  }
379 
380  ERROR("Unknown directive: %s\n", directive);
381  token_print(ptr->token);
382  return DIR_STOP;
383 }

Referenced by pass_one().

Token::stripped
char stripped[TOKEN_BUFFER_SIZE]
stripped text from source file
Definition: token_t.h:55
DIR_STOP
@ DIR_STOP
An error occured, stop compilation.
Definition: directive.h:21
DIR_NOP
@ DIR_NOP
Nothing special, do nothing.
Definition: directive.h:23
TokensListElement::token
Token * token
Token value.
Definition: tokenslist.h:18
DIR_ENDIF
@ DIR_ENDIF
An endif was encountered, should pop from state stack.
Definition: directive.h:29
skipProcessors
struct @1 skipProcessors[]
List of tokens to "process" when skipping tokens due to a falsy if.
processors
struct @0 processors[]
The list of all processor functions and their tokens.
util_match_string
int util_match_string(char *first, char *second, int count)
case-insensitive memcmp
Definition: util.c:32
compile_data
int compile_data(State *s, Token *t, char **dataptr)
Compile a .data directive into binary data.
Definition: directive.c:396
DIR_IF_FALSE
@ DIR_IF_FALSE
A conditional evaluated to false.
Definition: directive.h:27
Token::token_print
void token_print(Token *token)
Pretty-print one token, with its source and length.
Definition: tokenFunc.c:20
DIR_IF_TRUE
@ DIR_IF_TRUE
A conditional evaluated to true.
Definition: directive.h:25
compile_pad
int compile_pad(State *s, Token *t, char **dataptr)
Compile a .pad directive into binary data.
Definition: directive.c:469
ERROR
#define ERROR(...)
Fancy-print an error (cause of faliure). Works like printf.
Definition: logging.h:40