Skip to content

Commit 91cfd41

Browse files
committed
py: Implement native load for viper.
Viper can now do: ptr8(buf)[0], which loads a byte from a buffer using machine instructions.
1 parent 1ef2348 commit 91cfd41

File tree

11 files changed

+246
-33
lines changed

11 files changed

+246
-33
lines changed

py/asmarm.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,21 @@ void asm_arm_asr_reg_reg(asm_arm_t *as, uint rd, uint rs) {
357357
emit_al(as, 0x1a00050 | (rd << 12) | (rs << 8) | rd);
358358
}
359359

360+
void asm_arm_ldr_reg_reg(asm_arm_t *as, uint rd, uint rn) {
361+
// ldr rd, [rn]
362+
emit_al(as, 0x5900000 | (rn << 16) | (rd << 12));
363+
}
364+
365+
void asm_arm_ldrh_reg_reg(asm_arm_t *as, uint rd, uint rn) {
366+
// ldrh rd, [rn]
367+
emit_al(as, 0x1d000b0 | (rn << 16) | (rd << 12));
368+
}
369+
370+
void asm_arm_ldrb_reg_reg(asm_arm_t *as, uint rd, uint rn) {
371+
// ldrb rd, [rn]
372+
emit_al(as, 0x5d00000 | (rn << 16) | (rd << 12));
373+
}
374+
360375
void asm_arm_str_reg_reg(asm_arm_t *as, uint rd, uint rm) {
361376
// str rd, [rm]
362377
emit_al(as, 0x5800000 | (rm << 16) | (rd << 12));

py/asmarm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ void asm_arm_lsl_reg_reg(asm_arm_t *as, uint rd, uint rs);
104104
void asm_arm_asr_reg_reg(asm_arm_t *as, uint rd, uint rs);
105105

106106
// memory
107+
void asm_arm_ldr_reg_reg(asm_arm_t *as, uint rd, uint rn);
108+
void asm_arm_ldrh_reg_reg(asm_arm_t *as, uint rd, uint rn);
109+
void asm_arm_ldrb_reg_reg(asm_arm_t *as, uint rd, uint rn);
107110
void asm_arm_str_reg_reg(asm_arm_t *as, uint rd, uint rm);
108111
void asm_arm_strh_reg_reg(asm_arm_t *as, uint rd, uint rm);
109112
void asm_arm_strb_reg_reg(asm_arm_t *as, uint rd, uint rm);

py/asmx64.c

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@
5151
#define OPCODE_MOV_I32_TO_RM32 (0xc7)
5252
#define OPCODE_MOV_R8_TO_RM8 (0x88) /* /r */
5353
#define OPCODE_MOV_R64_TO_RM64 (0x89) /* /r */
54-
#define OPCODE_MOV_RM64_TO_R64 (0x8b)
54+
#define OPCODE_MOV_RM64_TO_R64 (0x8b) /* /r */
55+
#define OPCODE_MOVZX_RM8_TO_R64 (0xb6) /* 0x0f 0xb6/r */
56+
#define OPCODE_MOVZX_RM16_TO_R64 (0xb7) /* 0x0f 0xb7/r */
5557
#define OPCODE_LEA_MEM_TO_R64 (0x8d) /* /r */
5658
#define OPCODE_AND_R64_TO_RM64 (0x21) /* /r */
5759
#define OPCODE_OR_R64_TO_RM64 (0x09) /* /r */
@@ -302,7 +304,7 @@ void asm_x64_mov_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
302304
asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_MOV_R64_TO_RM64);
303305
}
304306

