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