Skip to content

Commit fb05335

Browse files
committed
amx: imply AMX_TOKENTHREADING when AMX_DONT_RELOCATE is set.
All assembler cores: remove OP_SYSREQ_D & OP_SYSREQ_ND opcodes when AMX_DONT_RELOCATE is set. GNU assembler cores: copy C preprocessor macros to assembler equates (so these cores can be built with GCC). Cortex M0 core: various bug fixes. Compiler: #pragma opcodeset.
1 parent bdf053c commit fb05335

File tree

8 files changed

+279
-133
lines changed

8 files changed

+279
-133
lines changed

amx/amx.c

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* License for the specific language governing permissions and limitations
1515
* under the License.
1616
*
17-
* Version: $Id: amx.c 6966 2023-07-20 18:46:01Z thiadmer $
17+
* Version: $Id: amx.c 6973 2023-08-05 20:07:04Z thiadmer $
1818
*/
1919

2020
#define WIN32_LEAN_AND_MEAN
@@ -101,7 +101,8 @@
101101
#define AMX_CLONE /* amx_Clone() */
102102
#define AMX_EXEC /* amx_Exec() */
103103
#define AMX_FLAGS /* amx_Flags() */
104-
#define AMX_INIT /* amx_Init() and amx_InitJIT() */
104+
#define AMX_INIT /* amx_Init() */
105+
#define AMX_JIT /* amx_InitJIT() and other JIT support */
105106
#define AMX_MEMINFO /* amx_MemInfo() */
106107
#define AMX_NAMELENGTH /* amx_NameLength() */
107108
#define AMX_NATIVEINFO /* amx_NativeInfo() */
@@ -146,7 +147,10 @@
146147
#define AMX_ALTCORE
147148
#endif
148149
#if !defined AMX_NO_PACKED_OPC && !defined AMX_TOKENTHREADING
149-
#define AMX_TOKENTHREADING /* packed opcodes require token threading */
150+
#define AMX_TOKENTHREADING /* packed opcodes require token threading (or switch threading) */
151+
#endif
152+
#if defined AMX_DONT_RELOCATE
153+
#define AMX_TOKENTHREADING /* direct threading cannot be used when opcodes cannot be relocated */
150154
#endif
151155

152156
#if defined __64BIT__
@@ -564,25 +568,27 @@ int AMXAPI amx_Callback(AMX *amx, cell index, cell *result, const cell *params)
564568
* This trick cannot work in the JIT, because the program would need to
565569
* be re-JIT-compiled after patching a P-code instruction.
566570
*/
567-
assert((amx->flags & AMX_FLAG_JITC)==0 || amx->sysreq_d==0);
568-
if (amx->sysreq_d!=0) {
569-
/* at the point of the call, the CIP pseudo-register points directly
570-
* behind the SYSREQ(.N) instruction and its parameter(s)
571-
*/
572-
unsigned char *code=amx->code+(int)amx->cip-sizeof(cell);
573-
if (amx->flags & AMX_FLAG_SYSREQN) /* SYSREQ.N has 2 parameters */
574-
code-=sizeof(cell);
575-
assert(amx->code!=NULL);
576-
assert(amx->cip>=4 && amx->cip<(hdr->dat - hdr->cod));
577-
assert(sizeof(f)<=sizeof(cell)); /* function pointer must fit in a cell */
578-
assert(*(cell*)code==index);
579-
#if defined AMX_TOKENTHREADING || !(defined __GNUC__ || defined __ICC || defined AMX_ASM || defined AMX_JIT)
580-
assert(!(amx->flags & AMX_FLAG_SYSREQN) && *(cell*)(code-sizeof(cell))==OP_SYSREQ
581-
|| (amx->flags & AMX_FLAG_SYSREQN) && *(cell*)(code-sizeof(cell))==OP_SYSREQ_N);
582-
#endif
583-
*(cell*)(code-sizeof(cell))=amx->sysreq_d;
584-
*(cell*)code=(cell)(intptr_t)f;
585-
} /* if */
571+
#if !defined AMX_DONT_RELOCATE
572+
assert((amx->flags & AMX_FLAG_JITC)==0 || amx->sysreq_d==0);
573+
if (amx->sysreq_d!=0) {
574+
/* at the point of the call, the CIP pseudo-register points directly
575+
* behind the SYSREQ(.N) instruction and its parameter(s)
576+
*/
577+
unsigned char *code=amx->code+(int)amx->cip-sizeof(cell);
578+
if (amx->flags & AMX_FLAG_SYSREQN) /* SYSREQ.N has 2 parameters */
579+
code-=sizeof(cell);
580+
assert(amx->code!=NULL);
581+
assert(amx->cip>=4 && amx->cip<(hdr->dat - hdr->cod));
582+
assert(sizeof(f)<=sizeof(cell)); /* function pointer must fit in a cell */
583+
assert(*(cell*)code==index);
584+
#if defined AMX_TOKENTHREADING || !(defined __GNUC__ || defined __ICC || defined AMX_ASM || defined AMX_JIT)
585+
assert(!(amx->flags & AMX_FLAG_SYSREQN) && *(cell*)(code-sizeof(cell))==OP_SYSREQ
586+
|| (amx->flags & AMX_FLAG_SYSREQN) && *(cell*)(code-sizeof(cell))==OP_SYSREQ_N);
587+
#endif
588+
*(cell*)(code-sizeof(cell))=amx->sysreq_d;
589+
*(cell*)code=(cell)(intptr_t)f;
590+
} /* if */
591+
#endif
586592