305-
void asm_x64_mov_r8_to_disp(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
307+
void asm_x64_mov_r8_to_mem8(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
306308
assert(dest_r64 < 8);
307309
if (src_r64 < 8) {
308310
asm_x64_write_byte_1(as, OPCODE_MOV_R8_TO_RM8);
@@ -312,7 +314,7 @@ void asm_x64_mov_r8_to_disp(asm_x64_t *as, int src_r64, int dest_r64, int dest_d
312314
asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
313315
}
314316

315-
void asm_x64_mov_r16_to_disp(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
317+
void asm_x64_mov_r16_to_mem16(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
316318
assert(dest_r64 < 8);
317319
if (src_r64 < 8) {
318320
asm_x64_write_byte_2(as, OP_SIZE_PREFIX, OPCODE_MOV_R64_TO_RM64);
@@ -322,14 +324,34 @@ void asm_x64_mov_r16_to_disp(asm_x64_t *as, int src_r64, int dest_r64, int dest_
322324
asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
323325
}
324326

325-
void asm_x64_mov_r64_to_disp(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
327+
void asm_x64_mov_r64_to_mem64(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
326328
// use REX prefix for 64 bit operation
327329
assert(dest_r64 < 8);
328330
asm_x64_write_byte_2(as, REX_PREFIX | REX_W | (src_r64 < 8 ? 0 : REX_R), OPCODE_MOV_R64_TO_RM64);
329331
asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
330332
}
331333

332-
void asm_x64_mov_disp_to_r64(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) {
334+
void asm_x64_mov_mem8_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) {
335+
assert(src_r64 < 8);
336+
if (dest_r64 < 8) {
337+
asm_x64_write_byte_2(as, 0x0f, OPCODE_MOVZX_RM8_TO_R64);
338+
} else {
339+
asm_x64_write_byte_3(as, REX_PREFIX | REX_R, 0x0f, OPCODE_MOVZX_RM8_TO_R64);
340+
}
341+
asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
342+
}
343+
344+
void asm_x64_mov_mem16_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) {
345+
assert(src_r64 < 8);
346+
if (dest_r64 < 8) {
347+
asm_x64_write_byte_2(as, 0x0f, OPCODE_MOVZX_RM16_TO_R64);
348+
} else {
349+
asm_x64_write_byte_3(as, REX_PREFIX | REX_R, 0x0f, OPCODE_MOVZX_RM16_TO_R64);
350+
}
351+
asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
352+
}
353+
354+
void asm_x64_mov_mem64_to_r64(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) {
333355
// use REX prefix for 64 bit operation
334356
assert(src_r64 < 8);
335357
asm_x64_write_byte_2(as, REX_PREFIX | REX_W | (dest_r64 < 8 ? 0 : REX_R), OPCODE_MOV_RM64_TO_R64);
@@ -587,11 +609,11 @@ STATIC int asm_x64_local_offset_from_ebp(asm_x64_t *as, int local_num) {
587609
}
588610

589611
void asm_x64_mov_local_to_r64(asm_x64_t *as, int src_local_num, int dest_r64) {
590-
asm_x64_mov_disp_to_r64(as, ASM_X64_REG_RBP, asm_x64_local_offset_from_ebp(as, src_local_num), dest_r64);
612+
asm_x64_mov_mem64_to_r64(as, ASM_X64_REG_RBP, asm_x64_local_offset_from_ebp(as, src_local_num), dest_r64);
591613
}
592614

593615
void asm_x64_mov_r64_to_local(asm_x64_t *as, int src_r64, int dest_local_num) {
594-
asm_x64_mov_r64_to_disp(as, src_r64, ASM_X64_REG_RBP, asm_x64_local_offset_from_ebp(as, dest_local_num));
616+
asm_x64_mov_r64_to_mem64(as, src_r64, ASM_X64_REG_RBP, asm_x64_local_offset_from_ebp(as, dest_local_num));
595617
}
596618

597619
void asm_x64_mov_local_addr_to_r64(asm_x64_t *as, int local_num, int dest_r64) {

py/asmx64.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,12 @@ void asm_x64_mov_r64_r64(asm_x64_t* as, int dest_r64, int src_r64);
8383
void asm_x64_mov_i64_to_r64(asm_x64_t* as, int64_t src_i64, int dest_r64);
8484
void asm_x64_mov_i64_to_r64_optimised(asm_x64_t *as, int64_t src_i64, int dest_r64);
8585
void asm_x64_mov_i64_to_r64_aligned(asm_x64_t *as, int64_t src_i64, int dest_r64);
86-
void asm_x64_mov_r8_to_disp(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp);
87-
void asm_x64_mov_r16_to_disp(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp);
88-
void asm_x64_mov_r64_to_disp(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp);
86+
void asm_x64_mov_r8_to_mem8(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp);
87+
void asm_x64_mov_r16_to_mem16(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp);
88+
void asm_x64_mov_r64_to_mem64(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp);
89+
void asm_x64_mov_mem8_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64);
90+
void asm_x64_mov_mem16_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64);
91+
void asm_x64_mov_mem64_to_r64(asm_x64_t *as, int src_r64, int src_disp, int dest_r64);
8992
void asm_x64_and_r64_r64(asm_x64_t *as, int dest_r64, int src_r64);
9093
void asm_x64_or_r64_r64(asm_x64_t *as, int dest_r64, int src_r64);
9194
void asm_x64_xor_r64_r64(asm_x64_t *as, int dest_r64, int src_r64);

py/asmx86.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@
5050
#define OPCODE_MOV_I32_TO_R32 (0xb8)
5151
//#define OPCODE_MOV_I32_TO_RM32 (0xc7)
5252
#define OPCODE_MOV_R8_TO_RM8 (0x88) /* /r */
53-
#define OPCODE_MOV_R32_TO_RM32 (0x89)
54-
#define OPCODE_MOV_RM32_TO_R32 (0x8b)
53+
#define OPCODE_MOV_R32_TO_RM32 (0x89) /* /r */
54+
#define OPCODE_MOV_RM32_TO_R32 (0x8b) /* /r */
55+
#define OPCODE_MOVZX_RM8_TO_R32 (0xb6) /* 0x0f 0xb6/r */
56+
#define OPCODE_MOVZX_RM16_TO_R32 (0xb7) /* 0x0f 0xb7/r */
5557
#define OPCODE_LEA_MEM_TO_R32 (0x8d) /* /r */
5658
#define OPCODE_AND_R32_TO_RM32 (0x21) /* /r */
5759
#define OPCODE_OR_R32_TO_RM32 (0x09) /* /r */
@@ -244,22 +246,32 @@ void asm_x86_mov_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
244246
asm_x86_generic_r32_r32(as, dest_r32, src_r32, OPCODE_MOV_R32_TO_RM32);
245247
}
246248

247-
void asm_x86_mov_r8_to_disp(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp) {
249+
void asm_x86_mov_r8_to_mem8(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp) {
248250
asm_x86_write_byte_1(as, OPCODE_MOV_R8_TO_RM8);
249251
asm_x86_write_r32_disp(as, src_r32, dest_r32, dest_disp);
250252
}
251253

252-
void asm_x86_mov_r16_to_disp(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp) {
254+
void asm_x86_mov_r16_to_mem16(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp) {
253255
asm_x86_write_byte_2(as, OP_SIZE_PREFIX, OPCODE_MOV_R32_TO_RM32);
254256
asm_x86_write_r32_disp(as, src_r32, dest_r32, dest_disp);
255257
}
256258

257-
void asm_x86_mov_r32_to_disp(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp) {
259+
void asm_x86_mov_r32_to_mem32(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp) {
258260
asm_x86_write_byte_1(as, OPCODE_MOV_R32_TO_RM32);
259261
asm_x86_write_r32_disp(as, src_r32, dest_r32, dest_disp);
260262
}
261263

262-
STATIC void asm_x86_mov_disp_to_r32(asm_x86_t *as, int src_r32, int src_disp, int dest_r32) {
264+
void asm_x86_mov_mem8_to_r32zx(asm_x86_t *as, int src_r32, int src_disp, int dest_r32) {
265+
asm_x86_write_byte_2(as, 0x0f, OPCODE_MOVZX_RM8_TO_R32);
266+
asm_x86_write_r32_disp(as, dest_r32, src_r32, src_disp);
267+
}
268+
269+
void asm_x86_mov_mem16_to_r32zx(asm_x86_t *as, int src_r32, int src_disp, int dest_r32) {
270+
asm_x86_write_byte_2(as, 0x0f, OPCODE_MOVZX_RM16_TO_R32);
271+
asm_x86_write_r32_disp(as, dest_r32, src_r32, src_disp);
272+
}
273+
274+
void asm_x86_mov_mem32_to_r32(asm_x86_t *as, int src_r32, int src_disp, int dest_r32) {
263275
asm_x86_write_byte_1(as, OPCODE_MOV_RM32_TO_R32);
264276
asm_x86_write_r32_disp(as, dest_r32, src_r32, src_disp);
265277
}
@@ -474,12 +486,12 @@ void asm_x86_push_arg(asm_x86_t *as, int src_arg_num) {
474486
#endif
475487

476488
void asm_x86_mov_arg_to_r32(asm_x86_t *as, int src_arg_num, int dest_r32) {
477-
asm_x86_mov_disp_to_r32(as, ASM_X86_REG_EBP, 2 * WORD_SIZE + src_arg_num * WORD_SIZE, dest_r32);
489+
asm_x86_mov_mem32_to_r32(as, ASM_X86_REG_EBP, 2 * WORD_SIZE + src_arg_num * WORD_SIZE, dest_r32);
478490
}
479491

480492
#if 0
481493
void asm_x86_mov_r32_to_arg(asm_x86_t *as, int src_r32, int dest_arg_num) {
482-
asm_x86_mov_r32_to_disp(as, src_r32, ASM_X86_REG_EBP, 2 * WORD_SIZE + dest_arg_num * WORD_SIZE);
494+
asm_x86_mov_r32_to_mem32(as, src_r32, ASM_X86_REG_EBP, 2 * WORD_SIZE + dest_arg_num * WORD_SIZE);
483495
}
484496
#endif
485497

@@ -499,11 +511,11 @@ STATIC int asm_x86_local_offset_from_ebp(asm_x86_t *as, int local_num) {
499511
}
500512

501513
void asm_x86_mov_local_to_r32(asm_x86_t *as, int src_local_num, int dest_r32) {
502-
asm_x86_mov_disp_to_r32(as, ASM_X86_REG_EBP, asm_x86_local_offset_from_ebp(as, src_local_num), dest_r32);
514+
asm_x86_mov_mem32_to_r32(as, ASM_X86_REG_EBP, asm_x86_local_offset_from_ebp(as, src_local_num), dest_r32);
503515
}
504516

505517
void asm_x86_mov_r32_to_local(asm_x86_t *as, int src_r32, int dest_local_num) {
506-
asm_x86_mov_r32_to_disp(as, src_r32, ASM_X86_REG_EBP, asm_x86_local_offset_from_ebp(as, dest_local_num));
518+
asm_x86_mov_r32_to_mem32(as, src_r32, ASM_X86_REG_EBP, asm_x86_local_offset_from_ebp(as, dest_local_num));
507519
}
508520

509521
void asm_x86_mov_local_addr_to_r32(asm_x86_t *as, int local_num, int dest_r32) {

py/asmx86.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,12 @@ void* asm_x86_get_code(asm_x86_t* as);
8080
void asm_x86_mov_r32_r32(asm_x86_t* as, int dest_r32, int src_r32);
8181
void asm_x86_mov_i32_to_r32(asm_x86_t *as, int32_t src_i32, int dest_r32);
8282
void asm_x86_mov_i32_to_r32_aligned(asm_x86_t *as, int32_t src_i32, int dest_r32);
83-
void asm_x86_mov_r8_to_disp(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp);
84-
void asm_x86_mov_r16_to_disp(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp);
85-
void asm_x86_mov_r32_to_disp(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp);
83+
void asm_x86_mov_r8_to_mem8(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp);
84+
void asm_x86_mov_r16_to_mem16(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp);
85+
void asm_x86_mov_r32_to_mem32(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp);
86+
void asm_x86_mov_mem8_to_r32zx(asm_x86_t *as, int src_r32, int src_disp, int dest_r32);
87+
void asm_x86_mov_mem16_to_r32zx(asm_x86_t *as, int src_r32, int src_disp, int dest_r32);
88+
void asm_x86_mov_mem32_to_r32(asm_x86_t *as, int src_r32, int src_disp, int dest_r32);
8689
void asm_x86_and_r32_r32(asm_x86_t *as, int dest_r32, int src_r32);
8790
void asm_x86_or_r32_r32(asm_x86_t *as, int dest_r32, int src_r32);
8891
void asm_x86_xor_r32_r32(asm_x86_t *as, int dest_r32, int src_r32);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy