Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i#6662 public traces, part 1: synthetic ISA #6691

Merged
merged 95 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
f9bebe0
i#6662 synthetic ISA: encoding
edeiana Feb 28, 2024
4b70a9d
Forgot to include encode header for synthetic ISA.
edeiana Feb 28, 2024
d301f3e
Added synthetic encoding header to CMakeList.
edeiana Feb 29, 2024
a0bbf6f
Added initial implementation of synthetic encoding/decoding.
edeiana Mar 6, 2024
8b29dcc
Using x86 encode/decode for synthetic encoding/decoding
edeiana Mar 6, 2024
d2115e6
Added test for synthetic encoding/decoding.
edeiana Mar 6, 2024
44c4c3c
Merge branch 'master' into i6662-synthetic-isa
edeiana Mar 6, 2024
246003c
Added encoding/deconding of operands, although virtual registers
edeiana Mar 15, 2024
85ac64d
Fixed description of synthetic ISA mode.
edeiana Mar 15, 2024
79b736b
Updated uses of spelled out DR_ISA_SYNTHETIC.
edeiana Mar 15, 2024
9e67807
Merge remote-tracking branch 'origin/master' into i6662-synthetic-isa
edeiana Mar 15, 2024
1e8a686
Updated synthetic ISA enum use.
edeiana Mar 15, 2024
8bc947a
Updated test for synthetic ISA encoding/decoding.
edeiana Mar 15, 2024
df82fcf
Now returning next instruction's PC after synthetic ISA
edeiana Mar 15, 2024
21e8d77
Improved description in comment.
edeiana Mar 15, 2024
b3cac08
Fixed building error.
edeiana Mar 15, 2024
89e80d7
clang-format.
edeiana Mar 15, 2024
b4b1fd8
Addressed memory alignment, eflags, constants, description of encoding.
edeiana Mar 18, 2024
8df36c1
Fixed minosr issue in Synthetic ISA test: merged lines.
edeiana Mar 18, 2024
dc5f750
Setting instr_t length according to the size of the synthetic
edeiana Mar 20, 2024
8400177
Now considering registers in destination operands that are memory
edeiana Mar 20, 2024
d346c27
Improved test.
edeiana Mar 20, 2024
8d63bbf
Check for instr_t synthetic ISA moved to instr_encode()
edeiana Mar 20, 2024
4787fad
We want to minimize the impact of synthetic ISA.
edeiana Mar 20, 2024
978ed90
Now setting arithetic, operand, and raw bits flags valid after decoding.
edeiana Mar 20, 2024
efc6cf2
We don't need to temporarly change instr_t ISA mode to retrieve flags
edeiana Mar 20, 2024
46bacdb
Documented that we use instr_t ISA mode for decoding when
edeiana Mar 20, 2024
9c96008
Now checking flags, instr length, and PC.
edeiana Mar 20, 2024
615d55f
Fixed "warning C4018: '<': signed/unsigned mismatch" treated
edeiana Mar 20, 2024
df3c68d
Fixed __attribute__ extention not supported by our windows compiler.
edeiana Mar 20, 2024
dd5a979
Whoops, forgot to zero-out the first 4 bytes before encoding.
edeiana Mar 20, 2024
d4f1b3e
Improved comments.
edeiana Mar 21, 2024
7a95872
Now using dcontext ISA mode for decoding in x86.
edeiana Mar 21, 2024
806a72c
Moved encode_to_synth() from instr_encode() to instr_encode_to_copy().
edeiana Mar 21, 2024
e13b25b
Using local variable for header bytes of encoding, instead of
edeiana Mar 21, 2024
ef8560c
Improved description.
edeiana Mar 21, 2024
ab65804
Addressed PR comments.
edeiana Mar 21, 2024
b1bd6d5
Fixed grammar.
edeiana Mar 21, 2024
23995d3
Added decode_from_synth() into decode() of every architecture.
edeiana Mar 22, 2024
b1f844a
Removed synthetic ISA related tests from ir_x86.c set of tests.
edeiana Mar 22, 2024
ce3c5e1
Fixed AARCH64 and RISCV64 tests.
edeiana Mar 22, 2024
0e318dd
Added DR_ISA_SYNTHETIC to is_isa_mode_legal() for all arches.
edeiana Mar 22, 2024
224fadf
Removed decode_from_synth() from decode_from_copy(), we only want it in
edeiana Mar 22, 2024
ddf7b10
Now allowing DR_ISA_SYNTHETIC for all arches in instr_set_isa_mode().
edeiana Mar 22, 2024
363ef2e
Fixed ARM 32 test.
edeiana Mar 22, 2024
c29f344
Addressed misc (mostly minor) review comments.
edeiana Mar 23, 2024
1a2a6b1
Mapping sub-registers to their canonical register (i.e., the largest …
edeiana Mar 24, 2024
035d014
Fixed register size encoding.
edeiana Mar 24, 2024
6d4d4cf
Check if src or dst operands are present before looping through
edeiana Mar 24, 2024
96133f8
Biggest encoded synthetis instruction is now 20 bytes.
edeiana Mar 24, 2024
91981df
Fixed offset when encoding/decoding src operands.
edeiana Mar 24, 2024
bc22a59
Moved decode_from_synth() from decode() to decode_common() for all
edeiana Mar 26, 2024
2856ab0
Now we only have a single operation size field (1 byte encoding).
edeiana Mar 26, 2024
70f6942
Fix building errors for aarch64 and riscv64.
edeiana Mar 26, 2024
4caf3aa
Fix failure on AARCH64.
edeiana Mar 27, 2024
9bc4a80
Fix building error (typo).
edeiana Mar 27, 2024
f88f9b2
Renamed DR_ISA_SYNTHETIC to DR_ISA_REGDEPS.
edeiana Mar 27, 2024
a9ff2d5
Forgot one DR_ISA_SYNTHETIC in aarch64. Fixed.
edeiana Mar 27, 2024
5cd8856
Added instr_convert_to_isa_regdeps() API to translate an instr_t
edeiana Mar 29, 2024
cc34ec5
Added new API to release doc.
edeiana Mar 29, 2024
87d60ab
Updated tests.
edeiana Mar 29, 2024
ef7554c
Minor cleanup.
edeiana Mar 29, 2024
9e7c7f8
Merge branch 'master' into i6662-synthetic-isa
edeiana Mar 29, 2024
3d0106f
clang-format-14 run.
edeiana Mar 29, 2024
4553779
Fixed warning as error on windows.
edeiana Mar 29, 2024
55f737c
Added #mem_ops (i.e., loads + stores) to encoding.
edeiana Mar 31, 2024
f05c367
Added a couple more x86 instructions to test with implicit
edeiana Mar 31, 2024
2374332
Increased size of array containing encoding to max encoding size of 16
edeiana Mar 31, 2024
1b01ed5
Reverted back to no #mem_ops.
edeiana Apr 2, 2024
28b779a
Added DR_ISA_REGDEPS description.
edeiana Apr 4, 2024
c284c6b
Changed convert_to_isa_regdeps() signature.
edeiana Apr 4, 2024
9d785e1
Removed unnecessary cast to (void *).
edeiana Apr 4, 2024
a7149e9
Renaming: from synthetic to isa_regdeps.
edeiana Apr 5, 2024
547d39b
Renaming: encoding/decoding functions using isa_regdeps name.
edeiana Apr 5, 2024
2a0ada8
Added operation_size, modified encoding/decoding accordingly.
edeiana Apr 6, 2024
0b58ad7
Improved description of encoding scheme and its readability.
edeiana Apr 6, 2024
e67df8a
Improved test.
edeiana Apr 6, 2024
d41e21e
Updated documentation.
edeiana Apr 7, 2024
8db9a06
Fixed typo in documentation.
edeiana Apr 7, 2024
4615d17
clang-format-14 run.
edeiana Apr 7, 2024
8d08c07
clang-format-14 run, again.
edeiana Apr 7, 2024
cefaba5
Added encode + decode of instructions generated by INSTR_CREATE_.
edeiana Apr 7, 2024
2b0ad5e
Removed INSTR_CREATE_adr from tests for aarch64 because of i#4847.
edeiana Apr 7, 2024
d125010
Removed setting of instr_t.length, we don't use it anymore,
edeiana Apr 7, 2024
be82357
Updated test's expected output.
edeiana Apr 7, 2024
e639309
Added instr_encode_common() with is now the common routine interposed
edeiana Apr 8, 2024
b29d8a1
Added valid encoding to bytes field of instr_t when converting and
edeiana Apr 9, 2024
74c681f
Formatting fixed.
edeiana Apr 9, 2024
b931392
Addressed PR feedback.
edeiana Apr 9, 2024
1340b90
Fromatting fixed.
edeiana Apr 9, 2024
0d57ec9
Removed unnecessary header.
edeiana Apr 9, 2024
1c828b8
Improved comments and code documentation (doxygen).
edeiana Apr 9, 2024
5ad7405
Merge branch 'master' into i6662-synthetic-isa
edeiana Apr 9, 2024
2aad8e0
Fixed doxygen. Cannot reference #DR_ISA_REGDEPS in a pre-comment for
edeiana Apr 9, 2024
01ef05c
Trying to fix doxygen.
edeiana Apr 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ set(DECODER_SRCS
ir/${ARCH_NAME}/decode.c
ir/encode_shared.c
ir/${ARCH_NAME}/encode.c
ir/synthetic/encode.c
edeiana marked this conversation as resolved.
Show resolved Hide resolved
ir/synthetic/decode.c
ir/disassemble_shared.c
ir/${ARCH_NAME}/disassemble.c
ir/ir_utils_shared.c
Expand Down
2 changes: 2 additions & 0 deletions core/ir/encode_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ typedef enum _dr_isa_mode_t {
DR_ISA_ARM_THUMB, /**< Thumb (ARM T32). */
DR_ISA_ARM_A64, /**< ARM A64 (AArch64). */
DR_ISA_RV64IMAFDC, /**< RISC-V (rv64imafdc). */
DR_ISA_SYNTHETIC, /**< Synthetic ISA for storing
edeiana marked this conversation as resolved.
Show resolved Hide resolved
only register dependencies. */
} dr_isa_mode_t;

DR_API
Expand Down
139 changes: 139 additions & 0 deletions core/ir/synthetic/decode.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/* **********************************************************
* Copyright (c) 2024 Google, Inc. All rights reserved.
* **********************************************************/

/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of VMware, Inc. nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL VMWARE, INC. OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/

/* decode.c -- a decoder for DR synthetic IR */

#include "decode.h"

#include "../globals.h"
#include "encoding_common.h"
#include "instr_api.h"
#include "opnd_api.h"

/* Decodes the raw bytes of an encoded instruction \p encoded_instr into DR instruction
* representation \p instr.
* Returns next instruction's pc.
*
* The smallest encoded instruction (i.e., with no operands) has 4 bytes and follows
edeiana marked this conversation as resolved.
Show resolved Hide resolved
* this scheme:
* |----------------------| |--| |----| |----|
* 31.. ..10 9,8 7..4 3..0
* category eflags #src #dst
*
* 22 bits, category: holds values of type #dr_instr_category_t. Note that an instruction
* can belong to more than one category.
* 2 bits, eflags: most significant bit set to 1 indicates the instruction reads at least
* one arithmetic flag, least significant bit set to 1 indicates the instruction writes
* at least one arithmetic flag.
* 4 bits, #src: number of source operands (read) that are registers.
* 4 bits, #dst: number of destination operands (written) that are registers.
* (Note that we are only interested in register dependencies, hence operands that are
* not register, such as immediates or memory references, are not present)
edeiana marked this conversation as resolved.
Show resolved Hide resolved
*
* Instructions with operands can reach up to 14 bytes: the 4 bytes described above, and
edeiana marked this conversation as resolved.
Show resolved Hide resolved
* up to 10 register operands of 1 byte each.
edeiana marked this conversation as resolved.
Show resolved Hide resolved
* For example, an instruction with 4 operands (2 src, 2 dst) has 4 additional bytes that
* are encoded following this scheme:
* |--------| |--------| |--------| |--------|
* 31.. ..24 23.. ..16 15.. ..8 7.. ..0
* src_opnd0 src_opnd1 dst_opnd0 dst_opnd1
*
* We assume all encoded values to be little-endian.
edeiana marked this conversation as resolved.
Show resolved Hide resolved
*/
byte *
decode_from_synth(dcontext_t *dcontext, byte *encoded_instr, instr_t *instr)
{
uint encoding = 0;
uint shift = 0;

/* Copy the first 4 bytes of encoded_instr (which is always present) in a uint for
* easier retrieving of category, eflags, #src, #dst values.
*/
memcpy(&encoding, encoded_instr, sizeof(encoding));
edeiana marked this conversation as resolved.
Show resolved Hide resolved

/* Decode number of destination operands.
*/
uint num_dsts_mask = ((1U << NUM_OPND_BITS) - 1);
edeiana marked this conversation as resolved.
Show resolved Hide resolved
uint num_dsts = encoding & num_dsts_mask;
shift += NUM_OPND_BITS;

/* Decode number of source operands.
*/
uint num_srcs_mask = ((1U << NUM_OPND_BITS) - 1) << shift;
uint num_srcs = (encoding & num_srcs_mask) >> shift;
shift += NUM_OPND_BITS;

instr_set_num_opnds(dcontext, instr, num_dsts, num_srcs);

/* Decode flags.
*/
// TODO i#6662: need to set flags in instr_t.
edeiana marked this conversation as resolved.
Show resolved Hide resolved
// Following code commented to remove currently unused variables.
// uint flags_mask = ((1U << FLAGS_BITS) - 1) << shift;
// uint flags = (encoding & flags_mask) >> shift;
shift += FLAGS_BITS;

/* Decode synthetic opcode as instruction category.
*/
uint category_mask = ((1U << CATEGORY_BITS) - 1) << shift;
uint category = (encoding & category_mask) >> shift;
instr_set_category(instr, category);

/* Decode register source operands, if present.
*/
uint encoding_size = (uint)sizeof(encoding); // Initial 4 bytes offset.
edeiana marked this conversation as resolved.
Show resolved Hide resolved
for (uint i = 0; i < num_srcs; ++i) {
ushort src = (ushort)encoded_instr[i + encoding_size];
edeiana marked this conversation as resolved.
Show resolved Hide resolved
edeiana marked this conversation as resolved.
Show resolved Hide resolved
// TODO i#6662: need to add virtual registers.
// Right now using regular reg_id_t (which holds DR_REG_ values) from
// opnd_api.h.
opnd_t src_opnd = opnd_create_reg(src);
instr_set_src(instr, i, src_opnd);
}

/* Decode register destination operands, if present.
*/
for (uint i = 0; i < num_dsts; ++i) {
ushort dst = (uint)encoded_instr[i + encoding_size + num_srcs];
// TODO i#6662: need to add virtual registers.
// Right now using regular reg_id_t (which holds DR_REG_ values) from
// opnd_api.h.
opnd_t dst_opnd = opnd_create_reg(dst);
instr_set_dst(instr, i, dst_opnd);
}

/* Compute next instruction's pc as: current pc + encoded instruction size.
*/
byte *next_pc = encoded_instr + encoding_size + num_srcs + num_dsts;
edeiana marked this conversation as resolved.
Show resolved Hide resolved
edeiana marked this conversation as resolved.
Show resolved Hide resolved

edeiana marked this conversation as resolved.
Show resolved Hide resolved
return next_pc;
}
41 changes: 41 additions & 0 deletions core/ir/synthetic/decode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* **********************************************************
* Copyright (c) 2024 Google, Inc. All rights reserved.
* **********************************************************/

/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of VMware, Inc. nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL VMWARE, INC. OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/

#ifndef _SYNTHETIC_DECODE_H_
#define _SYNTHETIC_DECODE_H_ 1

#include "../globals.h"

byte *
decode_from_synth(dcontext_t *dcontext, byte *encoded_instr, instr_t *instr);

#endif /* _SYNTHETIC_DECODE_H_ */
132 changes: 132 additions & 0 deletions core/ir/synthetic/encode.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/* **********************************************************
* Copyright (c) 2024 Google, Inc. All rights reserved.
* **********************************************************/

/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of VMware, Inc. nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL VMWARE, INC. OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/

/* encode.c -- an encoder for DR synthetic IR */

#include "encode.h"

#include "../globals.h"
#include "encoding_common.h"
#include "instr_api.h"
#include "opnd_api.h"

/* Encodes DR instruction representation \p instr into raw bytes \p encoded_instr.
* Returns next instruction's pc.
* A description of the encoding scheme is provided in core/ir/synthetic/decode.c.
* We assume all encoded values to be little-endian.
*/
byte *
encode_to_synth(dcontext_t *dcontext, instr_t *instr, byte *encoded_instr)
{
uint encoding = 0;
uint shift = 0;

/* Encode number of destination operands.
*/
uint original_num_dsts = (uint)instr_num_dsts(instr);
uint num_dsts = 0;
for (uint i = 0; i < original_num_dsts; ++i) {
opnd_t dst_opnd = instr_get_dst(instr, i);
if (opnd_is_reg(dst_opnd))
++num_dsts;
}
encoding |= num_dsts;
shift += NUM_OPND_BITS;

/* Encode number of source operands.
*/
uint original_num_srcs = (uint)instr_num_srcs(instr);
uint num_srcs = 0;
for (uint i = 0; i < original_num_srcs; ++i) {
opnd_t src_opnd = instr_get_src(instr, i);
if (opnd_is_reg(src_opnd))
++num_srcs;
edeiana marked this conversation as resolved.
Show resolved Hide resolved
}
encoding |= (num_srcs << shift);
shift += NUM_OPND_BITS;

/* Encode flags.
*/
// TODO i#6662: retrieve whether arithmetic flags are used from instr_t.
edeiana marked this conversation as resolved.
Show resolved Hide resolved
// Currently assuming all instructions both read and write at least one arithmetic
// flag (both bits set to 1).
uint flags = 0x3;
encoding |= (flags << shift);
shift += FLAGS_BITS;

/* Encode category as synthetic opcode.
*/
uint category = instr_get_category(instr);
encoding |= (category << shift);

/* Copy encoding that is common to all instructions (initial 4 bytes) to output:
* encoded_instr.
*/
memcpy(encoded_instr, &encoding, sizeof(encoding));
edeiana marked this conversation as resolved.
Show resolved Hide resolved

/* Encode register source operands, if present.
*/
uint encoding_size = (uint)sizeof(encoding); // Initial 4 bytes offset.
edeiana marked this conversation as resolved.
Show resolved Hide resolved
uint src_reg_counter = 0;
for (uint i = 0; i < original_num_srcs; ++i) {
opnd_t src_opnd = instr_get_src(instr, i);
if (opnd_is_reg(src_opnd)) {
// TODO i#6662: need to add virtual registers.
// Right now using regular reg_id_t (which holds DR_REG_ values) from
// opnd_api.h.
reg_id_t reg = opnd_get_reg(src_opnd);
encoded_instr[src_reg_counter + encoding_size] = (byte)reg;
edeiana marked this conversation as resolved.
Show resolved Hide resolved
++src_reg_counter;
}
}

/* Decode register source operands, if present.
*/
uint dst_reg_counter = 0;
for (uint i = 0; i < original_num_dsts; ++i) {
opnd_t dst_opnd = instr_get_dst(instr, i);
if (opnd_is_reg(dst_opnd)) {
edeiana marked this conversation as resolved.
Show resolved Hide resolved
// TODO i#6662: need to add virtual registers.
// Right now using regular reg_id_t (which holds DR_REG_ values) from
// opnd_api.h.
reg_id_t reg = opnd_get_reg(dst_opnd);
encoded_instr[dst_reg_counter + encoding_size + num_srcs] = (byte)reg;
++dst_reg_counter;
}
}

/* Compute next instruction's pc as: current pc + encoded instruction size.
*/
byte *next_pc = encoded_instr + encoding_size + src_reg_counter + dst_reg_counter;
edeiana marked this conversation as resolved.
Show resolved Hide resolved
edeiana marked this conversation as resolved.
Show resolved Hide resolved

return next_pc;
}
41 changes: 41 additions & 0 deletions core/ir/synthetic/encode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* **********************************************************
* Copyright (c) 2024 Google, Inc. All rights reserved.
* **********************************************************/

/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of VMware, Inc. nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL VMWARE, INC. OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/

#ifndef _SYNTHETIC_ENCODE_H_
#define _SYNTHETIC_ENCODE_H_ 1

#include "../globals.h"

byte *
encode_to_synth(dcontext_t *dcontext, instr_t *instr, byte *encoded_instr);

#endif /* _SYNTHETIC_ENCODE_H_ */
Loading
Loading