Skip to content

Commit f3b968c

Browse files
committed
WIP: Share allocation helpers between CoreCLR and NativeAOT
1 parent b6f17be commit f3b968c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+2680
-1966
lines changed

src/coreclr/System.Private.CoreLib/src/System/String.CoreCLR.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ internal static unsafe string StrCns(uint rid, IntPtr scopeHandle)
2525
}
2626

2727
[MethodImpl(MethodImplOptions.InternalCall)]
28-
internal static extern string FastAllocateString(int length);
28+
internal static extern unsafe string FastAllocateString(MethodTable *pMT, int length);
29+
30+
[DebuggerHidden]
31+
internal static unsafe string FastAllocateString(int length)
32+
{
33+
return FastAllocateString(TypeHandle.TypeHandleOf<string>().AsMethodTable(), length);
34+
}
2935

3036
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "String_Intern")]
3137
private static partial void Intern(StringHandleOnStack src);

src/coreclr/inc/jithelpers.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,20 @@
9898
JITHELPER(CORINFO_HELP_DBLREM, JIT_DblRem, METHOD__NIL)
9999

100100
// Allocating a new object
101-
JITHELPER(CORINFO_HELP_NEWFAST, JIT_New, METHOD__NIL)
102-
JITHELPER(CORINFO_HELP_NEWFAST_MAYBEFROZEN, JIT_NewMaybeFrozen,METHOD__NIL)
103-
DYNAMICJITHELPER(CORINFO_HELP_NEWSFAST, JIT_New, METHOD__NIL)
104-
JITHELPER(CORINFO_HELP_NEWSFAST_FINALIZE, NULL, METHOD__NIL)
105-
DYNAMICJITHELPER(CORINFO_HELP_NEWSFAST_ALIGN8, JIT_New, METHOD__NIL)
106-
JITHELPER(CORINFO_HELP_NEWSFAST_ALIGN8_VC, NULL, METHOD__NIL)
107-
JITHELPER(CORINFO_HELP_NEWSFAST_ALIGN8_FINALIZE, NULL, METHOD__NIL)
108-
DYNAMICJITHELPER(CORINFO_HELP_NEW_MDARR, NULL, METHOD__ARRAY__CREATEINSTANCEMDARRAY)
109-
DYNAMICJITHELPER(CORINFO_HELP_NEW_MDARR_RARE, NULL, METHOD__ARRAY__CREATEINSTANCEMDARRAY)
110-
JITHELPER(CORINFO_HELP_NEWARR_1_DIRECT, JIT_NewArr1,METHOD__NIL)
111-
JITHELPER(CORINFO_HELP_NEWARR_1_MAYBEFROZEN, JIT_NewArr1MaybeFrozen,METHOD__NIL)
112-
DYNAMICJITHELPER(CORINFO_HELP_NEWARR_1_OBJ, JIT_NewArr1,METHOD__NIL)
113-
DYNAMICJITHELPER(CORINFO_HELP_NEWARR_1_VC, JIT_NewArr1,METHOD__NIL)
114-
DYNAMICJITHELPER(CORINFO_HELP_NEWARR_1_ALIGN8, JIT_NewArr1,METHOD__NIL)
101+
JITHELPER(CORINFO_HELP_NEWFAST, RhpNew, METHOD__NIL)
102+
JITHELPER(CORINFO_HELP_NEWFAST_MAYBEFROZEN, RhpNewMaybeFrozen, METHOD__NIL)
103+
DYNAMICJITHELPER(CORINFO_HELP_NEWSFAST, RhpNew, METHOD__NIL)
104+
JITHELPER(CORINFO_HELP_NEWSFAST_FINALIZE, NULL, METHOD__NIL)
105+
DYNAMICJITHELPER(CORINFO_HELP_NEWSFAST_ALIGN8, RhpNew, METHOD__NIL)
106+
JITHELPER(CORINFO_HELP_NEWSFAST_ALIGN8_VC, NULL, METHOD__NIL)
107+
JITHELPER(CORINFO_HELP_NEWSFAST_ALIGN8_FINALIZE, NULL, METHOD__NIL)
108+
DYNAMICJITHELPER(CORINFO_HELP_NEW_MDARR, NULL, METHOD__ARRAY__CREATEINSTANCEMDARRAY)
109+
DYNAMICJITHELPER(CORINFO_HELP_NEW_MDARR_RARE, NULL, METHOD__ARRAY__CREATEINSTANCEMDARRAY)
110+
JITHELPER(CORINFO_HELP_NEWARR_1_DIRECT, RhpNewArray, METHOD__NIL)
111+
JITHELPER(CORINFO_HELP_NEWARR_1_MAYBEFROZEN, RhpNewArrayMaybeFrozen, METHOD__NIL)
112+
DYNAMICJITHELPER(CORINFO_HELP_NEWARR_1_OBJ, RhpNewArray, METHOD__NIL)
113+
DYNAMICJITHELPER(CORINFO_HELP_NEWARR_1_VC, RhpNewArray, METHOD__NIL)
114+
DYNAMICJITHELPER(CORINFO_HELP_NEWARR_1_ALIGN8, RhpNewArray, METHOD__NIL)
115115

