Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdint.h> |
| 4 | #include <string.h> |
| 5 | #include <assert.h> |
| 6 | |
| 7 | #include "misc.h" |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame^] | 8 | #include "mpconfig.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 9 | #include "lexer.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 10 | #include "parse.h" |
| 11 | #include "scope.h" |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame^] | 12 | #include "runtime0.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 13 | #include "emit.h" |
| 14 | |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 15 | #define EMIT(fun, arg...) (emit_method_table->fun(emit, ##arg)) |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 16 | |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 17 | void emit_common_load_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr) { |
| 18 | // assumes pass is greater than 1, ie that all identifiers are defined in the scope |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 19 | |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 20 | id_info_t *id = scope_find(scope, qstr); |
| 21 | assert(id != NULL); // TODO can this ever fail? |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 22 | |
| 23 | // call the emit backend with the correct code |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 24 | if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 25 | EMIT(load_name, qstr); |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 26 | } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 27 | EMIT(load_global, qstr); |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 28 | } else if (id->kind == ID_INFO_KIND_LOCAL) { |
| 29 | EMIT(load_fast, qstr, id->local_num); |
| 30 | } else if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) { |
Damien | 27fb45e | 2013-10-20 15:07:49 +0100 | [diff] [blame] | 31 | EMIT(load_deref, qstr, id->local_num); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 32 | } else { |
| 33 | assert(0); |
| 34 | } |
| 35 | } |
| 36 | |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 37 | void emit_common_store_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr) { |
| 38 | // assumes pass is greater than 1, ie that all identifiers are defined in the scope |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 39 | |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 40 | id_info_t *id = scope_find(scope, qstr); |
| 41 | assert(id != NULL); // TODO can this ever fail? |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 42 | |
| 43 | // call the emit backend with the correct code |
| 44 | if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) { |
| 45 | EMIT(store_name, qstr); |
| 46 | } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) { |
| 47 | EMIT(store_global, qstr); |
| 48 | } else if (id->kind == ID_INFO_KIND_LOCAL) { |
| 49 | EMIT(store_fast, qstr, id->local_num); |
| 50 | } else if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) { |
Damien | 27fb45e | 2013-10-20 15:07:49 +0100 | [diff] [blame] | 51 | EMIT(store_deref, qstr, id->local_num); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 52 | } else { |
| 53 | assert(0); |
| 54 | } |
| 55 | } |
| 56 | |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 57 | void emit_common_delete_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr) { |
| 58 | // assumes pass is greater than 1, ie that all identifiers are defined in the scope |
| 59 | |
| 60 | id_info_t *id = scope_find(scope, qstr); |
| 61 | assert(id != NULL); // TODO can this ever fail? |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 62 | |
| 63 | // call the emit backend with the correct code |
| 64 | if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) { |
| 65 | EMIT(delete_name, qstr); |
| 66 | } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) { |
| 67 | EMIT(delete_global, qstr); |
| 68 | } else if (id->kind == ID_INFO_KIND_LOCAL) { |
| 69 | EMIT(delete_fast, qstr, id->local_num); |
| 70 | } else if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) { |
Damien | 27fb45e | 2013-10-20 15:07:49 +0100 | [diff] [blame] | 71 | EMIT(delete_deref, qstr, id->local_num); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 72 | } else { |
| 73 | assert(0); |
| 74 | } |
| 75 | } |