summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2016-04-27 14:54:30 -0700
committerAlon Zakai <alonzakai@gmail.com>2016-04-27 17:00:35 -0700
commitf22e2796f5ac72c64c36cb562d1462093741f8d7 (patch)
tree9f33653987be8cb74be6783221e98340f95b27cc
parent90bdf763537815a523d0dbe75a67e7f0b14b0d9e (diff)
downloadbinaryen-f22e2796f5ac72c64c36cb562d1462093741f8d7.tar.gz
binaryen-f22e2796f5ac72c64c36cb562d1462093741f8d7.tar.bz2
binaryen-f22e2796f5ac72c64c36cb562d1462093741f8d7.zip
just use a simple vector in data segments
-rw-r--r--src/passes/Print.cpp2
-rw-r--r--src/s2wasm.h42
-rw-r--r--src/shell-interface.h6
-rw-r--r--src/wasm-binary.h14
-rw-r--r--src/wasm-linker.cpp9
-rw-r--r--src/wasm-linker.h5
-rw-r--r--src/wasm-validator.h4
-rw-r--r--src/wasm.h11
8 files changed, 51 insertions, 42 deletions
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp
index 256968bb7..1a62de4f3 100644
--- a/src/passes/Print.cpp
+++ b/src/passes/Print.cpp
@@ -485,7 +485,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
for (auto segment : curr->memory.segments) {
o << maybeNewLine;
o << (minify ? "" : " ") << "(segment " << segment.offset << " \"";
- for (size_t i = 0; i < segment.size; i++) {
+ for (size_t i = 0; i < segment.data.size(); i++) {
unsigned char c = segment.data[i];
switch (c) {
case '\n': o << "\\n"; break;
diff --git a/src/s2wasm.h b/src/s2wasm.h
index b66859ce9..3efd667d5 100644
--- a/src/s2wasm.h
+++ b/src/s2wasm.h
@@ -1035,7 +1035,7 @@ class S2WasmBuilder {
}
mustMatch(name.str);
mustMatch(":");
- auto raw = new std::vector<char>(); // leaked intentionally, no new allocation in Memory
+ std::vector<char> raw;
bool zero = true;
std::vector<std::pair<LinkerObject::Relocation*, size_t>> currRelocations; // [relocation, offset in raw]
while (1) {
@@ -1049,8 +1049,8 @@ class S2WasmBuilder {
z = true;
}
auto quoted = getQuoted();
- raw->insert(raw->end(), quoted.begin(), quoted.end());
- if (z) raw->push_back(0);
+ raw.insert(raw.end(), quoted.begin(), quoted.end());
+ if (z) raw.push_back(0);
zero = false;
} else if (match(".zero") || match(".skip")) {
int32_t size = getInt();
@@ -1063,43 +1063,43 @@ class S2WasmBuilder {
if (value != 0) zero = false;
}
for (size_t i = 0, e = size; i < e; i++) {
- raw->push_back(value);
+ raw.push_back(value);
}
} else if (match(".int8")) {
- size_t size = raw->size();
- raw->resize(size + 1);
- (*(int8_t*)(&(*raw)[size])) = getInt();
+ size_t size = raw.size();
+ raw.resize(size + 1);
+ (*(int8_t*)(&raw[size])) = getInt();
zero = false;
} else if (match(".int16")) {
- size_t size = raw->size();
- raw->resize(size + 2);
- (*(int16_t*)(&(*raw)[size])) = getInt();
+ size_t size = raw.size();
+ raw.resize(size + 2);
+ (*(int16_t*)(&raw[size])) = getInt();
zero = false;
} else if (match(".int32")) {
- size_t size = raw->size();
- raw->resize(size + 4);
- if (getConst((uint32_t*)&(*raw)[size])) { // just the size, as we may reallocate; we must fix this later, if it's a relocation
+ size_t size = raw.size();
+ raw.resize(size + 4);
+ if (getConst((uint32_t*)&raw[size])) { // just the size, as we may reallocate; we must fix this later, if it's a relocation
currRelocations.emplace_back(linkerObj->getCurrentRelocation(), size);
}
zero = false;
} else if (match(".int64")) {
- size_t size = raw->size();
- raw->resize(size + 8);
- (*(int64_t*)(&(*raw)[size])) = getInt64();
+ size_t size = raw.size();
+ raw.resize(size + 8);
+ (*(int64_t*)(&raw[size])) = getInt64();
zero = false;
} else {
break;
}
}
skipWhitespace();
- size_t size = raw->size();
+ size_t size = raw.size();
if (match(".size")) {
mustMatch(name.str);
mustMatch(",");
size_t seenSize = atoi(getStr().str); // TODO: optimize
assert(seenSize >= size);
- while (raw->size() < seenSize) {
- raw->push_back(0);
+ while (raw.size() < seenSize) {
+ raw.push_back(0);
}
size = seenSize;
}
@@ -1107,12 +1107,12 @@ class S2WasmBuilder {
for (auto& curr : currRelocations) {
auto* r = curr.first;
auto i = curr.second;
- r->data = (uint32_t*)&(*raw)[i];
+ r->data = (uint32_t*)&raw[i];
}
// assign the address, add to memory
linkerObj->addStatic(size, align, name);
if (!zero) {
- linkerObj->addSegment(name, (const char*)&(*raw)[0], size);
+ linkerObj->addSegment(name, raw);
}
}
diff --git a/src/shell-interface.h b/src/shell-interface.h
index b23b7b645..1dbabf3fa 100644
--- a/src/shell-interface.h
+++ b/src/shell-interface.h
@@ -88,9 +88,9 @@ struct ShellExternalInterface : ModuleInstance::ExternalInterface {
void init(Module& wasm) override {
memory.resize(wasm.memory.initial * wasm::Memory::kPageSize);
// apply memory segments
- for (auto segment : wasm.memory.segments) {
- assert(segment.offset + segment.size <= wasm.memory.initial * wasm::Memory::kPageSize);
- for (size_t i = 0; i != segment.size; ++i) {
+ for (auto& segment : wasm.memory.segments) {
+ assert(segment.offset + segment.data.size() <= wasm.memory.initial * wasm::Memory::kPageSize);
+ for (size_t i = 0; i != segment.data.size(); ++i) {
memory.set(segment.offset + i, segment.data[i]);
}
}
diff --git a/src/wasm-binary.h b/src/wasm-binary.h
index 9d4f567b0..91fe91b49 100644
--- a/src/wasm-binary.h
+++ b/src/wasm-binary.h
@@ -666,14 +666,14 @@ public:
if (wasm->memory.segments.size() == 0) return;
uint32_t num = 0;
for (auto& segment : wasm->memory.segments) {
- if (segment.size > 0) num++;
+ if (segment.data.size() > 0) num++;
}
auto start = startSection(BinaryConsts::Section::DataSegments);
o << U32LEB(num);
for (auto& segment : wasm->memory.segments) {
- if (segment.size == 0) continue;
+ if (segment.data.size() == 0) continue;
o << U32LEB(segment.offset);
- writeInlineBuffer(segment.data, segment.size);
+ writeInlineBuffer(&segment.data[0], segment.data.size());
}
finishSection(start);
}
@@ -1548,15 +1548,13 @@ public:
auto num = getU32LEB();
for (size_t i = 0; i < num; i++) {
Memory::Segment curr;
- curr.offset = getU32LEB();
+ auto offset = getU32LEB();
auto size = getU32LEB();
- auto buffer = (char*)malloc(size);
+ char buffer[size];
for (size_t j = 0; j < size; j++) {
buffer[j] = char(getInt8());
}
- curr.data = (const char*)buffer;
- curr.size = size;
- wasm.memory.segments.push_back(curr);
+ wasm.memory.segments.emplace_back(offset, (const char*)buffer, size);
}
}
diff --git a/src/wasm-linker.cpp b/src/wasm-linker.cpp
index 334ea475d..a854f0cd0 100644
--- a/src/wasm-linker.cpp
+++ b/src/wasm-linker.cpp
@@ -43,10 +43,11 @@ void Linker::placeStackPointer(size_t stackAllocation) {
if (stackAllocation) {
// If we are allocating the stack, set up a relocation to initialize the
// stack pointer to point to one past-the-end of the stack allocation.
- auto* raw = new uint32_t;
- out.addRelocation(LinkerObject::Relocation::kData, raw, ".stack", stackAllocation);
+ std::vector<char> raw;
+ raw.resize(pointerSize);
+ out.addRelocation(LinkerObject::Relocation::kData, (uint32_t*)&raw[0], ".stack", stackAllocation);
assert(out.wasm.memory.segments.size() == 0);
- out.addSegment("__stack_pointer", reinterpret_cast<char*>(raw), pointerSize);
+ out.addSegment("__stack_pointer", raw);
}
}
@@ -238,7 +239,7 @@ void Linker::emscriptenGlue(std::ostream& o) {
if (curr->target == EMSCRIPTEN_ASM_CONST) {
auto arg = curr->operands[0]->cast<Const>();
size_t segmentIndex = parent->segmentsByAddress[arg->value.geti32()];
- std::string code = escape(parent->out.wasm.memory.segments[segmentIndex].data);
+ std::string code = escape(&parent->out.wasm.memory.segments[segmentIndex].data[0]);
int32_t id;
if (ids.count(code) == 0) {
id = ids.size();
diff --git a/src/wasm-linker.h b/src/wasm-linker.h
index 07f270d69..f6cf832c7 100644
--- a/src/wasm-linker.h
+++ b/src/wasm-linker.h
@@ -103,6 +103,11 @@ class LinkerObject {
wasm.memory.segments.emplace_back(0, data, size);
}
+ void addSegment(Name name, std::vector<char>& data) {
+ segments[name] = wasm.memory.segments.size();
+ wasm.memory.segments.emplace_back(0, data);
+ }
+
void addInitializerFunction(Name name) {
initializerFunctions.emplace_back(name);
assert(symbolInfo.implementedFunctions.count(name));
diff --git a/src/wasm-validator.h b/src/wasm-validator.h
index 73aa7fecf..ca168e48b 100644
--- a/src/wasm-validator.h
+++ b/src/wasm-validator.h
@@ -87,9 +87,9 @@ public:
void visitMemory(Memory *curr) {
shouldBeFalse(curr->initial > curr->max);
size_t top = 0;
- for (auto segment : curr->segments) {
+ for (auto& segment : curr->segments) {
shouldBeFalse(segment.offset < top);
- top = segment.offset + segment.size;
+ top = segment.offset + segment.data.size();
}
shouldBeFalse(top > curr->initial);
}
diff --git a/src/wasm.h b/src/wasm.h
index 45a01bd76..b5dee81b8 100644
--- a/src/wasm.h
+++ b/src/wasm.h
@@ -1169,10 +1169,15 @@ public:
static const size_t kPageMask = ~(kPageSize - 1);
struct Segment {
size_t offset;
- const char* data;
- size_t size;
+ std::vector<char> data; // TODO: optimize
Segment() {}
- Segment(size_t offset, const char *data, size_t size) : offset(offset), data(data), size(size) {}
+ Segment(size_t offset, const char *init, size_t size) : offset(offset) {
+ data.resize(size);
+ memcpy(&data[0], init, size);
+ }
+ Segment(size_t offset, std::vector<char>& init) : offset(offset) {
+ data.swap(init);
+ }
};
size_t initial, max; // sizes are in pages