Browse Source

Implement most of resolve_defaults(); fix for declsize default behavior.

master
Ben Kurtovic 9 years ago
parent
commit
05e0cbb0fb
7 changed files with 38 additions and 17 deletions
  1. +13
    -7
      src/assembler.c
  2. +1
    -1
      src/assembler/parse_util.c
  3. +18
    -6
      src/assembler/preprocessor.c
  4. +1
    -1
      src/assembler/state.c
  5. +1
    -0
      src/assembler/state.h
  6. +2
    -2
      src/util.c
  7. +2
    -0
      src/util.h

+ 13
- 7
src/assembler.c View File

@@ -9,6 +9,8 @@
#include "assembler/preprocessor.h" #include "assembler/preprocessor.h"
#include "assembler/state.h" #include "assembler/state.h"
#include "logging.h" #include "logging.h"
#include "rom.h"
#include "util.h"


/* /*
Tokenize ASMLines into ASMInstructions. Tokenize ASMLines into ASMInstructions.
@@ -48,17 +50,21 @@ static ErrorInfo* tokenize(AssemblerState *state)
*/ */
static ErrorInfo* resolve_defaults(AssemblerState *state) static ErrorInfo* resolve_defaults(AssemblerState *state)
{ {
// TODO
if (!state->rom_size) {
state->rom_size = 32 << 10;


// if (!state.rom_size)
// set to max possible >= 32 KB (max of instructions, offset, and state.header.rom_size)
// TODO: use highest instruction too


// if (!state.header.rom_size)
// set to actual rom size using util's size_bytes_to_code()
if (state->header.rom_size != INVALID_SIZE_CODE) {
size_t decl_size = size_code_to_bytes(state->header.rom_size);
if (decl_size > state->rom_size)
state->rom_size = decl_size;
}
}


state->rom_size = 8;
if (state->header.rom_size == INVALID_SIZE_CODE)
state->header.rom_size = size_bytes_to_code(state->rom_size);


(void) state;
return NULL; return NULL;
} }




+ 1
- 1
src/assembler/parse_util.c View File

@@ -202,7 +202,7 @@ PARSE_FUNC_HEADER(size_code, uint8_t)
} }


uint8_t code = size_bytes_to_code(bytes); uint8_t code = size_bytes_to_code(bytes);
if (code)
if (code != INVALID_SIZE_CODE)
return (*result = code), true; return (*result = code), true;
return false; return false;
} }

+ 18
- 6
src/assembler/preprocessor.c View File

@@ -51,7 +51,7 @@
if (CALL_PARSER_(arg_type)) {true_part} else {false_part} if (CALL_PARSER_(arg_type)) {true_part} else {false_part}


#define SAVE_LINE(target) \ #define SAVE_LINE(target) \
target = line;
if (!dir_is_auto) target = line;


#define BEGIN_DIRECTIVE_BLOCK \ #define BEGIN_DIRECTIVE_BLOCK \
ssize_t first_ctr = -1; \ ssize_t first_ctr = -1; \
@@ -63,7 +63,8 @@
FAIL_ON_COND_(!DIRECTIVE_HAS_ARG(line, directive), ED_PP_NO_ARG) \ FAIL_ON_COND_(!DIRECTIVE_HAS_ARG(line, directive), ED_PP_NO_ARG) \
arg_type arg = 0; \ arg_type arg = 0; \
arg_type* dest = &(dest_loc); \ arg_type* dest = &(dest_loc); \
if (DIRECTIVE_IS_AUTO(line, directive)) { \
bool dir_is_auto = DIRECTIVE_IS_AUTO(line, directive); \
if (dir_is_auto) { \
arg = auto_val; \ arg = auto_val; \
} else { } else {


@@ -311,6 +312,14 @@ static ErrorInfo* build_asm_lines(
} }


/* /*
Return whether the given ROM size is valid.
*/
static inline bool is_rom_size_valid(size_t size)
{
return size_bytes_to_code(size) != INVALID_SIZE_CODE;
}

/*
Return whether the given header offset is a valid location. Return whether the given header offset is a valid location.
*/ */
static inline bool is_header_offset_valid(uint16_t offset) static inline bool is_header_offset_valid(uint16_t offset)
@@ -365,7 +374,7 @@ ErrorInfo* preprocess(AssemblerState *state, const LineBuffer *source)
PARSER_BRANCH(uint32_t, {}, { PARSER_BRANCH(uint32_t, {}, {
USE_PARSER(uint32_t, rom_size) USE_PARSER(uint32_t, rom_size)
}) })
VALIDATE(size_bytes_to_code)
VALIDATE(is_rom_size_valid)
SAVE_LINE(rom_size_line) SAVE_LINE(rom_size_line)
END_DIRECTIVE END_DIRECTIVE


@@ -397,7 +406,7 @@ ErrorInfo* preprocess(AssemblerState *state, const LineBuffer *source)
}) })
END_DIRECTIVE END_DIRECTIVE


