-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
wasm: fix type_size and precompute symbol sizes, alignment and offset of fields in structs #26268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
20a37b6 to
fcec66e
Compare
… of fields in structs
fcec66e to
76a8a6b
Compare
dc4ea8a to
e142168
Compare
|
As it is. This pr is done. However as seen in CI I thought working around the |
pub fn (mut p Pool) type_size(typ ast.Type) (int, int) {
ts := p.table.sym(typ)
if ts.size != -1 && typ.idx() in p.structs {
return ts.size, ts.align
}
if ts.info is ast.Enum {
return p.table.type_size(ts.info.typ)
}
if ts.info !is ast.Struct {
return p.table.type_size(typ)
}
ti := ts.info as ast.Struct
// code borrowed from native, inserted in wasm, and now here!
mut strc := StructInfo{}
mut size := 0
mut align := 1
mut structs := []ast.Type{}
for f in ti.fields {
sym := p.table.sym(f.typ)
f_size, f_align := if typ.is_ptr() {
if sym.info is ast.Struct && !p.structs.keys().contains(typ.clear_ref()) {
structs << typ.clear_ref()
}
p.type_size(ast.voidptr_type)
} else {
p.type_size(f.typ)
}
if f_size == 0 {
strc.offsets << 0
continue
}
padding := (f_align - size % f_align) % f_align
strc.offsets << size + padding
size += f_size + padding
if f_align > align {
align = f_align
}
}
size = (size + align - 1) / align * align
p.structs[typ.idx()] = strc
for s in structs {
p.type_size(s)
}
mut ts_ := p.table.sym(typ)
ts_.size = size
ts_.align = align
return size, align
} |
|
To simplify it the "nr_muls > 0" check at the beginning of the function is already enough. But I choose not to do that. Thinking more about it, I am not sure anymore why I did it this way. |
no problem, i found some wrong in padding caculate |
|
Closing in favor of #26357 |

Previously type_size would incorrectly compute sizes for pointers to structs in struct fields as if they were structs themselves and not pointers to structs leading to an infinite loop for structs containing pointers to themselves besides not handling pointers correctly. Offsets of fields in structs are needed to allocate memory and be able to access these fields these are precomputed now together with symbol sizes and their alignment like it is in the native backend. The code for the function type_size is adapted from the almost equivalent function in the ast module.
Handling structs which contain references to themself no longer segfaults the compiler.