Skip to content

Commit

Permalink
Bug 1288222 - Baldr: match signature types structurally (r=bbouvier)
Browse files Browse the repository at this point in the history
MozReview-Commit-ID: 7Noq2TBkKmB
  • Loading branch information
Luke Wagner committed Jul 22, 2016
1 parent 747a41e commit ff32583
Show file tree
Hide file tree
Showing 30 changed files with 616 additions and 281 deletions.
6 changes: 3 additions & 3 deletions js/src/asmjs/AsmJS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1543,10 +1543,10 @@ class MOZ_STACK_CLASS ModuleValidator
class NamedSig
{
PropertyName* name_;
const DeclaredSig* sig_;
const SigWithId* sig_;

public:
NamedSig(PropertyName* name, const DeclaredSig& sig)
NamedSig(PropertyName* name, const SigWithId& sig)
: name_(name), sig_(&sig)
{}
PropertyName* name() const {
Expand All @@ -1570,7 +1570,7 @@ class MOZ_STACK_CLASS ModuleValidator
}
};
typedef HashMap<NamedSig, uint32_t, NamedSig> ImportMap;
typedef HashMap<const DeclaredSig*, uint32_t, SigHashPolicy> SigMap;
typedef HashMap<const SigWithId*, uint32_t, SigHashPolicy> SigMap;
typedef HashMap<PropertyName*, Global*> GlobalMap;
typedef HashMap<PropertyName*, MathBuiltin> MathNameMap;
typedef HashMap<PropertyName*, AsmJSAtomicsBuiltinFunction> AtomicsNameMap;
Expand Down
20 changes: 15 additions & 5 deletions js/src/asmjs/WasmBaselineCompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1737,7 +1737,7 @@ class BaseCompiler
void beginFunction() {
JitSpew(JitSpew_Codegen, "# Emitting wasm baseline code");

wasm::GenerateFunctionPrologue(masm, localSize_, mg_.funcSigIndex(func_.index()),
wasm::GenerateFunctionPrologue(masm, localSize_, mg_.funcSigs[func_.index()]->id,
&compileResults_.offsets());

MOZ_ASSERT(masm.framePushed() == uint32_t(localSize_));
Expand Down Expand Up @@ -2041,7 +2041,7 @@ class BaseCompiler

// Precondition: sync()

void funcPtrCall(const Sig& sig, uint32_t sigIndex, uint32_t length, uint32_t globalDataOffset,
void funcPtrCall(const SigWithId& sig, uint32_t length, uint32_t globalDataOffset,
Stk& indexVal, const FunctionCall& call)
{
Register ptrReg = WasmTableCallPtrReg;
Expand All @@ -2054,7 +2054,17 @@ class BaseCompiler
} else {
masm.branch32(Assembler::Condition::AboveOrEqual, ptrReg, Imm32(length),
wasm::JumpTarget::OutOfBounds);
masm.move32(Imm32(sigIndex), WasmTableCallSigReg);
}

switch (sig.id.kind()) {
case SigIdDesc::Kind::Global:
masm.loadWasmGlobalPtr(sig.id.globalDataOffset(), WasmTableCallSigReg);
break;
case SigIdDesc::Kind::Immediate:
masm.move32(Imm32(sig.id.immediate()), WasmTableCallSigReg);
break;
case SigIdDesc::Kind::None:
break;
}

{
Expand Down Expand Up @@ -5143,7 +5153,7 @@ BaseCompiler::emitCallIndirect(uint32_t callOffset)

Nothing callee_;

const Sig& sig = mg_.sigs[sigIndex];
const SigWithId& sig = mg_.sigs[sigIndex];

if (deadCode_) {
return skipCall(sig.args()) && iter_.readCallIndirectCallee(&callee_) &&
Expand Down Expand Up @@ -5175,7 +5185,7 @@ BaseCompiler::emitCallIndirect(uint32_t callOffset)
? mg_.tables[mg_.asmJSSigToTableIndex[sigIndex]]
: mg_.tables[0];

funcPtrCall(sig, sigIndex, table.initial, table.globalDataOffset, callee, baselineCall);
funcPtrCall(sig, table.initial, table.globalDataOffset, callee, baselineCall);

endCall(baselineCall);

Expand Down
56 changes: 12 additions & 44 deletions js/src/asmjs/WasmCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,98 +249,62 @@ CodeSegment::~CodeSegment()
DeallocateExecutableMemory(bytes_, totalLength(), gc::SystemPageSize());
}

static size_t
SerializedSigSize(const Sig& sig)
{
return sizeof(ExprType) +
SerializedPodVectorSize(sig.args());
}

static uint8_t*
SerializeSig(uint8_t* cursor, const Sig& sig)
{
cursor = WriteScalar<ExprType>(cursor, sig.ret());
cursor = SerializePodVector(cursor, sig.args());
return cursor;
}

static const uint8_t*
DeserializeSig(const uint8_t* cursor, Sig* sig)
{
ExprType ret;
cursor = ReadScalar<ExprType>(cursor, &ret);

ValTypeVector args;
cursor = DeserializePodVector(cursor, &args);
if (!cursor)
return nullptr;

*sig = Sig(Move(args), ret);
return cursor;
}

static size_t
SizeOfSigExcludingThis(const Sig& sig, MallocSizeOf mallocSizeOf)
{
return sig.args().sizeOfExcludingThis(mallocSizeOf);
}

size_t
FuncExport::serializedSize() const
{
return SerializedSigSize(sig_) +
return sig_.serializedSize() +
sizeof(pod);
}

uint8_t*
FuncExport::serialize(uint8_t* cursor) const
{
cursor = SerializeSig(cursor, sig_);
cursor = sig_.serialize(cursor);
cursor = WriteBytes(cursor, &pod, sizeof(pod));
return cursor;
}

const uint8_t*
FuncExport::deserialize(const uint8_t* cursor)
{
(cursor = DeserializeSig(cursor, &sig_)) &&
(cursor = sig_.deserialize(cursor)) &&
(cursor = ReadBytes(cursor, &pod, sizeof(pod)));
return cursor;
}

size_t
FuncExport::sizeOfExcludingThis(MallocSizeOf mallocSizeOf) const
{
return SizeOfSigExcludingThis(sig_, mallocSizeOf);
return sig_.sizeOfExcludingThis(mallocSizeOf);
}

size_t
FuncImport::serializedSize() const
{
return SerializedSigSize(sig_) +
return sig_.serializedSize() +
sizeof(pod);
}

uint8_t*
FuncImport::serialize(uint8_t* cursor) const
{
cursor = SerializeSig(cursor, sig_);
cursor = sig_.serialize(cursor);
cursor = WriteBytes(cursor, &pod, sizeof(pod));
return cursor;
}

const uint8_t*
FuncImport::deserialize(const uint8_t* cursor)
{
(cursor = DeserializeSig(cursor, &sig_)) &&
(cursor = sig_.deserialize(cursor)) &&
(cursor = ReadBytes(cursor, &pod, sizeof(pod)));
return cursor;
}

size_t
FuncImport::sizeOfExcludingThis(MallocSizeOf mallocSizeOf) const
{
return SizeOfSigExcludingThis(sig_, mallocSizeOf);
return sig_.sizeOfExcludingThis(mallocSizeOf);
}

CodeRange::CodeRange(Kind kind, Offsets offsets)
Expand Down Expand Up @@ -452,6 +416,7 @@ Metadata::serializedSize() const
return sizeof(pod()) +
SerializedVectorSize(funcImports) +
SerializedVectorSize(funcExports) +
SerializedVectorSize(sigIds) +
SerializedPodVectorSize(tables) +
SerializedPodVectorSize(memoryAccesses) +
SerializedPodVectorSize(boundsChecks) +
Expand All @@ -469,6 +434,7 @@ Metadata::serialize(uint8_t* cursor) const
cursor = WriteBytes(cursor, &pod(), sizeof(pod()));
cursor = SerializeVector(cursor, funcImports);
cursor = SerializeVector(cursor, funcExports);
cursor = SerializeVector(cursor, sigIds);
cursor = SerializePodVector(cursor, tables);
cursor = SerializePodVector(cursor, memoryAccesses);
cursor = SerializePodVector(cursor, boundsChecks);
Expand All @@ -487,6 +453,7 @@ Metadata::deserialize(const uint8_t* cursor)
(cursor = ReadBytes(cursor, &pod(), sizeof(pod()))) &&
(cursor = DeserializeVector(cursor, &funcImports)) &&
(cursor = DeserializeVector(cursor, &funcExports)) &&
(cursor = DeserializeVector(cursor, &sigIds)) &&
(cursor = DeserializePodVector(cursor, &tables)) &&
(cursor = DeserializePodVector(cursor, &memoryAccesses)) &&
(cursor = DeserializePodVector(cursor, &boundsChecks)) &&
Expand All @@ -504,6 +471,7 @@ Metadata::sizeOfExcludingThis(MallocSizeOf mallocSizeOf) const
{
return SizeOfVectorExcludingThis(funcImports, mallocSizeOf) +
SizeOfVectorExcludingThis(funcExports, mallocSizeOf) +
SizeOfVectorExcludingThis(sigIds, mallocSizeOf) +
tables.sizeOfExcludingThis(mallocSizeOf) +
memoryAccesses.sizeOfExcludingThis(mallocSizeOf) +
boundsChecks.sizeOfExcludingThis(mallocSizeOf) +
Expand Down
1 change: 1 addition & 0 deletions js/src/asmjs/WasmCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ struct Metadata : ShareableBase<Metadata>, MetadataCacheablePod

FuncImportVector funcImports;
FuncExportVector funcExports;
SigWithIdVector sigIds;
TableDescVector tables;
MemoryAccessVector memoryAccesses;
BoundsCheckVector boundsChecks;
Expand Down
10 changes: 5 additions & 5 deletions js/src/asmjs/WasmCompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ DecodeTypeSection(Decoder& d, ModuleGeneratorData* init)
}

static bool
DecodeSignatureIndex(Decoder& d, const ModuleGeneratorData& init, const DeclaredSig** sig)
DecodeSignatureIndex(Decoder& d, const ModuleGeneratorData& init, const SigWithId** sig)
{
uint32_t sigIndex;
if (!d.readVarU32(&sigIndex))
Expand Down Expand Up @@ -723,7 +723,7 @@ static bool
DecodeImport(Decoder& d, bool newFormat, ModuleGeneratorData* init, ImportVector* imports)
{
if (!newFormat) {
const DeclaredSig* sig = nullptr;
const SigWithId* sig = nullptr;
if (!DecodeSignatureIndex(d, *init, &sig))
return false;

Expand Down Expand Up @@ -764,7 +764,7 @@ DecodeImport(Decoder& d, bool newFormat, ModuleGeneratorData* init, ImportVector

switch (DefinitionKind(importKind)) {
case DefinitionKind::Function: {
const DeclaredSig* sig = nullptr;
const SigWithId* sig = nullptr;
if (!DecodeSignatureIndex(d, *init, &sig))
return false;
if (!CheckTypeForJS(d, *sig))
Expand Down Expand Up @@ -1067,7 +1067,7 @@ DecodeFunctionBody(Decoder& d, ModuleGenerator& mg, uint32_t funcIndex)
return false;

ValTypeVector locals;
const DeclaredSig& sig = mg.funcSig(funcIndex);
const Sig& sig = mg.funcSig(funcIndex);
if (!locals.appendAll(sig.args()))
return false;

Expand Down Expand Up @@ -1120,7 +1120,7 @@ DecodeStartSection(Decoder& d, ModuleGenerator& mg)
if (startFuncIndex >= mg.numFuncSigs())
return Fail(d, "unknown start function");

const DeclaredSig& sig = mg.funcSig(startFuncIndex);
const Sig& sig = mg.funcSig(startFuncIndex);
if (sig.ret() != ExprType::Void)
return Fail(d, "start function must not return anything");

Expand Down
19 changes: 16 additions & 3 deletions js/src/asmjs/WasmFrameIterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ GenerateProfilingEpilogue(MacroAssembler& masm, unsigned framePushed, ExitReason
// Specifically, ToggleProfiling patches all callsites to either call the
// profiling or non-profiling entry point.
void
wasm::GenerateFunctionPrologue(MacroAssembler& masm, unsigned framePushed, uint32_t sigIndex,
wasm::GenerateFunctionPrologue(MacroAssembler& masm, unsigned framePushed, const SigIdDesc& sigId,
FuncOffsets* offsets)
{
#if defined(JS_CODEGEN_ARM)
Expand All @@ -349,8 +349,21 @@ wasm::GenerateFunctionPrologue(MacroAssembler& masm, unsigned framePushed, uint3
// Generate table entry thunk:
masm.haltingAlign(CodeAlignment);
offsets->tableEntry = masm.currentOffset();
masm.branch32(Assembler::Condition::NotEqual, WasmTableCallSigReg, Imm32(sigIndex),
JumpTarget::BadIndirectCall);
switch (sigId.kind()) {
case SigIdDesc::Kind::Global: {
Register scratch = WasmTableCallPtrReg; // clobbered by the indirect call
masm.loadWasmGlobalPtr(sigId.globalDataOffset(), scratch);
masm.branch32(Assembler::Condition::NotEqual, WasmTableCallSigReg, scratch,
JumpTarget::BadIndirectCall);
break;
}
case SigIdDesc::Kind::Immediate:
masm.branch32(Assembler::Condition::NotEqual, WasmTableCallSigReg, Imm32(sigId.immediate()),
JumpTarget::BadIndirectCall);
break;
case SigIdDesc::Kind::None:
break;
}
offsets->tableProfilingJump = masm.nopPatchableToNearJump().offset();

// Generate normal prologue:
Expand Down
3 changes: 2 additions & 1 deletion js/src/asmjs/WasmFrameIterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace wasm {
class CallSite;
class CodeRange;
class Instance;
class SigIdDesc;
struct CallThunk;
struct FuncOffsets;
struct Metadata;
Expand Down Expand Up @@ -111,7 +112,7 @@ void
GenerateExitEpilogue(jit::MacroAssembler& masm, unsigned framePushed, ExitReason reason,
ProfilingOffsets* offsets);
void
GenerateFunctionPrologue(jit::MacroAssembler& masm, unsigned framePushed, uint32_t sigIndex,
GenerateFunctionPrologue(jit::MacroAssembler& masm, unsigned framePushed, const SigIdDesc& sigId,
FuncOffsets* offsets);
void
GenerateFunctionEpilogue(jit::MacroAssembler& masm, unsigned framePushed, FuncOffsets* offsets);
Expand Down
30 changes: 25 additions & 5 deletions js/src/asmjs/WasmGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,26 @@ ModuleGenerator::init(UniqueModuleGeneratorData shared, CompileArgs&& args,
if (!allocateGlobalBytes(sizeof(void*), sizeof(void*), &table.globalDataOffset))
return false;
}

for (uint32_t i = 0; i < numSigs_; i++) {
SigWithId& sig = shared_->sigs[i];
if (SigIdDesc::isGlobal(sig)) {
uint32_t globalDataOffset;
if (!allocateGlobalBytes(sizeof(void*), sizeof(void*), &globalDataOffset))
return false;

sig.id = SigIdDesc::global(sig, globalDataOffset);

Sig copy;
if (!copy.clone(sig))
return false;

if (!metadata_->sigIds.emplaceBack(Move(copy), sig.id))
return false;
} else {
sig.id = SigIdDesc::immediate(sig);
}
}
} else {
MOZ_ASSERT(shared_->sigs.length() == MaxSigs);
MOZ_ASSERT(shared_->tables.length() == MaxTables);
Expand Down Expand Up @@ -341,9 +361,6 @@ ModuleGenerator::finishTask(IonCompileTask* task)
return true;
}

typedef Vector<Offsets, 0, SystemAllocPolicy> OffsetVector;
typedef Vector<ProfilingOffsets, 0, SystemAllocPolicy> ProfilingOffsetVector;

bool
ModuleGenerator::finishFuncExports()
{
Expand Down Expand Up @@ -376,6 +393,9 @@ ModuleGenerator::finishFuncExports()
return true;
}

typedef Vector<Offsets, 0, SystemAllocPolicy> OffsetVector;
typedef Vector<ProfilingOffsets, 0, SystemAllocPolicy> ProfilingOffsetVector;

bool
ModuleGenerator::finishCodegen()
{
Expand Down Expand Up @@ -616,7 +636,7 @@ ModuleGenerator::initSig(uint32_t sigIndex, Sig&& sig)
shared_->sigs[sigIndex] = Move(sig);
}

const DeclaredSig&
const SigWithId&
ModuleGenerator::sig(uint32_t index) const
{
MOZ_ASSERT(index < numSigs_);
Expand Down Expand Up @@ -650,7 +670,7 @@ ModuleGenerator::bumpMinMemoryLength(uint32_t newMinMemoryLength)
shared_->minMemoryLength = newMinMemoryLength;
}

const DeclaredSig&
const SigWithId&
ModuleGenerator::funcSig(uint32_t funcIndex) const
{
MOZ_ASSERT(shared_->funcSigs[funcIndex]);
Expand Down
Loading

0 comments on commit ff32583

Please sign in to comment.