BEGIN_DIRECTIVE(DIR_ROM_DECLSIZE, uint8_t, state->header.rom_size, 0)
BEGIN_DIRECTIVE(DIR_ROM_DECLSIZE, uint8_t, state->header.rom_size, DEFAULT_DECLSIZE)
PARSER_BRANCH(uint8_t, { PARSER_BRANCH(uint8_t, {
CHECK_RANGE(0x10) CHECK_RANGE(0x10)
VALIDATE(size_code_to_bytes) VALIDATE(size_code_to_bytes)
@@ -416,18 +425,21 @@ ErrorInfo* preprocess(AssemblerState *state, const LineBuffer *source)
line = prev; line = prev;
} }


if (state->rom_size && state->header.offset + HEADER_SIZE > state->rom_size) {
if (rom_size_line && state->header.offset + HEADER_SIZE > state->rom_size) {
ei = error_info_create(rom_size_line, ET_PREPROC, ED_PP_HEADER_RANGE); ei = error_info_create(rom_size_line, ET_PREPROC, ED_PP_HEADER_RANGE);
goto cleanup; goto cleanup;
} }


if (state->rom_size && state->header.rom_size &&
if (rom_size_line && rom_declsize_line &&
size_code_to_bytes(state->header.rom_size) > state->rom_size) { size_code_to_bytes(state->header.rom_size) > state->rom_size) {
ei = error_info_create(rom_size_line, ET_PREPROC, ED_PP_DECLARE_RANGE); ei = error_info_create(rom_size_line, ET_PREPROC, ED_PP_DECLARE_RANGE);
error_info_append(ei, rom_declsize_line); error_info_append(ei, rom_declsize_line);
goto cleanup; goto cleanup;
} }


if (!rom_declsize_line) // Mark as undefined, for resolve_defaults()
state->header.rom_size = INVALID_SIZE_CODE;

cleanup: cleanup:
asm_lines_free(condemned); asm_lines_free(condemned);
state->lines = dummy.next; // Fix list head if first line was a directive state->lines = dummy.next; // Fix list head if first line was a directive


+ 1
- 1
src/assembler/state.c View File

@@ -17,7 +17,7 @@ void state_init(AssemblerState *state)
state->header.product_code = 0; state->header.product_code = 0;
state->header.version = 0; state->header.version = 0;
state->header.region = DEFAULT_REGION; state->header.region = DEFAULT_REGION;
state->header.rom_size = 0;
state->header.rom_size = DEFAULT_DECLSIZE;
state->optimizer = false; state->optimizer = false;
state->rom_size = 0; state->rom_size = 0;




+ 1
- 0
src/assembler/state.h View File

@@ -11,6 +11,7 @@


#define DEFAULT_HEADER_OFFSET 0x7FF0 #define DEFAULT_HEADER_OFFSET 0x7FF0
#define DEFAULT_REGION 6 // GG Export #define DEFAULT_REGION 6 // GG Export
#define DEFAULT_DECLSIZE 0xC // 32 KB


#define SYMBOL_TABLE_BUCKETS 128 #define SYMBOL_TABLE_BUCKETS 128




+ 2
- 2
src/util.c View File

@@ -121,7 +121,7 @@ size_t size_code_to_bytes(uint8_t code)
/* /*
Given the number of bytes in a ROM image, return the size code. Given the number of bytes in a ROM image, return the size code.


0 is returned if the size is invalid.
INVALID_SIZE_CODE is returned if the size is invalid.
*/ */
uint8_t size_bytes_to_code(size_t bytes) uint8_t size_bytes_to_code(size_t bytes)
{ {
@@ -138,6 +138,6 @@ uint8_t size_bytes_to_code(size_t bytes)
case 256: return 0x0; case 256: return 0x0;
case 512: return 0x1; case 512: return 0x1;
case 1024: return 0x2; case 1024: return 0x2;
default: return 0;
default: return INVALID_SIZE_CODE;
} }
} }

+ 2
- 0
src/util.h View File

@@ -5,6 +5,8 @@


#include <stdint.h> #include <stdint.h>


#define INVALID_SIZE_CODE 0x8

/* Functions */ /* Functions */


uint8_t bcd_decode(uint8_t); uint8_t bcd_decode(uint8_t);


Loading…
Cancel
Save