Skip to content

Commit

Permalink
update patch_test_bytecode for forc test
Browse files Browse the repository at this point in the history
  • Loading branch information
vaivaswatha committed Sep 11, 2024
1 parent 6f5c1b2 commit 1b99c89
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
24 changes: 16 additions & 8 deletions forc-test/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,25 @@ impl TestExecutor {
/// The following is how the beginning of the bytecode is laid out:
///
/// ```ignore
/// [0] ji i4 ; Jumps to the data section setup.
/// [1] noop
/// [2] DATA_SECTION_OFFSET[0..32]
/// [3] DATA_SECTION_OFFSET[32..64]
/// [4] lw $ds $is 1 ; The data section setup, i.e. where the first ji lands.
/// [5] add $$ds $$ds $is
/// [6] <first-entry-point> ; This is where we want to jump from to our test code!
/// [ 0] ji i4 ; Jumps to the data section setup.
/// [ 1] noop
/// [ 2] DATA_SECTION_OFFSET[0..32]
/// [ 3] DATA_SECTION_OFFSET[32..64]
/// [ 4] METADATA (0-32)
/// [ 5] METADATA (32-64)
/// [ 6] METADATA (64-96)
/// [ 7] METADATA (96-128)
/// [ 8] METADATA (128-160)
/// [ 9] METADATA (160-192)
/// [10] METADATA (192-224)
/// [11] METADATA (224-256)
/// [12] lw $ds $is 1 ; The data section setup, i.e. where the first ji lands.
/// [13] add $$ds $$ds $is
/// [14] <first-entry-point> ; This is where we want to jump from to our test code!
/// ```
fn patch_test_bytecode(bytecode: &[u8], test_offset: u32) -> std::borrow::Cow<[u8]> {
// TODO: Standardize this or add metadata to bytecode.
const PROGRAM_START_INST_OFFSET: u32 = 6;
const PROGRAM_START_INST_OFFSET: u32 = 14;
const PROGRAM_START_BYTE_OFFSET: usize = PROGRAM_START_INST_OFFSET as usize * Instruction::SIZE;

// If our desired entry point is the program start, no need to jump.
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/asm_generation/fuel/programs/abstract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl AbstractProgram {
///
/// WORD OP
/// 1 MOV $scratch $pc
/// - JMPF $zero i4
/// - JMPF $zero i10
/// 2 DATA_START (0-32) (in bytes, offset from $is)
/// - DATA_START (32-64)
/// 3 METADATA (0-32)
Expand Down
1 change: 1 addition & 0 deletions sway-core/src/asm_lang/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,7 @@ impl fmt::Display for VirtualOp {
/* Non-VM Instructions */
BLOB(a) => write!(fmtr, "blob {a}"),
DataSectionOffsetPlaceholder => write!(fmtr, "data section offset placeholder"),
Metadata => write!(fmtr, "Metadata"),
LoadDataId(a, b) => write!(fmtr, "load {a} {b}"),
AddrDataId(a, b) => write!(fmtr, "addr {a} {b}"),
Undefined => write!(fmtr, "undefined op"),
Expand Down
8 changes: 8 additions & 0 deletions sway-core/src/asm_lang/virtual_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ pub(crate) enum VirtualOp {

/* Non-VM Instructions */
BLOB(VirtualImmediate24),
Metadata,
DataSectionOffsetPlaceholder,
// LoadDataId takes a virtual register and a DataId, which points to a labeled piece
// of data in the data section. Note that the ASM op corresponding to a LW is
Expand Down Expand Up @@ -347,6 +348,7 @@ impl VirtualOp {
/* Non-VM Instructions */
BLOB(_imm) => vec![],
DataSectionOffsetPlaceholder => vec![],
Metadata => vec![],
LoadDataId(r1, _i) => vec![r1],
AddrDataId(r1, _) => vec![r1],

Expand Down Expand Up @@ -465,6 +467,7 @@ impl VirtualOp {
// Virtual OPs
| BLOB(_)
| DataSectionOffsetPlaceholder
| Metadata
| Undefined => true
}
}
Expand Down Expand Up @@ -571,6 +574,7 @@ impl VirtualOp {
| GTF(_, _, _)
| BLOB(_)
| DataSectionOffsetPlaceholder
| Metadata
| LoadDataId(_, _)
| AddrDataId(_, _)
| Undefined => vec![],
Expand Down Expand Up @@ -692,6 +696,7 @@ impl VirtualOp {
/* Non-VM Instructions */
BLOB(_imm) => vec![],
DataSectionOffsetPlaceholder => vec![],
Metadata => vec![],
LoadDataId(_r1, _i) => vec![],
AddrDataId(_r1, _i) => vec![],

Expand Down Expand Up @@ -815,6 +820,7 @@ impl VirtualOp {
LoadDataId(r1, _i) => vec![r1],
AddrDataId(r1, _i) => vec![r1],
DataSectionOffsetPlaceholder => vec![],
Metadata => vec![],
Undefined => vec![],
})
.into_iter()
Expand Down Expand Up @@ -1263,6 +1269,7 @@ impl VirtualOp {
/* Non-VM Instructions */
BLOB(i) => Self::BLOB(i.clone()),
DataSectionOffsetPlaceholder => Self::DataSectionOffsetPlaceholder,
Metadata => Self::Metadata,
LoadDataId(r1, i) => Self::LoadDataId(update_reg(reg_to_reg_map, r1), i.clone()),
AddrDataId(r1, i) => Self::AddrDataId(update_reg(reg_to_reg_map, r1), i.clone()),
Undefined => Self::Undefined,
Expand Down Expand Up @@ -1743,6 +1750,7 @@ impl VirtualOp {
/* Non-VM Instructions */
BLOB(imm) => AllocatedOpcode::BLOB(imm.clone()),
DataSectionOffsetPlaceholder => AllocatedOpcode::DataSectionOffsetPlaceholder,
Metadata => AllocatedOpcode::Metadata,
LoadDataId(reg1, label) => {
AllocatedOpcode::LoadDataId(map_reg(&mapping, reg1), label.clone())
}
Expand Down

0 comments on commit 1b99c89

Please sign in to comment.