587593
/* Note:
588594
* params[0] == number of bytes for the additional parameters passed to the native function
@@ -1334,13 +1340,13 @@ int AMXAPI amx_Init(AMX *amx,void *program)
13341340
char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
13351341
int ret=proc_pidpath(getpid(), pathbuf, sizeof(pathbuf));
13361342
if (ret>0) {
1337-
char ptr=strrchr(pathbuf,'/');
1338-
assert(ptr!=NULL);
1343+
char ptr=strrchr(pathbuf,'/');
1344+
assert(ptr!=NULL);
13391345
*ptr='\0';
13401346
asprintf(&ptr,"%s/%s",pathbuf,libname);
1341-
assert(ptr!=NULL);
1347+
assert(ptr!=NULL);
13421348
hlib=dlopen(ptr,RTLD_NOW);
1343-
free(ptr);
1349+
free(ptr);
13441350
}
13451351
if (hlib==NULL) {
13461352
/* if failed, try to search elsewhere */
@@ -1457,16 +1463,6 @@ int AMXAPI amx_InitJIT(AMX *amx, void *reloc_table, void *native_code)
14571463
return (res == 0) ? AMX_ERR_NONE : AMX_ERR_INIT_JIT;
14581464
}
14591465

1460-
#else /* #if defined AMX_JIT */
1461-
1462-
int AMXAPI amx_InitJIT(AMX *amx,void *compiled_program,void *reloc_table)
1463-
{
1464-
(void)amx;
1465-
(void)compiled_program;
1466-
(void)reloc_table;
1467-
return AMX_ERR_INIT_JIT;
1468-
}
1469-
14701466
#endif /* #if defined AMX_JIT */
14711467

14721468
#endif /* AMX_INIT */

amx/amxexec_arm7.s

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
; License for the specific language governing permissions and limitations
3030
; under the License.
3131
;
32-
; Version: $Id: amxexec_arm7.s 6131 2020-04-29 19:47:15Z thiadmer $
32+
; Version: $Id: amxexec_arm7.s 6973 2023-08-05 20:07:04Z thiadmer $
3333

3434

3535
AREA amxexec_data, DATA, READONLY
@@ -1033,6 +1033,10 @@ OP_CASETBL_OVL
10331033
mov r11, #AMX_ERR_INVINSTR ; these instructions are no longer supported
10341034
b amx_exit
10351035

