Skip to content

Commit

Permalink
Give string literals an alloca
Browse files Browse the repository at this point in the history
  • Loading branch information
Victorious3 committed Oct 8, 2021
1 parent 193cc44 commit c45e7fe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
5 changes: 0 additions & 5 deletions princess.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <signal.h>
#include <stdarg.h>
#include <stdalign.h>

#ifndef _WIN32
#include <linux/limits.h>
Expand Down
21 changes: 19 additions & 2 deletions src/compiler.pr
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,14 @@ def walk_String(node: *parser::Node, state: *State) -> Value {
} !Value

let global = make_global_value(strtpe, "str", str_value, state)

let alloca_ret = make_local_value(tpe, null, state)
let alloca = make_insn_dbg(InsnKind::ALLOCA, loc)
alloca.value.alloca = {
ret = alloca_ret
} !InsnAlloca
push_insn(alloca, state)
alloca_ret.tpe = typechecking::pointer(tpe)

let index = allocate(Value, 2)
index[0] = make_int_value(0)
Expand All @@ -823,8 +831,10 @@ def walk_String(node: *parser::Node, state: *State) -> Value {
index = index
} !InsnGetElementPtr
push_insn(gep, state)

let ret = make_local_value(tpe, null, state)

let alloca_retp = allocate(Value)
@alloca_retp = alloca_ret
let ret = make_local_value(tpe, alloca_retp, state)

let values = allocate(Value, 2)
values[0] = {
Expand Down Expand Up @@ -854,6 +864,13 @@ def walk_String(node: *parser::Node, state: *State) -> Value {
} !InsnInsertValue
push_insn(insert, state)

let store = make_insn_dbg(InsnKind::STORE, loc)
store.value.store = {
value = ret,
loc = alloca_ret
} !InsnStore
push_insn(store, state)

return ret
}

Expand Down

0 comments on commit c45e7fe

Please sign in to comment.