2012-04-17 08:29:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012
|
2012-04-17 20:24:22 +00:00
|
|
|
* Dale Weiler
|
2012-04-17 08:29:58 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
|
|
* of the Software, and to permit persons to whom the Software is furnished to do
|
|
|
|
* so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "gmqcc.h"
|
|
|
|
/*
|
|
|
|
* Some assembler keywords not part of the opcodes above: these are
|
|
|
|
* for creating functions, or constants.
|
|
|
|
*/
|
|
|
|
const char *const asm_keys[] = {
|
2012-04-17 20:24:22 +00:00
|
|
|
"FLOAT" , /* define float */
|
|
|
|
"VECTOR" , /* define vector */
|
|
|
|
"ENTITY" , /* define ent */
|
|
|
|
"FIELD" , /* define field */
|
|
|
|
"STRING" , /* define string */
|
|
|
|
"FUNCTION"
|
2012-04-17 08:29:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static char *const asm_getline(size_t *byte, FILE *fp) {
|
2012-04-17 20:24:22 +00:00
|
|
|
char *line = NULL;
|
|
|
|
ssize_t read = util_getline(&line, byte, fp);
|
|
|
|
*byte = read;
|
|
|
|
if (read == -1) {
|
|
|
|
mem_d (line);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return line;
|
2012-04-17 08:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void asm_init(const char *file, FILE **fp) {
|
2012-04-17 20:24:22 +00:00
|
|
|
*fp = fopen(file, "r");
|
|
|
|
code_init();
|
2012-04-17 08:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void asm_close(FILE *fp) {
|
2012-04-17 20:24:22 +00:00
|
|
|
fclose(fp);
|
|
|
|
code_write();
|
2012-04-17 08:29:58 +00:00
|
|
|
}
|
2012-04-17 20:14:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Following parse states:
|
2012-04-17 20:24:22 +00:00
|
|
|
* ASM_FUNCTION -- in a function accepting input statements
|
|
|
|
* ....
|
2012-04-17 20:14:00 +00:00
|
|
|
*/
|
|
|
|
typedef enum {
|
2012-04-17 20:24:22 +00:00
|
|
|
ASM_NULL,
|
|
|
|
ASM_FUNCTION
|
2012-04-17 20:14:00 +00:00
|
|
|
} asm_state;
|
|
|
|
|
|
|
|
typedef struct {
|
2012-04-17 20:24:22 +00:00
|
|
|
char *name; /* name of constant */
|
|
|
|
int offset; /* location in globals */
|
2012-04-17 20:14:00 +00:00
|
|
|
} globals;
|
|
|
|
VECTOR_MAKE(globals, assembly_constants);
|
2012-04-17 21:16:11 +00:00
|
|
|
|
|
|
|
void asm_clear() {
|
2012-04-19 22:03:30 +00:00
|
|
|
size_t i = 0;
|
|
|
|
for (; i < assembly_constants_elements; i++)
|
|
|
|
mem_d(assembly_constants_data[i].name);
|
|
|
|
mem_d(assembly_constants_data);
|
2012-04-17 21:16:11 +00:00
|
|
|
}
|
2012-04-19 20:41:03 +00:00
|
|
|
|
2012-04-17 08:29:58 +00:00
|
|
|
void asm_parse(FILE *fp) {
|
2012-04-17 20:24:22 +00:00
|
|
|
char *data = NULL;
|
|
|
|
long line = 1; /* current line */
|
|
|
|
size_t size = 0; /* size of line */
|
|
|
|
asm_state state = ASM_NULL;
|
|
|
|
|
2012-04-27 20:31:38 +00:00
|
|
|
while ((data = asm_getline(&size, fp)) != NULL) {
|
|
|
|
data = util_strsws(data); /* skip whitespace */
|
|
|
|
data = util_strrnl(data); /* delete newline */
|
2012-04-20 04:44:44 +00:00
|
|
|
|
2012-04-27 20:31:38 +00:00
|
|
|
/* TODO: everything */
|
|
|
|
(void)state;
|
|
|
|
goto end;
|
|
|
|
end:
|
2012-04-17 20:24:22 +00:00
|
|
|
mem_d(data);
|
|
|
|
line ++;
|
|
|
|
}
|
2012-04-19 20:41:03 +00:00
|
|
|
asm_clear();
|
2012-04-17 08:29:58 +00:00
|
|
|
}
|