Skip to content

Commit

Permalink
pass the line number to the parsed instructions for a source map gene…
Browse files Browse the repository at this point in the history
…ration #6
  • Loading branch information
Horcrux7 committed Mar 31, 2019
1 parent 0b1ff00 commit 5e40f16
Show file tree
Hide file tree
Showing 16 changed files with 334 additions and 259 deletions.
23 changes: 14 additions & 9 deletions src/de/inetsoftware/jwebassembly/module/BranchManger.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ private void convertToLoop( ParsedBlock gotoBlock, int conditionStart, int condi

gotoBlock.op = JavaBlockOperator.LOOP;
gotoBlock.endPosition = conditionEnd;
instructions.add( i, new WasmBlockInstruction( WasmBlockOperator.BR, 0, conditionNew ) );
instructions.add( conditionIdx++, new WasmBlockInstruction( WasmBlockOperator.BR_IF, 1, conditionNew ) );
instructions.add( i, new WasmBlockInstruction( WasmBlockOperator.BR, 0, conditionNew, gotoBlock.lineNumber ) );
instructions.add( conditionIdx++, new WasmBlockInstruction( WasmBlockOperator.BR_IF, 1, conditionNew, gotoBlock.lineNumber ) );
}

/**
Expand Down Expand Up @@ -743,15 +743,18 @@ boolean isCatch( int codePosition ) {
void handle( CodeInputStream byteCode ) {
int codePosition = -1;
for( int idx = 0; idx < instructions.size(); idx++ ) {
int nextCodePosition = instructions.get( idx ).getCodePosition();
WasmInstruction instr = instructions.get( idx );
int lineNumber;
int nextCodePosition = instr.getCodePosition();
if( nextCodePosition <= codePosition ) {
continue;
} else {
codePosition = nextCodePosition;
lineNumber = instr.getLineNumber();
}
idx = root.handle( codePosition, instructions, idx );
idx = root.handle( codePosition, instructions, idx, lineNumber );
}
root.handle( byteCode.getCodePosition(), instructions, instructions.size() );
root.handle( byteCode.getCodePosition(), instructions, instructions.size(), byteCode.getLineNumber() );
}

/**
Expand Down Expand Up @@ -947,20 +950,22 @@ public boolean add( BranchNode e ) {
* the target for instructions
* @param idx
* index in the current instruction
* @param lineNumber
* the line number in the Java source code
* @return the new index in the instructions
*/
int handle( int codePosition, List<WasmInstruction> instructions, int idx ) {
int handle( int codePosition, List<WasmInstruction> instructions, int idx, int lineNumber ) {
if( codePosition < startPos || codePosition > endPos ) {
return idx;
}
if( codePosition == startPos && startOp != null ) {
instructions.add( idx++, new WasmBlockInstruction( startOp, data, codePosition ) );
instructions.add( idx++, new WasmBlockInstruction( startOp, data, codePosition, lineNumber ) );
}
for( BranchNode branch : this ) {
idx = branch.handle( codePosition, instructions, idx );
idx = branch.handle( codePosition, instructions, idx, lineNumber );
}
if( codePosition == endPos && endOp != null ) {
instructions.add( idx++, new WasmBlockInstruction( endOp, null, codePosition ) );
instructions.add( idx++, new WasmBlockInstruction( endOp, null, codePosition, lineNumber ) );
}
return idx;
}
Expand Down
359 changes: 184 additions & 175 deletions src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ class WasmArrayInstruction extends WasmInstruction {
* the type of the parameters
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
*/
WasmArrayInstruction( @Nullable ArrayOperator op, @Nullable AnyType type, int javaCodePos ) {
super( javaCodePos );
WasmArrayInstruction( @Nullable ArrayOperator op, @Nullable AnyType type, int javaCodePos, int lineNumber ) {
super( javaCodePos, lineNumber );
this.op = op;
this.type = type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ class WasmBlockInstruction extends WasmInstruction {
* extra data depending of the operator
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
*/
WasmBlockInstruction( @Nonnull WasmBlockOperator op, @Nullable Object data, int javaCodePos ) {
super( javaCodePos );
WasmBlockInstruction( @Nonnull WasmBlockOperator op, @Nullable Object data, int javaCodePos, int lineNumber ) {
super( javaCodePos, lineNumber );
this.op = op;
this.data = data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ class WasmCallInstruction extends WasmInstruction {
* the function name that should be called
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
*/
WasmCallInstruction( FunctionName name, int javaCodePos ) {
super( javaCodePos );
WasmCallInstruction( FunctionName name, int javaCodePos, int lineNumber ) {
super( javaCodePos, lineNumber );
this.name = name;
}

Expand Down
72 changes: 48 additions & 24 deletions src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,13 @@ protected void calculateVariables() {
* the memory/slot index of the variable in Java byte code
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
*/
@Nonnull
protected void addLoadStoreInstruction( AnyType valueType, boolean load, @Nonnegative int javaIdx, int javaCodePos ) {
protected void addLoadStoreInstruction( AnyType valueType, boolean load, @Nonnegative int javaIdx, int javaCodePos, int lineNumber ) {
localVariables.use( valueType, javaIdx );
instructions.add( new WasmLoadStoreInstruction( load, javaIdx, localVariables, javaCodePos ) );
instructions.add( new WasmLoadStoreInstruction( load, javaIdx, localVariables, javaCodePos, lineNumber ) );
}

/**
Expand All @@ -129,10 +131,12 @@ protected void addLoadStoreInstruction( AnyType valueType, boolean load, @Nonneg
* the index of the variable
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
*/
@Nonnull
protected void addLocalInstruction( boolean load, @Nonnegative int wasmIdx, int javaCodePos ) {
instructions.add( new WasmLocalInstruction( load, wasmIdx, javaCodePos ) );
protected void addLocalInstruction( boolean load, @Nonnegative int wasmIdx, int javaCodePos, int lineNumber ) {
instructions.add( new WasmLocalInstruction( load, wasmIdx, javaCodePos, lineNumber ) );
}

/**
Expand All @@ -144,9 +148,11 @@ protected void addLocalInstruction( boolean load, @Nonnegative int wasmIdx, int
* reference to a static field
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
*/
protected void addGlobalInstruction( boolean load, Member ref, int javaCodePos ) {
instructions.add( new WasmGlobalInstruction( load, ref, javaCodePos ) );
protected void addGlobalInstruction( boolean load, Member ref, int javaCodePos, int lineNumber ) {
instructions.add( new WasmGlobalInstruction( load, ref, javaCodePos, lineNumber ) );
}

/**
Expand All @@ -158,9 +164,11 @@ protected void addGlobalInstruction( boolean load, Member ref, int javaCodePos )
* the value type
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
*/
protected void addConstInstruction( Number value, ValueType valueType, int javaCodePos ) {
instructions.add( new WasmConstInstruction( value, valueType, javaCodePos ) );
protected void addConstInstruction( Number value, ValueType valueType, int javaCodePos, int lineNumber ) {
instructions.add( new WasmConstInstruction( value, valueType, javaCodePos, lineNumber ) );
}

/**
Expand All @@ -170,9 +178,11 @@ protected void addConstInstruction( Number value, ValueType valueType, int javaC
* the value
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
*/
protected void addConstInstruction( Number value, int javaCodePos ) {
instructions.add( new WasmConstInstruction( value, javaCodePos ) );
protected void addConstInstruction( Number value, int javaCodePos, int lineNumber ) {
instructions.add( new WasmConstInstruction( value, javaCodePos, lineNumber ) );
}

/**
Expand All @@ -184,9 +194,11 @@ protected void addConstInstruction( Number value, int javaCodePos ) {
* the value type
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
*/
protected void addNumericInstruction( @Nullable NumericOperator numOp, @Nullable ValueType valueType, int javaCodePos ) {
instructions.add( new WasmNumericInstruction( numOp, valueType, javaCodePos ) );
protected void addNumericInstruction( @Nullable NumericOperator numOp, @Nullable ValueType valueType, int javaCodePos, int lineNumber ) {
instructions.add( new WasmNumericInstruction( numOp, valueType, javaCodePos, lineNumber ) );
}

/**
Expand All @@ -196,9 +208,11 @@ protected void addNumericInstruction( @Nullable NumericOperator numOp, @Nullable
* the conversion
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
*/
protected void addConvertInstruction( ValueTypeConvertion conversion, int javaCodePos ) {
instructions.add( new WasmConvertInstruction( conversion, javaCodePos ) );
protected void addConvertInstruction( ValueTypeConvertion conversion, int javaCodePos, int lineNumber ) {
instructions.add( new WasmConvertInstruction( conversion, javaCodePos, lineNumber ) );
}

/**
Expand All @@ -208,9 +222,11 @@ protected void addConvertInstruction( ValueTypeConvertion conversion, int javaCo
* the function name that should be called
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
*/
protected void addCallInstruction( FunctionName name, int javaCodePos ) {
instructions.add( new WasmCallInstruction( name, javaCodePos ) );
protected void addCallInstruction( FunctionName name, int javaCodePos, int lineNumber ) {
instructions.add( new WasmCallInstruction( name, javaCodePos, lineNumber ) );
}

/**
Expand All @@ -222,9 +238,11 @@ protected void addCallInstruction( FunctionName name, int javaCodePos ) {
* extra data for some operations
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
*/
protected void addBlockInstruction( WasmBlockOperator op, @Nullable Object data, int javaCodePos ) {
instructions.add( new WasmBlockInstruction( op, data, javaCodePos ) );
protected void addBlockInstruction( WasmBlockOperator op, @Nullable Object data, int javaCodePos, int lineNumber ) {
instructions.add( new WasmBlockInstruction( op, data, javaCodePos, lineNumber ) );
}

/**
Expand All @@ -233,9 +251,11 @@ protected void addBlockInstruction( WasmBlockOperator op, @Nullable Object data,
*
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
*/
protected void addNopInstruction( int javaCodePos ) {
instructions.add( new WasmNopInstruction( javaCodePos ) );
protected void addNopInstruction( int javaCodePos, int lineNumber ) {
instructions.add( new WasmNopInstruction( javaCodePos, lineNumber ) );
}

/**
Expand All @@ -247,9 +267,11 @@ protected void addNopInstruction( int javaCodePos ) {
* the array type
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
*/
protected void addArrayInstruction( ArrayOperator op, AnyType type, int javaCodePos ) {
instructions.add( new WasmArrayInstruction( op, type, javaCodePos ) );
protected void addArrayInstruction( ArrayOperator op, AnyType type, int javaCodePos, int lineNumber ) {
instructions.add( new WasmArrayInstruction( op, type, javaCodePos, lineNumber ) );
}

/**
Expand All @@ -263,8 +285,10 @@ protected void addArrayInstruction( ArrayOperator op, AnyType type, int javaCode
* the name of field if needed for the operation
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
*/
protected void addStructInstruction( StructOperator op, @Nullable String typeName, @Nullable String fieldName, int javaCodePos ) {
instructions.add( new WasmStructInstruction( op, typeName == null ? null : types.valueOf( typeName ), fieldName, javaCodePos ) );
protected void addStructInstruction( StructOperator op, @Nullable String typeName, @Nullable String fieldName, int javaCodePos, int lineNumber ) {
instructions.add( new WasmStructInstruction( op, typeName == null ? null : types.valueOf( typeName ), fieldName, javaCodePos, lineNumber ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import javax.annotation.Nonnull;

import de.inetsoftware.jwebassembly.WasmException;
import de.inetsoftware.jwebassembly.module.WasmInstruction.Type;
import de.inetsoftware.jwebassembly.wasm.AnyType;
import de.inetsoftware.jwebassembly.wasm.ValueType;

Expand All @@ -46,9 +45,11 @@ class WasmConstInstruction extends WasmInstruction {
* the data type of the number
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
*/
WasmConstInstruction( Number value, ValueType valueType, int javaCodePos ) {
super( javaCodePos );
WasmConstInstruction( Number value, ValueType valueType, int javaCodePos, int lineNumber ) {
super( javaCodePos, lineNumber );
this.value = value;
this.valueType = valueType;
}
Expand All @@ -60,9 +61,11 @@ class WasmConstInstruction extends WasmInstruction {
* the constant value
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
*/
WasmConstInstruction( Number value, int javaCodePos ) {
this( value, getValueType( value ), javaCodePos );
WasmConstInstruction( Number value, int javaCodePos, int lineNumber ) {
this( value, getValueType( value ), javaCodePos, lineNumber );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.io.IOException;

import de.inetsoftware.jwebassembly.module.WasmInstruction.Type;
import de.inetsoftware.jwebassembly.wasm.AnyType;
import de.inetsoftware.jwebassembly.wasm.ValueType;

Expand All @@ -38,9 +37,11 @@ class WasmConvertInstruction extends WasmInstruction {
* the conversion type
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
*/
WasmConvertInstruction( ValueTypeConvertion conversion, int javaCodePos ) {
super( javaCodePos );
WasmConvertInstruction( ValueTypeConvertion conversion, int javaCodePos, int lineNumber ) {
super( javaCodePos, lineNumber );
this.conversion = conversion;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import javax.annotation.Nonnull;

import de.inetsoftware.classparser.Member;
import de.inetsoftware.jwebassembly.module.WasmInstruction.Type;
import de.inetsoftware.jwebassembly.wasm.AnyType;
import de.inetsoftware.jwebassembly.wasm.ValueType;

Expand All @@ -46,9 +45,11 @@ class WasmGlobalInstruction extends WasmInstruction {
* reference to a static field
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
*/
WasmGlobalInstruction( boolean load, Member ref, int javaCodePos ) {
super( javaCodePos );
WasmGlobalInstruction( boolean load, Member ref, int javaCodePos, int lineNumber ) {
super( javaCodePos, lineNumber );
this.load = load;
this.ref = ref;
}
Expand Down
Loading

0 comments on commit 5e40f16

Please sign in to comment.