stage1 支持聚合类型方法
This commit is contained in:
1 parent
9bdb7f892e
commit
5073c2b3ea
10 files changed
+387
-148
No files matched your search
@@ -1,6 +1,8 @@
|
||||
#include "spl_ir2vm.h"
|
||||
#include "spl_layout.h"
|
||||
|
||||
static usize align_up(usize x, usize a);
|
||||
|
||||
static usize ir_type_size(spl_ir2vm_t *ctx, spl_type_id_t tid) {
|
||||
spl_type_node_t *tn = spl_type_node(ctx->type, tid);
|
||||
if (!tn)
|
||||
@@ -20,6 +22,16 @@ static usize ir_type_size(spl_ir2vm_t *ctx, spl_type_id_t tid) {
|
||||
return sizeof(void *);
|
||||
case SPL_TYPE_ARRAY:
|
||||
return ir_type_size(ctx, tn->array_type.element) * tn->array_type.len;
|
||||
case SPL_TYPE_ENUM: {
|
||||
usize tag_sz = ir_type_size(ctx, tn->agg_type.tag_type);
|
||||
usize max_payload = 0;
|
||||
for (usize i = 0; i < vec_size(tn->agg_type.variants); ++i) {
|
||||
usize vs = ir_type_size(ctx, vec_at(tn->agg_type.variants, i).type_id);
|
||||
if (vs > max_payload)
|
||||
max_payload = vs;
|
||||
}
|
||||
return align_up(tag_sz, 8) + max_payload;
|
||||
}
|
||||
case SPL_TYPE_STRUCT:
|
||||
case SPL_TYPE_UNION: {
|
||||
usize s = 0;
|
||||
@@ -44,6 +56,14 @@ static spl_type_id_t ir_agg_field_tid(spl_ir2vm_t *ctx, spl_type_id_t tid, usize
|
||||
spl_type_node_t *tn = spl_type_node(ctx->type, tid);
|
||||
if (!tn)
|
||||
return 0;
|
||||
if (tn->kind == SPL_TYPE_ENUM) {
|
||||
if (idx == SPL_ENUM_TAG_FIELD_IDX)
|
||||
return tn->agg_type.tag_type;
|
||||
usize vidx = idx - 1;
|
||||
if (vidx < vec_size(tn->agg_type.variants))
|
||||
return vec_at(tn->agg_type.variants, vidx).type_id;
|
||||
return 0;
|
||||
}
|
||||
if (tn->kind == SPL_TYPE_STRUCT || tn->kind == SPL_TYPE_UNION) {
|
||||
if (idx < vec_size(tn->agg_type.variants))
|
||||
return vec_at(tn->agg_type.variants, idx).type_id;
|
||||
|
||||
Reference in new issue
Block a user