T.I.M | Toolchain Documentation
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
asm.h
Go to the documentation of this file.
1 
9 #include "stdio.h"
10 #include "stdlib.h"
11 #include "string.h"
12 #include "assert.h"
13 
14 #include "common.h"
15 
16 #include "asm_lex.h"
17 
18 #ifndef ASM_H
19 #define ASM_H
20 
21 #ifdef TIM_PRINT_PROMPT
22  #undef TIM_PRINT_PROMPT
23 #endif
24 #define TIM_PRINT_PROMPT "\e[1;36masm>\e[0m "
25 
27 typedef struct asm_args_reg_single_t{
30 
32 typedef struct asm_args_reg_double_t{
36 
38 typedef struct asm_args_reg_tripple_t{
43 
45 typedef struct asm_args_reg_double_imm_t{
50 
52 typedef struct asm_args_reg_single_imm{
56 
58 typedef struct asm_args_single_imm{
61 
64  char * label;
66 
68 typedef union asm_opcode_args_u{
77 
84 typedef struct asm_statement_t asm_statement;
86 {
89 
92 
95 
98 
101 
103  unsigned int address;
104 
106  unsigned int line_number;
107 
109  asm_statement * next;
111  asm_statement * prev;
112 };
113 
114 
116 typedef int asm_hash_key;
117 
119 typedef struct asm_hash_table_bin_t asm_hash_table_bin;
120 
122 typedef enum asm_format_e {BINARY, ASCII} asm_format;
123 
124 
129 {
130  char used;
132  char * key;
134  void * data;
136  asm_hash_table_bin * next;
137 } ;
138 
142 typedef struct asm_hash_table_t
143 {
146 
149 
151  asm_hash_table_bin * buckets;
152 
154 
158 typedef struct asm_context_t
159 {
161  char * input_file;
163  char * output_file;
164 
167 
169  FILE * source;
171  FILE * binary;
172 
174  asm_statement * statements;
176  asm_lex_token * token_stream;
177 
181 
182 } asm_context;
183 
184 
193 int asm_emit_instructions(asm_statement * statements, FILE * file, asm_format format);
194 
201 int asm_calculate_addresses(asm_statement * statements, unsigned int base_address, asm_hash_table * labels);
202 
203 
215 asm_statement * asm_parse_token_stream(asm_lex_token * tokens, asm_hash_table * labels, int * errors);
216 
217 
226 int asm_hash_table_insert(asm_hash_table * table, char * key, void * data);
227 
233 void asm_hash_table_new(int initial_size, asm_hash_table * tr);
234 
242 void * asm_hash_table_get(asm_hash_table * table, char * strkey);
243 
244 #endif
245 
246 
Register arguments structure for opcodes with one register argument and an immediate.
Definition: asm.h:52
Union types for all different types of argument/operand combinations used.
Definition: asm.h:68
void asm_hash_table_new(int initial_size, asm_hash_table *tr)
Creates and returns a new pointer to a hash table.
Definition: asm_hash_table.c:15
int asm_calculate_addresses(asm_statement *statements, unsigned int base_address, asm_hash_table *labels)
Assigns addresses to each statement so that jumps and calls can be calculated.
Definition: asm_control_flow.c:16
asm_statement * next
The next statement to be executed in the program.
Definition: asm.h:109
tim_condition condition
The conditional execution code for this statement.
Definition: asm.h:94
tim_register reg_1
Definition: asm.h:28
tim_immediate immediate
Definition: asm.h:48
int tim_immediate
typedef for tim immediate values as operands to instructions.
Definition: common.h:76
tim_condition
A condition code for conditional execution.
Definition: common.h:187
asm_args_reg_single_imm reg_immediate
Definition: asm.h:73
tim_immediate immediate
Definition: asm.h:54
char * output_file
The path of the output binary file.
Definition: asm.h:163
unsigned int address
The address of the instruction in byte-aligned memory.
Definition: asm.h:103
unsigned int line_number
The line number of the source file the instruction came from.
Definition: asm.h:106
asm_opcode_args args
Arguments to the instruction.
Definition: asm.h:97
tim_instruction_opcode
Encodes all of the valid instruction opcodes and their bit values.
Definition: common.h:120
tim_instruction_size size
The size in bytes of this instruction.
Definition: asm.h:91
tim_register reg_1
Definition: asm.h:33
asm_args_reg_double reg_reg
Definition: asm.h:70
int current_size
The current size of the hash tables internal data structure.
Definition: asm.h:148
tim_register
Encodes the address of the registers.
Definition: common.h:79
char * label
Definition: asm.h:64
Register arguments structure for opcodes with only a single register argument.
Definition: asm.h:27
int element_count
A running count of the number of elements in the hashtable.
Definition: asm.h:145
asm_statement * asm_parse_token_stream(asm_lex_token *tokens, asm_hash_table *labels, int *errors)
Top function to trigger the parsing of an input source file.
Definition: asm_parse.c:518
Contains all information for the program in a format that can be easily passed around.
Definition: asm.h:158
Register arguments structure for opcodes with two register arguments.
Definition: asm.h:32
unsigned char tim_instruction_size
Encodes the size in bytes of an instruction in memory.
Definition: common.h:117
Register arguments structure for opcodes with three register arguments.
Definition: asm.h:38
Keeps a key,value pairing of elements. It's a hash table folks.
Definition: asm.h:142
Stores all information on a single ASM instruction.
Definition: asm.h:85
asm_args_single_imm immediate
Definition: asm.h:74
char * key
The key of the data.
Definition: asm.h:132
Definition: asm.h:122
int asm_hash_key
Typedef masking an integer to be the asm hash table key type.
Definition: asm.h:116
char * input_file
The path of the input source file.
Definition: asm.h:161
tim_register reg_1
Definition: asm.h:53
BOOL label_to_resolve
set to true IFF the statement needs an immediate resolving.
Definition: asm.h:100
Header file for data types and functions used by the asm code lexer.
tim_register reg_2
Definition: asm.h:47
tim_register reg_3
Definition: asm.h:41
void * asm_hash_table_get(asm_hash_table *table, char *strkey)
Returns a pointer to the data stored in a hash table with the given key or NULL if no such element ex...
Definition: asm_hash_table.c:100
Definition: asm.h:122
Register arguments structure for opcodes with two register arguments and an immediate.
Definition: asm.h:45
asm_args_reg_double_imm reg_reg_immediate
Definition: asm.h:72
asm_args_reg_single reg
Definition: asm.h:69
asm_hash_table_bin * buckets
Holds the internal data.
Definition: asm.h:151
int asm_hash_table_insert(asm_hash_table *table, char *key, void *data)
Inserts an element into the hash table associated with the provided key.
Definition: asm_hash_table.c:66
asm_hash_table * symbol_table
Definition: asm.h:180
FILE * source
The opened source file stream.
Definition: asm.h:169
char used
Definition: asm.h:130
Register arguments structure for opcodes with one immediate who's value is a label.
Definition: asm.h:63
asm_args_reg_tripple reg_reg_reg
Definition: asm.h:71
tim_register reg_2
Definition: asm.h:40
unsigned char BOOL
Boolean type.
Definition: common.h:70
asm_lex_token * token_stream
The tokens stream parsed from the raw file.
Definition: asm.h:176
asm_statement * prev
The previously executed statement in the program.
Definition: asm.h:111
tim_immediate immediate
Definition: asm.h:59
Header file for all common code shared across the tim toolchain.
asm_format
Describes whether to output the parsed asm code as binary or ascii code.
Definition: asm.h:122
tim_instruction_opcode opcode
The opcode of the instruction.
Definition: asm.h:88
tim_register reg_1
Definition: asm.h:39
asm_args_single_imm_label immediate_label
Definition: asm.h:75
void * data
Pointer to the data item inserted.
Definition: asm.h:134
asm_hash_table_bin * next
Pointer to the next data item in a linked list.
Definition: asm.h:136
asm_format format
How should we output to the binary file? ASCII or bytes?
Definition: asm.h:166
tim_register reg_1
Definition: asm.h:46
asm_statement * statements
The asm program in linked list form.
Definition: asm.h:174
int asm_emit_instructions(asm_statement *statements, FILE *file, asm_format format)
Responsible for writing all statements to the supplied file.
Definition: asm_emit.c:628
FILE * binary
THe opened output file stream.
Definition: asm.h:171
Register arguments structure for opcodes with one immediate.
Definition: asm.h:58
tim_register reg_2
Definition: asm.h:34
Contains a single bin in the hashtable.
Definition: asm.h:128