116116
DYNAMICJITHELPER(CORINFO_HELP_STRCNS, NULL, METHOD__STRING__STRCNS)
117117

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<StepFilter xmlns="http://schemas.microsoft.com/vstudio/debugger/natstepfilter/2010">
33
<Function>
4-
<Name>RhpNewFast|RhpNewFinalizable|RhpNewFastAlign8|RhpNewFastMisalign|RhpNewFinalizableAlign8|RhpNewArray|RhpNewArrayAlign8</Name>
4+
<Name>RhpNewFast|RhpNewFinalizable|RhpNewFastAlign8|RhpNewFastMisalign|RhpNewFinalizableAlign8|RhpNewArrayFast|RhpNewArrayFastAlign8</Name>
55
<Action>NoStepInto</Action>
66
</Function>
77
</StepFilter>

src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/InternalCalls.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ internal static int RhEndNoGCRegion()
126126
[MethodImpl(MethodImplOptions.InternalCall)]
127127
internal static extern unsafe object RhpNewFinalizable(MethodTable* pEEType);
128128

129-
[RuntimeImport(Redhawk.BaseName, "RhpNewArray")]
129+
[RuntimeImport(Redhawk.BaseName, "RhpNewArrayFast")]
130130
[MethodImpl(MethodImplOptions.InternalCall)]
131-
internal static extern unsafe object RhpNewArray(MethodTable* pEEType, int length);
131+
internal static extern unsafe object RhpNewArrayFast(MethodTable* pEEType, int length);
132132

133133
#if FEATURE_64BIT_ALIGNMENT
134134
[RuntimeImport(Redhawk.BaseName, "RhpNewFastAlign8")]
@@ -141,7 +141,7 @@ internal static int RhEndNoGCRegion()
141141

142142
[RuntimeImport(Redhawk.BaseName, "RhpNewArrayAlign8")]
143143
[MethodImpl(MethodImplOptions.InternalCall)]
144-
internal static extern unsafe object RhpNewArrayAlign8(MethodTable* pEEType, int length);
144+
internal static extern unsafe object RhpNewArrayFastAlign8(MethodTable* pEEType, int length);
145145

146146
[RuntimeImport(Redhawk.BaseName, "RhpNewFastMisalign")]
147147
[MethodImpl(MethodImplOptions.InternalCall)]

src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/RuntimeExports.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ public static unsafe object RhNewArray(MethodTable* pEEType, int length)
6464
MethodTable* pEEElementType = pEEType->RelatedParameterType;
6565
if (pEEElementType->IsValueType && pEEElementType->RequiresAlign8)
6666
{
67-
return InternalCalls.RhpNewArrayAlign8(pEEType, length);
67+
return InternalCalls.RhpNewArrayFastAlign8(pEEType, length);
6868
}
6969
else
7070
#endif // FEATURE_64BIT_ALIGNMENT
7171
{
72-
return InternalCalls.RhpNewArray(pEEType, length);
72+
return InternalCalls.RhpNewArrayFast(pEEType, length);
7373
}
7474
}
7575

