From e03216ba8bbc7547b1451ed99497c0693446983b Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Tue, 8 Sep 2026 18:32:42 -0700 Subject: add "provide" words for all sizes of hex literal, and use them in amd64 Force-Push: yes Change-Id: Ibdc4e05f8c63f0b4b6b8e504f89e053e6cd3a912 --- amd64.e | 120 ++++++++++++++++++++++++++++++++++++++++-------------------- transform.e | 60 ++++++++++++++++++++++++++---- 2 files changed, 132 insertions(+), 48 deletions(-) diff --git a/amd64.e b/amd64.e index d2fa2cd..e11ec2e 100644 --- a/amd64.e +++ b/amd64.e @@ -591,8 +591,10 @@ s" :cc-greater" keyword ~ (output point, immediate value -- output point) : push-imm32-extended64 - ~ : 5 push-imm32-extended64 - swap 0x68 pack8 swap pack32 ; + ~ : 5 # push-imm32-extended64 + swap 0x68 pack8 swap + ~ : provide-hex32 + pack32 ; ~ (output point, source register, source displacement value, target register ~ -- output point) @@ -641,19 +643,25 @@ s" :cc-greater" keyword ~ (output point, immediate value, register -- output point) : mov-reg64-imm32 - ~ : 5 mov-reg64-imm32 + ~ : 5 # mov-reg64-imm32 3roll rex-w 0xC7 pack8 swap 0 swap addressing-reg64 - swap pack32 ; + swap + ~ : provide-hex32 + pack32 ; : mov-reg64-imm64 - ~ : 8 mov-reg64-imm64 - 3roll rex-w swap reg64 0xB8 opcodereg swap pack64 ; + ~ : 8 # mov-reg64-imm64 + 3roll rex-w swap reg64 0xB8 opcodereg swap + ~ : provide-hex64 + pack64 ; : mov-extrareg64-imm64 - ~ : 8 mov-extrareg64-imm64 + ~ : 8 # mov-extrareg64-imm64 ~ Note the use of the B rex bit here; this instruction puts the register ~ number in the opcode field, so it uses Table 3-1. - 3roll rex-wb swap extrareg64 0xB8 opcodereg swap pack64 ; + 3roll rex-wb swap extrareg64 0xB8 opcodereg swap + ~ : provide-hex64 + pack64 ; ~ (output point, source register, target register -- output point) : mov-reg64-reg64 @@ -780,8 +788,10 @@ s" :cc-greater" keyword ~ (output point, source register, source displacement value, target register ~ -- output point) : mov-reg8-disp8-reg64 - ~ : 2 mov-reg8-disp8-reg64 - 4 roll pack8 0x8A pack8 4 unroll + ~ : 2 # mov-reg8-disp8-reg64 + 4 roll + ~ : provide-hex8 + pack8 0x8A pack8 4 unroll reg8 3unroll addressing-disp8-reg64 ; ~ (output point, source register, target register -- output point) @@ -1008,9 +1018,11 @@ s" :cc-greater" keyword ~ (output point, immediate value, target register -- output point) : add-reg64-imm8 - ~ : 2 add-reg64-imm8 + ~ : 2 # add-reg64-imm8 3roll rex-w 0x83 pack8 swap 0 swap addressing-reg64 - swap pack8 ; + swap + ~ : provide-hex8 + pack8 ; ~ See above re: parameter order. ~ @@ -1030,17 +1042,21 @@ s" :cc-greater" keyword ~ ~ (output point, immediate value, target register -- output point) : sub-reg64-imm8 - ~ : 2 sub-reg64-imm8 + ~ : 2 # sub-reg64-imm8 3roll rex-w 0x83 pack8 swap 5 swap addressing-reg64 - swap pack8 ; + swap + ~ : provide-hex8 + pack8 ; ~ See above re: parameter order. ~ ~ (output point, source register, target register -- output point) : sbb-reg64-imm8 - ~ : 2 sbb-reg64-imm8 + ~ : 2 # sbb-reg64-imm8 3roll rex-w 0x83 pack8 swap 3 swap addressing-reg64 - swap pack8 ; + swap + ~ : provide-hex8 + pack8 ; ~ The target register is always rax. ~ @@ -1089,10 +1105,12 @@ s" :cc-greater" keyword ~ (output point, source value, target register -- output point) : and-reg64-imm8 - ~ : 2 and-reg64-imm8 + ~ : 2 # and-reg64-imm8 3roll rex-w 0x83 pack8 swap 4 swap addressing-reg64 - swap pack8 ; + swap + ~ : provide-hex8 + pack8 ; ~ (output point, source register, target register -- output point) : or-reg64-reg64 @@ -1102,10 +1120,12 @@ s" :cc-greater" keyword ~ (output point, source value, target register -- output point) : or-reg64-imm8 - ~ : 2 or-reg64-imm8 + ~ : 2 # or-reg64-imm8 3roll rex-w 0x83 pack8 swap 1 swap addressing-reg64 - swap pack8 ; + swap + ~ : provide-hex8 + pack8 ; ~ (output point, source register, target register -- output point) : xor-reg64-reg64 @@ -1121,31 +1141,39 @@ s" :cc-greater" keyword ~ (output point, bit count, target register -- output point) : rol-reg64-imm8 - ~ : 2 rol-reg64-imm8 + ~ : 2 # rol-reg64-imm8 3roll rex-w 0xC1 pack8 swap 0 swap addressing-reg64 - swap pack8 ; + swap + ~ : provide-hex8 + pack8 ; ~ (output point, bit count, target register -- output point) : rol-reg8-imm8 - ~ : 2 rol-reg8-imm8 + ~ : 2 # rol-reg8-imm8 3roll 0xC0 pack8 swap 0 swap addressing-reg8 - swap pack8 ; + swap + ~ : provide-hex8 + pack8 ; ~ (output point, bit count, target register -- output point) : ror-reg64-imm8 - ~ : 1 ror-reg64-imm8 + ~ : 1 # ror-reg64-imm8 3roll rex-w 0xC1 swap 1 swap addressing-reg64 - swap pack8 ; + swap + ~ : provide-hex8 + pack8 ; ~ (output point, bit count, target register -- output point) : ror-reg8-imm8 - ~ : 2 ror-reg8-imm8 + ~ : 2 # ror-reg8-imm8 3roll 0xC0 pack8 swap 1 swap addressing-reg64 - swap pack8 ; + swap + ~ : provide-hex8 + pack8 ; ~ Control flow instructions @@ -1168,12 +1196,14 @@ s" :cc-greater" keyword ~ ~ (output point, immediate value, target register -- output point) : cmp-reg64-imm8 - ~ : 2 cmp-reg64-imm8 + ~ : 2 # cmp-reg64-imm8 3roll rex-w 0x83 pack8 ~ (immediate value, register, output point) swap 7 swap addressing-reg64 ~ ( output point, immediate value) - swap pack8 ; + swap + ~ : provide-hex8 + pack8 ; ~ Pretend to bitwise-and left with right, and set the flags the same way as ~ if we actually had. @@ -1197,16 +1227,20 @@ s" :cc-greater" keyword ~ (output point, address offset value, condition code -- output point) : jmp-cc-rel-imm8 - ~ : 1 jmp-cc-rel-imm8 + ~ : 1 # jmp-cc-rel-imm8 3roll swap condition-code 0x70 opcodecc - swap pack8 ; + swap + ~ : provide-hex8 + pack8 ; ~ (output point, address offset value, condition code -- output point) : jmp-cc-rel-imm32 - ~ : 5 jmp-cc-rel-imm32 + ~ : 5 # jmp-cc-rel-imm32 3unroll 0x0F pack8 swap condition-code 0x70 opcodecc - swap pack32 ; + swap + ~ : provide-hex32 + pack32 ; ~ (output point, register -- output point) : jmp-abs-indirect-reg64 @@ -1216,23 +1250,29 @@ s" :cc-greater" keyword ~ (output point, address offset value -- output point) : jmp-rel-imm8 - ~ : 2 jmp-rel-imm8 + ~ : 2 # jmp-rel-imm8 swap 0xEB pack8 - swap pack8 ; + swap + ~ : provide-hex8 + pack8 ; ~ (output point, address offset value -- output point) : jmp-rel-imm32 - ~ : 5 jmp-rel-imm32 + ~ : 5 # jmp-rel-imm32 swap 0xE9 pack8 - swap pack32 ; + swap + ~ : provide-hex32 + pack32 ; ~ This is technically a "near" call. The name intentionally doesn't say so. ~ ~ (output point, address offset value -- output point) : call-rel-imm32 - ~ : 5 call-rel-imm32 + ~ : 5 # call-rel-imm32 swap 0xE8 pack8 - swap pack32 ; + swap + ~ : provide-hex32 + pack32 ; ~ Similarly, this is a "near" return. ~ diff --git a/transform.e b/transform.e index 4201d64..7d5d5bd 100644 --- a/transform.e +++ b/transform.e @@ -2742,7 +2742,10 @@ allocate-transformation-state s" transformation-state" variable : hex-output-metadata-entry-type-string-literal 5 ; : hex-output-metadata-entry-type-raw-string-literal 6 ; : hex-output-metadata-entry-type-alignment 7 ; -: hex-output-metadata-entry-type-provide-substring-hex64 8 ; +: hex-output-metadata-entry-type-provide-substring-hex8 8 ; +: hex-output-metadata-entry-type-provide-substring-hex16 9 ; +: hex-output-metadata-entry-type-provide-substring-hex32 10 ; +: hex-output-metadata-entry-type-provide-substring-hex64 11 ; ~ Initialize the contents of the output metadata to all zeroes. This is ~ called from hex-transform at its top level, at the very start, to make sure @@ -2996,7 +2999,11 @@ allocate-transformation-state s" transformation-state" variable : is-provide-substring-entry hex-output-metadata-entry-type @ - hex-output-metadata-entry-type-provide-substring-hex64 = ; + dup hex-output-metadata-entry-type-provide-substring-hex8 = swap + dup hex-output-metadata-entry-type-provide-substring-hex16 = swap + dup hex-output-metadata-entry-type-provide-substring-hex32 = swap + hex-output-metadata-entry-type-provide-substring-hex64 = swap + || || || ; : is-fresh-line@ transformation-state transformation-state-output-metadata @ @@ -3244,6 +3251,24 @@ allocate-transformation-state s" transformation-state" variable pop-substring-entry-stack + dup hex-output-metadata-entry-type @ + hex-output-metadata-entry-type-provide-substring-hex8 = { + ." 0x" + dup hex-output-metadata-entry-string @ .hex8 + } if + + dup hex-output-metadata-entry-type @ + hex-output-metadata-entry-type-provide-substring-hex16 = { + ." 0x" + dup hex-output-metadata-entry-string @ .hex16 + } if + + dup hex-output-metadata-entry-type @ + hex-output-metadata-entry-type-provide-substring-hex32 = { + ." 0x" + dup hex-output-metadata-entry-string @ .hex32 + } if + dup hex-output-metadata-entry-type @ hex-output-metadata-entry-type-provide-substring-hex64 = { ." 0x" @@ -3383,8 +3408,7 @@ allocate-transformation-state s" transformation-state" variable 0 current-column! } if - dup hex-output-metadata-entry-type @ - hex-output-metadata-entry-type-provide-substring-hex64 = { + dup is-provide-substring-entry { dup push-substring-entry-stack } if } if @@ -4212,10 +4236,30 @@ allocate-transformation-state s" transformation-state" variable exit } if - dup s" provide-substring-hex64" stringcmp 0 = { - ~ Create a new "provide substring hex64" entry. Notice that we - ~ intentionally read one stack level deeper than anything of ours, which - ~ will be a value provided by the code the magic comment is embedded in. + ~ Now we have some commands for the various provide-substring variants. + ~ Notice with all of these that we intentionally read one stack level deeper + ~ than anything of ours, which will be a value provided by the code the + ~ magic comment is embedded in. + dup s" provide-hex8" stringcmp 0 = { + ~ Create a new "provide substring hex8" entry. + drop 2 pick hex-output-metadata-entry-type-provide-substring-hex8 swap + add-hex-output-metadata-entry + exit + } if + dup s" provide-hex16" stringcmp 0 = { + ~ Create a new "provide substring hex16" entry. + drop 2 pick hex-output-metadata-entry-type-provide-substring-hex16 swap + add-hex-output-metadata-entry + exit + } if + dup s" provide-hex32" stringcmp 0 = { + ~ Create a new "provide substring hex32" entry. + drop 2 pick hex-output-metadata-entry-type-provide-substring-hex32 swap + add-hex-output-metadata-entry + exit + } if + dup s" provide-hex64" stringcmp 0 = { + ~ Create a new "provide substring hex64" entry. drop 2 pick hex-output-metadata-entry-type-provide-substring-hex64 swap add-hex-output-metadata-entry exit -- cgit 1.4.1