got rid of most of the *_init() functions

git-svn-id: https://svn.code.sf.net/p/acme-crossass/code-0/trunk@291 4df02467-bbd4-4a76-a152-e7ce94205b78
This commit is contained in:
marcobaye 2020-08-16 17:39:13 +00:00
parent 2be25080aa
commit beb1e178cd
18 changed files with 97 additions and 119 deletions

View File

@ -32,7 +32,7 @@ encoding.o: config.h alu.h acme.h dynabuf.h global.h output.h input.h tree.h enc
flow.o: config.h acme.h alu.h dynabuf.h global.h input.h mnemo.h symbol.h tree.h flow.h flow.c
global.o: config.h platform.h acme.h cpu.h encoding.h input.h macro.h pseudoopcodes.h section.h symbol.h global.h global.c
global.o: config.h platform.h acme.h cpu.h dynabuf.h encoding.h input.h macro.h pseudoopcodes.h section.h symbol.h global.h global.c
input.o: config.h alu.h dynabuf.h global.h section.h symbol.h tree.h input.h input.c

View File

@ -32,7 +32,7 @@ encoding.o: config.h alu.h acme.h dynabuf.h global.h output.h input.h tree.h enc
flow.o: config.h acme.h alu.h dynabuf.h global.h input.h mnemo.h symbol.h tree.h flow.h flow.c
global.o: config.h platform.h acme.h cpu.h encoding.h input.h macro.h pseudoopcodes.h section.h symbol.h global.h global.c
global.o: config.h platform.h acme.h cpu.h dynabuf.h encoding.h input.h macro.h pseudoopcodes.h section.h symbol.h global.h global.c
input.o: config.h alu.h dynabuf.h global.h section.h symbol.h tree.h input.h input.c

View File

@ -35,7 +35,7 @@ encoding.o: config.h alu.h acme.h dynabuf.h global.h output.h input.h tree.h enc
flow.o: config.h acme.h alu.h dynabuf.h global.h input.h mnemo.h symbol.h tree.h flow.h flow.c
global.o: config.h platform.h acme.h cpu.h encoding.h input.h macro.h pseudoopcodes.h section.h symbol.h global.h global.c
global.o: config.h platform.h acme.h cpu.h dynabuf.h encoding.h input.h macro.h pseudoopcodes.h section.h symbol.h global.h global.c
input.o: config.h alu.h dynabuf.h global.h section.h symbol.h tree.h input.h input.c

View File

@ -30,7 +30,7 @@ encoding.o: config.h alu.h acme.h dynabuf.h global.h output.h input.h tree.h enc
flow.o: config.h acme.h alu.h dynabuf.h global.h input.h mnemo.h symbol.h tree.h flow.h flow.c
global.o: config.h platform.h acme.h cpu.h encoding.h input.h macro.h pseudoopcodes.h section.h symbol.h global.h global.c
global.o: config.h platform.h acme.h cpu.h dynabuf.h encoding.h input.h macro.h pseudoopcodes.h section.h symbol.h global.h global.c
input.o: config.h alu.h dynabuf.h global.h section.h symbol.h tree.h input.h input.c

View File

@ -639,24 +639,15 @@ int main(int argc, const char *argv[])
if (argc == 1)
show_help_and_exit();
cliargs_init(argc, argv);
DynaBuf_init(); // inits *global* dynamic buffer - important, so first
// Init platform-specific stuff.
// For example, this could read the library path from an
// environment variable, which in turn may need DynaBuf already.
// init platform-specific stuff.
// this may read the library path from an environment variable.
PLATFORM_INIT;
// prepare a buffer large enough to hold pointers to "-D" switch values
// cli_defines = safe_malloc(argc * sizeof(*cli_defines));
includepaths_init(); // must be done before cli arg handling
// handle command line arguments
cliargs_handle_options(short_option, long_option);
// generate list of files to process
cliargs_get_rest(&toplevel_src_count, &toplevel_sources, "No top level sources given");
// Init modules (most of them will just build keyword trees)
ALU_init();
Macro_init();
Mnemo_init();
// init output buffer
Output_init(fill_value, config.test_new_features);
pseudoopcodes_init(); // setup keyword tree for pseudo opcodes
if (do_actual_work())
save_output_file();
return ACME_finalize(EXIT_SUCCESS); // dump labels, if wanted

View File