1036+
1037+
; patched instructions
1038+
IF :LNOT::DEF:AMX_DONT_RELOCATE
1039+
10361040
OP_SYSREQ_D ; tested
10371041
GETPARAM r12 ; address of native function in r12
10381042
; store stack and heap state AMX state
@@ -1084,6 +1088,15 @@ OP_SYSREQ_ND ; tested
10841088
bne amx_exit ; yes -> quit
10851089
NEXT
10861090

1091+
ELSE ; AMX_DONT_RELOCATE
1092+
1093+
OP_SYSREQ_D
1094+
OP_SYSREQ_ND
1095+
mov r11, #AMX_ERR_INVINSTR ; relocation disabled -> patched instructions should not be present
1096+
b amx_exit
1097+
1098+
ENDIF ; AMX_DONT_RELOCATE
1099+
10871100

10881101
; overlay instructions
10891102
IF :LNOT::DEF:AMX_NO_OVERLAY

amx/amxexec_arm7_gas.s

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
@ machine.
1616
@
1717
@
18-
@ Copyright (c) CompuPhase, 2006-2020
18+
@ Copyright (c) CompuPhase, 2006-2023
1919
@
2020
@ Licensed under the Apache License, Version 2.0 (the "License"); you may not
2121
@ use this file except in compliance with the License. You may obtain a copy
@@ -29,11 +29,34 @@
2929
@ License for the specific language governing permissions and limitations
3030
@ under the License.
3131
@
32-
@ Version: $Id: amxexec_arm7_gas.s 6131 2020-04-29 19:47:15Z thiadmer $
32+
@ Version: $Id: amxexec_arm7_gas.s 6973 2023-08-05 20:07:04Z thiadmer $
3333

3434
.file "amxexec_arm7_gas.s"
3535
.syntax unified
3636

37+
38+
@ Copy GCC preprocessor definitions to assembler equates, so that the assembler
39+
@ file can be built with 'gcc' as well as 'as'.
40+
#if defined BIG_ENDIAN
41+
.equ BIG_ENDIAN, 1
42+
#endif
43+
#if defined AMX_DONT_RELOCATE
44+
.equ AMX_DONT_RELOCATE, 1
45+
#endif
46+
#if defined AMX_NO_MACRO_INSTR
47+
.equ AMX_NO_MACRO_INSTR, 1
48+
#endif
49+
#if defined AMX_NO_OVERLAY
50+
.equ AMX_NO_OVERLAY, 1
51+
#endif
52+
#if defined AMX_NO_PACKED_OPC
53+
.equ AMX_NO_PACKED_OPC, 1
54+
#endif
55+
#if define AMX_TOKENTHREADING
56+
.equ AMX_TOKENTHREADING, 1
57+
#endif
58+
59+
3760
.ifndef AMX_NO_PACKED_OPC
3861
.ifndef AMX_TOKENTHREADING
3962
.equ AMX_TOKENTHREADING, 1 @ packed opcodes require token threading
@@ -1024,6 +1047,10 @@ amx_exec_run:
10241047
mov r11, #AMX_ERR_INVINSTR @ these instructions are no longer supported
10251048
b .amx_exit
10261049

1050+
1051+
@ patched instructions
1052+
.ifndef AMX_DONT_RELOCATE
1053+
10271054
.OP_SYSREQ_D: @ tested
10281055
GETPARAM r12 @ address of native function in r12
10291056
@ store stack and heap state AMX state
@@ -1075,6 +1102,15 @@ amx_exec_run:
10751102
bne .amx_exit @ yes -> quit
10761103
NEXT
10771104

1105+
.else @ AMX_DONT_RELOCATE
1106+
1107+
.OP_SYSREQ_D:
1108+
.OP_SYSREQ_ND:
1109+
mov r11, #AMX_ERR_INVINSTR @ relocation disabled -> patched instructions should not be present
1110+
b .amx_exit
1111+
1112+
.endif @ AMX_DONT_RELOCATE
1113+
10781114

10791115
@ overlay instructions
10801116
.ifndef AMX_NO_OVERLAY

0 commit comments

Comments
 (0)