@@ -380,10 +380,10 @@ internal static unsafe IntPtr RhGetRuntimeHelperForType(MethodTable* pEEType, Ru
380380
#if FEATURE_64BIT_ALIGNMENT
381381
MethodTable* pEEElementType = pEEType->RelatedParameterType;
382382
if (pEEElementType->IsValueType && pEEElementType->RequiresAlign8)
383-
return (IntPtr)(delegate*<MethodTable*, int, object>)&InternalCalls.RhpNewArrayAlign8;
383+
return (IntPtr)(delegate*<MethodTable*, int, object>)&InternalCalls.RhpNewArrayFastAlign8;
384384
#endif // FEATURE_64BIT_ALIGNMENT
385385

386-
return (IntPtr)(delegate*<MethodTable*, int, object>)&InternalCalls.RhpNewArray;
386+
return (IntPtr)(delegate*<MethodTable*, int, object>)&InternalCalls.RhpNewArrayFast;
387387

388388
default:
389389
Debug.Fail("Unknown RuntimeHelperKind");

src/coreclr/nativeaot/Runtime/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ if (CLR_CMAKE_TARGET_ARCH_AMD64)
205205
endif (CLR_CMAKE_TARGET_ARCH_AMD64)
206206

207207
list(APPEND RUNTIME_SOURCES_ARCH_ASM
208-
${ARCH_SOURCES_DIR}/AllocFast.${ASM_SUFFIX}
208+
${RUNTIME_DIR}/${ARCH_SOURCES_DIR}/AllocFast.${ASM_SUFFIX}
209209
${ARCH_SOURCES_DIR}/ExceptionHandling.${ASM_SUFFIX}
210210
${ARCH_SOURCES_DIR}/GcProbe.${ASM_SUFFIX}
211211
${ARCH_SOURCES_DIR}/MiscStubs.${ASM_SUFFIX}

src/coreclr/nativeaot/Runtime/amd64/AsmMacros.inc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ PUSH_COOP_PINVOKE_FRAME macro trashReg
302302

303303
;; allocate scratch space and any required alignment
304304
alloc_stack 28h
305+
306+
END_PROLOGUE
305307
endm
306308

307309
;;
@@ -322,6 +324,11 @@ POP_COOP_PINVOKE_FRAME macro
322324
pop r10 ; discard caller RSP
323325
endm
324326

327+
INLINE_GET_ALLOC_CONTEXT macro destReg, trashReg
328+
INLINE_GET_TLS_VAR destReg, trashReg, tls_CurrentThread
329+
lea destReg, [destReg + OFFSETOF__Thread__m_eeAllocContext]
330+
endm
331+
325332
; - TAILCALL_RAX: ("jmp rax") should be used for tailcalls, this emits an instruction
326333
; sequence which is recognized by the unwinder as a valid epilogue terminator
327334
TAILJMP_RAX TEXTEQU <DB 048h, 0FFh, 0E0h>
@@ -335,10 +342,7 @@ TSF_DoNotTriggerGc equ 10h
335342
;;
336343
;; Rename fields of nested structs
337344
;;
338-
OFFSETOF__Thread__m_alloc_context__alloc_ptr equ OFFSETOF__Thread__m_eeAllocContext + OFFSETOF__ee_alloc_context__m_rgbAllocContextBuffer + OFFSETOF__gc_alloc_context__alloc_ptr
339-
OFFSETOF__Thread__m_eeAllocContext__combined_limit equ OFFSETOF__Thread__m_eeAllocContext + OFFSETOF__ee_alloc_context__combined_limit
340-
341-
345+
OFFSETOF__ee_alloc_context__alloc_ptr equ OFFSETOF__ee_alloc_context__m_rgbAllocContextBuffer + OFFSETOF__gc_alloc_context__alloc_ptr
342346

343347
;; GC type flags
344348
GC_ALLOC_FINALIZE equ 1

src/coreclr/nativeaot/Runtime/arm64/AsmMacros.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ STATUS_REDHAWK_THREAD_ABORT equ 0x43
7878
;;
7979
;; Rename fields of nested structs
8080
;;
81-
OFFSETOF__Thread__m_alloc_context__alloc_ptr equ OFFSETOF__Thread__m_eeAllocContext + OFFSETOF__ee_alloc_context__m_rgbAllocContextBuffer + OFFSETOF__gc_alloc_context__alloc_ptr
82-
OFFSETOF__Thread__m_eeAllocContext__combined_limit equ OFFSETOF__Thread__m_eeAllocContext + OFFSETOF__ee_alloc_context__combined_limit
81+
OFFSETOF__ee_alloc_context__alloc_ptr equ OFFSETOF__ee_alloc_context__m_rgbAllocContextBuffer + OFFSETOF__gc_alloc_context__alloc_ptr
8382

8483
;;
8584
;; IMPORTS
@@ -220,7 +219,6 @@ TrashRegister32Bit SETS "w":CC:("$TrashRegister32Bit":RIGHT:((:LEN:TrashRegister
220219
INLINE_GET_TLS_VAR $destReg, $trashReg, tls_CurrentThread
221220
MEND
222221

223-
224222
MACRO
225223
INLINE_THREAD_UNHIJACK $threadReg, $trashReg1, $trashReg2
226224
;;
@@ -236,6 +234,13 @@ TrashRegister32Bit SETS "w":CC:("$TrashRegister32Bit":RIGHT:((:LEN:TrashRegister
236234
0
237235
MEND
238236

237+
MACRO
238+
INLINE_GET_ALLOC_CONTEXT $destReg, $trashReg
239+
240+
INLINE_GET_TLS_VAR $destReg, $trashReg, tls_CurrentThread
241+
add $destReg, $destReg, OFFSETOF__Thread__m_eeAllocContext
242+
MEND
243+
239244
;; ---------------------------------------------------------------------------- -
240245
;;
241246
;; Macro to add a memory barrier. Equal to __sync_synchronize().

src/coreclr/nativeaot/Runtime/i386/AllocFast.S

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/coreclr/nativeaot/Runtime/i386/AsmMacros.inc

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,18 @@ endm
7070
;;
7171
;; This macro builds a frame describing the current state of managed code.
7272
;;
73-
;; The macro assumes it is called from a helper that has already set up an EBP frame and that the values of
74-
;; EBX, ESI and EDI remain unchanged from their values in managed code. It pushes the frame at the top of the
75-
;; stack.
73+
;; The macro assumes it is called from a helper that the values of EBX, ESI and EDI remain unchanged from their
74+
;; values in managed code. It pushes the frame at the top of the stack.
7675
;;
77-
;; EAX is trashed by this macro.
76+
;; EAX is trashed by this macro. ECX, EDX are preserved.
7877
;;
7978
PUSH_COOP_PINVOKE_FRAME macro transitionFrameReg
79+
push ebp
80+
mov ebp, esp
81+
82+
push ecx
83+
push edx
84+
8085
lea eax, [ebp + 8] ; get the ESP of the caller
8186
push eax ; save ESP
8287
push edi
@@ -94,18 +99,25 @@ endm
9499

95100
;;
96101
;; Remove the frame from a previous call to PUSH_COOP_PINVOKE_FRAME from the top of the stack and restore EBX,
97-
;; ESI and EDI to their previous values.
98-
;;
99-
;; TRASHES ECX
102+
;; ESI, EDI, ECX, and EDX to their previous values. Tears down the EBP frame.
100103
;;
101104
POP_COOP_PINVOKE_FRAME macro
102105
add esp, 4*4
103106
pop ebx
104107
pop esi
105108
pop edi
106109
pop ecx
110+
111+
pop edx
112+
pop ecx
113+
114+
pop ebp
107115
endm
108116

117+
INLINE_GET_ALLOC_CONTEXT macro destReg, trashReg
118+
INLINE_GETTHREAD destReg, trashReg
119+
lea destReg, [destReg + OFFSETOF__Thread__m_eeAllocContext]
120+
endm
109121

110122
;;
111123
;; CONSTANTS -- INTEGER
@@ -137,8 +149,7 @@ STATUS_REDHAWK_THREAD_ABORT equ 43h
137149
;;
138150
;; Rename fields of nested structs
139151
;;
140-
OFFSETOF__Thread__m_alloc_context__alloc_ptr equ OFFSETOF__Thread__m_eeAllocContext + OFFSETOF__ee_alloc_context__m_rgbAllocContextBuffer + OFFSETOF__gc_alloc_context__alloc_ptr
141-
OFFSETOF__Thread__m_eeAllocContext__combined_limit equ OFFSETOF__Thread__m_eeAllocContext + OFFSETOF__ee_alloc_context__combined_limit
152+
OFFSETOF__ee_alloc_context__alloc_ptr equ OFFSETOF__ee_alloc_context__m_rgbAllocContextBuffer + OFFSETOF__gc_alloc_context__alloc_ptr
142153

143154
;;
144155
;; CONSTANTS -- SYMBOLS

0 commit comments

Comments
 (0)