@ -35,8 +35,8 @@
// constants
#define ERRORMSG_DYNABUF_INITIALSIZE 256 // ad hoc
#define FUNCTION_DYNABUF_INITIALSIZE 8 // enough for "arctan"
#define ERRORMSG_INITIALSIZE 256 // ad hoc
#define FUNCTION_INITIALSIZE 8 // enough for "arctan"
#define HALF_INITIAL_STACK_SIZE 8
static const char exception_div_by_zero[] = "Division by zero.";
static const char exception_no_value[] = "No value given.";
@ -167,8 +167,8 @@ static struct op ops_isstring = {42, OPGROUP_MONADIC, OPID_ISSTRING, "is_string
// variables
static struct dynabuf *errormsg_dyna_buf; // dynamic buffer to build variable-length error messages
static struct dynabuf *function_dyna_buf; // dynamic buffer for fn names
static STRUCT_DYNABUF_REF(errormsg_dyna_buf, ERRORMSG_INITIALSIZE); // to build variable-length error messages
static STRUCT_DYNABUF_REF(function_dyna_buf, FUNCTION_INITIALSIZE); // for fn names
// operator stack, current size and stack pointer:
static struct op **op_stack = NULL;
static int opstack_size = HALF_INITIAL_STACK_SIZE;
@ -248,6 +248,7 @@ do { \
static void enlarge_operator_stack(void)
{
opstack_size *= 2;
//printf("Doubling op stack size to %d.\n", opstack_size);
op_stack = realloc(op_stack, opstack_size * sizeof(*op_stack));
if (op_stack == NULL)
Throw_serious_error(exception_no_memory_left);
@ -258,22 +259,13 @@ static void enlarge_operator_stack(void)
static void enlarge_argument_stack(void)
{
argstack_size *= 2;
//printf("Doubling arg stack size to %d.\n", argstack_size);
arg_stack = realloc(arg_stack, argstack_size * sizeof(*arg_stack));
if (arg_stack == NULL)
Throw_serious_error(exception_no_memory_left);
}
// create dynamic buffer, operator/function trees and operator/argument stacks
void ALU_init(void)
{
errormsg_dyna_buf = DynaBuf_create(ERRORMSG_DYNABUF_INITIALSIZE);
function_dyna_buf = DynaBuf_create(FUNCTION_DYNABUF_INITIALSIZE);
enlarge_operator_stack();
enlarge_argument_stack();
}
// not-so-braindead algorithm for calculating "to the power of" function for
// integer arguments.
// my_pow(whatever, 0) returns 1.
@ -2376,6 +2368,12 @@ static int parse_expression(struct expression *expression)
{
struct object *result = &expression->result;
// make sure stacks are ready (if not yet initialised, do it now)
if (arg_stack == NULL)
enlarge_argument_stack();
if (op_stack == NULL)
enlarge_operator_stack();
// init
expression->is_empty = TRUE; // becomes FALSE when first valid char gets parsed
expression->open_parentheses = 0;
@ -2388,9 +2386,10 @@ static int parse_expression(struct expression *expression)
PUSH_OP(&ops_start_expression);
alu_state = STATE_EXPECT_ARG_OR_MONADIC_OP;
do {
// check stack sizes. enlarge if needed
// check arg stack size. enlarge if needed
if (arg_sp >= argstack_size)
enlarge_argument_stack();
// (op stack size is checked whenever pushing an operator)
switch (alu_state) {
case STATE_EXPECT_ARG_OR_MONADIC_OP:
if (expect_argument_or_monadic_operator(expression))

View File

@ -50,19 +50,16 @@ struct expression {
// labels that are undefined, we can't simply get the addressing mode
// from looking at the parameter's value. FIXME - rename to TAINTED :)
// create dynamic buffer, operator/function trees and operator/operand stacks
extern void ALU_init(void);
/*
// FIXME - replace all the functions below with a single one using a "flags" arg!
// its return value would then be "error"/"ok".
// input flags:
#define ACCEPT_UNDEFINED (1u << 0) // if not given, undefined throws serious error
#define ACCEPT_INT (1u << 1) needed when strings come along!
#define ACCEPT_INT (1u << 1)
#define ACCEPT_FLOAT (1u << 2) // if not given, floats are converted to integer
#define ACCEPT_OPENPARENTHESIS (1u << 3) // if not given, throws syntax error
//#define ACCEPT_STRING
#define ACCEPT_STRING (1u << 4) // if not given, convert 1-char strings to int?
#define ACCEPT_LIST (1u << 5)
// do I need ACCEPT_NONADDR and/or ACCEPT_ADDRESS?
*/

View File

@ -21,10 +21,12 @@
// initial size for global dynabuf
// (as it holds macros, loop bodies, etc., make it large to begin with)
#define GLOBALDYNABUF_INITIALSIZE 1024 // should be >0 (see above)
// TODO - get rid of this, or move to global.c
// Variables
struct dynabuf *GlobalDynaBuf; // global dynamic buffer
STRUCT_DYNABUF_REF(GlobalDynaBuf, GLOBALDYNABUF_INITIALSIZE); // global dynamic buffer
// TODO - get rid of this, or move to global.c
// Functions
@ -34,37 +36,42 @@ static void resize(struct dynabuf *db, size_t new_size)
{
char *new_buf;
//printf("Growing dynabuf to size %d.\n", new_size);
new_buf = realloc(db->buffer, new_size);
if (new_buf == NULL)
Throw_serious_error(exception_no_memory_left);
db->reserved = new_size;
db->buffer = new_buf;
}
// get buffer mem and fill in struct
static void initstruct(struct dynabuf *db, size_t initial_size)
{
//printf("dynabuf-init: %d.\n", initial_size);
if (initial_size < DYNABUF_MINIMUM_INITIALSIZE)
initial_size = DYNABUF_MINIMUM_INITIALSIZE;
db->size = 0;
db->reserved = initial_size;
db->buffer = malloc(initial_size);
if (db->buffer == NULL) {
// scream and die because there is not enough memory
fputs("Error: No memory for dynamic buffer.\n", stderr);
exit(EXIT_FAILURE);
}
}
// Exported functions
// Create and init a dynamic buffer and return pointer
struct dynabuf *DynaBuf_create(int initial_size)
// (ensure buffer is ready to use, then) clear dynamic buffer
void dynabuf_clear(struct dynabuf *db)
{
struct dynabuf *db;
if (initial_size < DYNABUF_MINIMUM_INITIALSIZE)
initial_size = DYNABUF_MINIMUM_INITIALSIZE;
if ((db = malloc(sizeof(*db)))) {
db->size = 0;
db->reserved = initial_size;
db->buffer = malloc(initial_size);
if (db->buffer)
return db; // if both pointers are != NULL, no error
}
// otherwise, complain
fputs("Error: No memory for dynamic buffer.\n", stderr);
exit(EXIT_FAILURE);
if (db->buffer == NULL)
initstruct(db, db->reserved); // get initial buffer
db->size = 0; // clear buffer
}
// Enlarge buffer
void DynaBuf_enlarge(struct dynabuf *db)
void dynabuf_enlarge(struct dynabuf *db)
{
resize(db, MAKE_LARGER_THAN(db->reserved));
}
@ -112,6 +119,11 @@ void DynaBuf_to_lower(struct dynabuf *target, struct dynabuf *source)
*write,
byte;
// if target has not been initialised yet, do it now
// (do not clear it unconditionally, because it may equal source!)
if (target->buffer == NULL)
initstruct(target, target->reserved); // get initial buffer
// make sure target can take it
if (source->size > target->reserved)
resize(target, source->size);
@ -131,9 +143,3 @@ void DynaBuf_to_lower(struct dynabuf *target, struct dynabuf *source)
// FIXME - use BYTE_ macro from global.h
*write = '\0'; // terminate
}
// Initialisation - allocate global dynamic buffer
void DynaBuf_init(void)
{
GlobalDynaBuf = DynaBuf_create(GLOBALDYNABUF_INITIALSIZE);
}

View File

@ -1,5 +1,5 @@
// ACME - a crossassembler for producing 6502/65c02/65816/65ce02 code.
// Copyright (C) 1998-2016 Marco Baye
// Copyright (C) 1998-2020 Marco Baye
// Have a look at "acme.c" for further info
//
// Dynamic buffer stuff
@ -8,14 +8,14 @@
#include "config.h"
#include <stdlib.h> // for size_t
// macros
#define DYNABUF_CLEAR(db) do {db->size = 0;} while (0)
#define DYNABUF_APPEND(db, byte) \
do { \
if (db->size == db->reserved) \
DynaBuf_enlarge(db); \
dynabuf_enlarge(db); \
db->buffer[(db->size)++] = byte;\
} while (0)
// the next one is dangerous - the buffer location can change when a character
@ -27,21 +27,28 @@ do { \
// dynamic buffer structure
struct dynabuf {
char *buffer; // pointer to buffer
int size; // size of buffer's used portion
int reserved; // total size of buffer
size_t size; // size of buffer's used portion
size_t reserved; // total size of buffer
};
// new way of declaration/definition:
// the small struct above is static, only the buffer itself gets malloc'd (on
// first "clear").
#define STRUCT_DYNABUF_REF(name, size) struct dynabuf name[1] = {{NULL, 0, size}}
// the "[1]" makes sure the name refers to the address and not the struct
// itself, so existing code where the name referred to a pointer does not need
// to be changed.
// variables
extern struct dynabuf *GlobalDynaBuf; // global dynamic buffer
extern struct dynabuf GlobalDynaBuf[1]; // global dynamic buffer
// TODO - get rid of this, or move to global.c
// create global DynaBuf (call once on program startup)
extern void DynaBuf_init(void);
// create (private) DynaBuf
extern struct dynabuf *DynaBuf_create(int initial_size);
// (ensure buffer is ready to use, then) clear dynamic buffer
#define DYNABUF_CLEAR(db) dynabuf_clear(db) // TODO - remove old macro
extern void dynabuf_clear(struct dynabuf *db);
// call whenever buffer is too small
extern void DynaBuf_enlarge(struct dynabuf *db);
extern void dynabuf_enlarge(struct dynabuf *db);
// return malloc'd copy of buffer contents
extern char *DynaBuf_get_copy(struct dynabuf *db);
// copy string to buffer (without terminator)

View File

@ -686,23 +686,25 @@ struct ipi {
*prev;
const char *path;
};
static struct ipi ipi_head; // head element
static struct dynabuf *pathbuf; // buffer to combine search path and file spec
// TODO - just use &ipi_head as init values!
static struct ipi ipi_head = {NULL, NULL, NULL}; // head element
static STRUCT_DYNABUF_REF(pathbuf, 256); // to combine search path and file spec
// init list
void includepaths_init(void)
// make sure list is ready
static void check_includepaths_list(void)
{
// init ring list
ipi_head.next = &ipi_head;
ipi_head.prev = &ipi_head;
// init dynabuf
pathbuf = DynaBuf_create(256);
if (ipi_head.next == NULL) {
// init ring list
ipi_head.next = &ipi_head;
ipi_head.prev = &ipi_head;
}
}
// add entry
void includepaths_add(const char *path)
{
struct ipi *ipi;
check_includepaths_list();
ipi = safe_malloc(sizeof(*ipi));
ipi->path = path;
ipi->next = &ipi_head;
@ -718,6 +720,7 @@ FILE *includepaths_open_ro(boolean uses_lib)
FILE *stream;
struct ipi *ipi;
check_includepaths_list();
// first try directly, regardless of whether lib or not:
stream = fopen(GLOBALDYNABUF_CURRENT, FILE_READBINARY);
// if failed and not lib, try include paths:

View File

@ -126,8 +126,6 @@ extern bits Input_get_force_bit(void);
// include path stuff - should be moved to its own file:
// init list
extern void includepaths_init(void);
// add entry
extern void includepaths_add(const char *path);
// open file for reading (trying list entries as prefixes)

View File

@ -18,7 +18,7 @@
// Constants
#define MACRONAME_DYNABUF_INITIALSIZE 128
#define NAME_INITIALSIZE 128
#define ARG_SEPARATOR ' ' // separates macro title from arg types
#define ARGTYPE_VALUE 'v'
#define ARGTYPE_REF 'r'
@ -46,8 +46,8 @@ union macro_arg_t {
// Variables
static struct dynabuf *user_macro_name; // original macro title
static struct dynabuf *internal_name; // plus param type chars
static STRUCT_DYNABUF_REF(user_macro_name, NAME_INITIALSIZE); // original macro title
static STRUCT_DYNABUF_REF(internal_name, NAME_INITIALSIZE); // plus param type chars
static struct rwnode *macro_forest[256]; // trees (because of 8b hash)
// Dynamic argument table
static union macro_arg_t *arg_table = NULL;
@ -60,19 +60,12 @@ static int argtable_size = HALF_INITIAL_ARG_TABLE_SIZE;
static void enlarge_arg_table(void)
{
argtable_size *= 2;
//printf("Doubling arg table size to %d.\n", argtable_size);
arg_table = realloc(arg_table, argtable_size * sizeof(*arg_table));
if (arg_table == NULL)
Throw_serious_error(exception_no_memory_left);
}
// create dynamic buffers and arg table
void Macro_init(void)
{
user_macro_name = DynaBuf_create(MACRONAME_DYNABUF_INITIALSIZE);
internal_name = DynaBuf_create(MACRONAME_DYNABUF_INITIALSIZE);
enlarge_arg_table();
}
// Read macro scope and title. Title is read to GlobalDynaBuf and then copied
// over to internal_name DynaBuf, where ARG_SEPARATOR is added.
// In user_macro_name DynaBuf, the original name is reconstructed (even with
@ -241,6 +234,10 @@ void Macro_parse_call(void) // Now GotByte = dot or first char of macro name
int arg_count = 0;
int outer_err_count;
// make sure arg_table is ready (if not yet initialised, do it now)
if (arg_table == NULL)
enlarge_arg_table();
// Enter deeper nesting level
// Quit program if recursion too deep.
if (--macro_recursions_left < 0)

View File

@ -12,8 +12,6 @@
// Prototypes
// create dynamic buffers and arg table
extern void Macro_init(void); // create private dynabuf
// only call once (during first pass)
extern void Macro_parse_definition(void);
// Parse macro call ("+MACROTITLE"). Has to be re-entrant.

View File

@ -16,7 +16,7 @@
// constants
#define MNEMO_DYNABUF_INITIALSIZE 8 // 4 + terminator should suffice
#define MNEMO_INITIALSIZE 8 // 4 + terminator should suffice
// These values are needed to recognize addressing modes:
// indexing:
@ -147,7 +147,7 @@ static const char exception_oversized_addrmode[] = "Using oversized addressing m
// Variables
static struct dynabuf *mnemo_dyna_buf; // dynamic buffer for mnemonics
static STRUCT_DYNABUF_REF(mnemo_dyna_buf, MNEMO_INITIALSIZE); // for mnemonics
// mnemonic's code, flags and group values are stored together in a single integer.
// ("code" is either a table index or the opcode itself, depending on group value)
@ -530,13 +530,6 @@ static struct ronode mnemo_m65_tree[] = {
// Functions
// create dynamic buffer, build keyword trees
void Mnemo_init(void)
{
mnemo_dyna_buf = DynaBuf_create(MNEMO_DYNABUF_INITIALSIZE);
}
// Address mode parsing
// utility function for parsing indices. result must be processed via AMB_PREINDEX() or AMB_INDEX() macro!

View File

@ -10,8 +10,6 @@
#include "config.h"
// create dynamic buffer, build keyword trees
extern void Mnemo_init(void);
// check whether mnemonic in GlobalDynaBuf is supported by standard 6502 cpu.
extern boolean keyword_is_6502_mnemo(int length);
// check whether mnemonic in GlobalDynaBuf is supported by NMOS 6502 cpu (includes undocumented opcodes).

View File

@ -1204,11 +1204,11 @@ static enum eos po_watch(void)
*/
// constants
#define USERMSG_DYNABUF_INITIALSIZE 80
#define USERMSG_INITIALSIZE 80
// variables
static struct dynabuf *user_message; // dynamic buffer (!warn/error/serious)
static STRUCT_DYNABUF_REF(user_message, USERMSG_INITIALSIZE); // for !warn/error/serious
// helper function to show user-defined messages
@ -1367,13 +1367,6 @@ static struct ronode pseudo_opcode_tree[] = {
};
// register pseudo opcodes and create dynamic buffer
void pseudoopcodes_init(void)
{
user_message = DynaBuf_create(USERMSG_DYNABUF_INITIALSIZE);
}
// parse a pseudo opcode. has to be re-entrant.
void pseudoopcode_parse(void) // now GotByte = "!"
{

View File

@ -9,8 +9,6 @@
// call when "*= EXPRESSION" is parsed
extern void notreallypo_setpc(void);
// register pseudo opcodes
extern void pseudoopcodes_init(void);
// parse pseudo opcode. has to be re-entrant.
extern void pseudoopcode_parse(void);

View File

@ -9,7 +9,7 @@
#define RELEASE "0.97" // update before release FIXME
#define CODENAME "Zem" // update before release
#define CHANGE_DATE "15 Aug" // update before release FIXME
#define CHANGE_DATE "16 Aug" // update before release FIXME
#define CHANGE_YEAR "2020" // update before release
//#define HOME_PAGE "http://home.pages.de/~mac_bacon/smorbrod/acme/"
#define HOME_PAGE "http://sourceforge.net/p/acme-crossass/" // FIXME