LINUX.ORG.RU

Qod. Опубликовал исходники компилятора, над которым работаю

 , qod, ,


5

4

Финально определился с названием языка, подчистил разные хвосты и написал README. Теперь наконец-то можно посмотреть на нечто большее, чем просто фрагменты кода в постах на форуме: https://github.com/wandrien/qod/

Драфты по дизайну языка пока еще не готовы. Если перед НГ завала работы не будет, то может выложу их в течение пары недель. Черновики пишу на русском, осилить всё чётко сформулировать на английском в разумные сроки я точно не смогу. На русском-то не всегда получается.

Надеюсь, что после публикации материалов по языку, мне щедро насыпят в панамку как конструктивной, так и не очень, критики.

А пока можно посмотреть на сам код вживую.

★★★

Немножко причесал код для генерации пролога и эпилога функций.

Improve code generation for function prologue and epilogue

* Emit "enter" opcode only when it actually shortens the code. (when optimizing for size)
* Prefer just plain "pop EBP" over "leave" when ESP doesn't need restoring. (No local variables in the stack frame).
* Add new optimization option OmitRedundantFramePointer and enable it for size and speed modes. The function prologue and epilogue are omitted when a function has neither parameters on the stack, nor local variables.
void EmitFunctionPrologue(char @Buff; FunctionContext @FnCtx)
	if OmitRedundantFramePointer & FnCtx.ParamFrameSize == 0 & FnCtx.StackSize == 0 then
		FnCtx.FramePointer = false;
	else
		FnCtx.FramePointer = true;
	end

	if FnCtx.FramePointer then
		/*
			enter 1FEh, 0        - c8 fe 01 00
			push  ebp            - 55
			mov   ebp, esp       - 89 e5
			sub   esp, 07Eh      - 83 ec 7e
			sub   esp, 0FEh      - 81 ec fe 00 00 00
			enter is always 4 bytes, with 0FFFFh is the maximum possible value
			sub esp, num is 3 bytes for signed byte operand, 6 bytes otherwise
		*/
		select
		case FnCtx.StackSize == 0:
			Emit("push    EBP");
			Emit("mov     EBP,  ESP");
		case EmitEnterInstruction & (FnCtx.StackSize <= 0xFFFF):
			Emit(@strcpy3(@Buff,"enter   ",@str(FnCtx.StackSize), ", 0"));
		default:
			Emit("push    EBP");
			Emit("mov     EBP,  ESP");
			Emit(@strcpy2(@Buff,"sub     ESP,  ",@str(FnCtx.StackSize)));
		end:select
	end:if
end

void EmitFunctionEpilogue(char @Buff; FunctionContext @FnCtx)
	if FnCtx.FramePointer then
		/*
			leave            - c9
			pop    ebp       - 5d
			mov    esp, ebp  - 89 ec
		*/
		select
		case FnCtx.StackSize == 0:
			Emit("pop     EBP");
		case EmitLeaveInstruction:
			Emit("leave");
		default:
			Emit("mov     ESP,  EBP");
			Emit("pop     EBP");
		end:select
	end:if

	if FnCtx.ParamFrameSize > 0 then
		Emit(@strcpy2(@Buff,"ret     ",@str(FnCtx.ParamFrameSize)));
	else
		Emit("ret");
	end:if
end
wandrien ★★★
() автор топика

Продолжение улучшений в генерации кода

Implement aliasing detection for PHO_LinkNode_Store()

1. New function bool PHO_CheckAliasing(word pStore; word pLoad) is added for the aliasing detection between pStore and pLoad subtrees.
2. PHO_LinkNode_Store(pStore) now resets any saved links that may be aliased by pStore.

This fixes potentially incorrect code optimizations, as in this example:

         mov     EBX,  dword [EBP-16]
         mov     EDX,  dword [EBP-8]
         mov     dword [EDX], EBX
                                          ; #line frontend_syn_statements.qdi:190
+        mov     EBX,  dword [EBP-16]
         imul    EBX,  37
         add     EBX,  @@DATA+8877685
         mov     dword [EBP-8], EBX

3. The implementation of aliasing checking allowed adding iINDEX to the list of node types available for linking by PHO_LinkNode() and PHO_LinkNode_Store().

This led to more efficient elimination of common subexpressions, as in the following examples:

         mov     EAX,  dword [EBP+12]
         lea     EAX,  [EAX*8+EAX]
         lea     EAX,  [EAX*8+EAX]
         mov     EAX,  dword [@@DATA+EAX+777204]
         cmp     EAX,  dword [EBP+12]
         je      @10472
                                          ; #line compiler_dict.qdi:115
-        mov     EAX,  dword [EBP+12]
-        lea     EAX,  [EAX*8+EAX]
-        lea     EAX,  [EAX*8+EAX]
-        mov     EAX,  dword [@@DATA+EAX+777204]
         push    EAX
         push    dword [EBP+8]
         call    @10468                   ; Dict_MakeFullyQualifiedName

........................................................................

         mov     EAX,  dword [EBP+8]
         imul    EAX,  37
         mov     EAX,  dword [@@DATA+EAX+8877685]
         cmp     EAX,  100000
         je      @11783
                                          ; #line frontend_syn_cte.qdi:477
         push    dword @@ROLITERALS+6581
-        mov     EAX,  dword [EBP+8]
-        imul    EAX,  37
-        mov     EAX,  dword [@@DATA+EAX+8877685]
         push    EAX
         call    @11006                   ; WarnAtNode

........................................................................

         mov     EBX,  dword [EBP-40]
         lea     EBX,  [EBX*8+EBX]
         lea     EBX,  [EBX*8+EBX]
         mov     dword [@@DATA+EBX+777236], EAX
                                          ; #line frontend_syn_up.qdi:406
-        mov     EAX,  dword [EBP-40]
-        lea     EAX,  [EAX*8+EAX]
-        lea     EAX,  [EAX*8+EAX]
-        mov     EAX,  dword [@@DATA+EAX+777236]
         push    EAX
         call    @10780                   ; T_IsFunc

........................................................................

         mov     EAX,  dword [EBP-20]
         imul    EAX,  37
         mov     EAX,  dword [@@DATA+EAX+8877672]
         cmp     EAX,  55
         je      @13658
                                          ; #line backend_labels.qdi:175
-        mov     EAX,  dword [EBP-20]
-        imul    EAX,  37
-        mov     EAX,  dword [@@DATA+EAX+8877672]
         cmp     EAX,  56
         je      @13658
                                          ; #line backend_labels.qdi:176
-        mov     EAX,  dword [EBP-20]
-        imul    EAX,  37
-        mov     EAX,  dword [@@DATA+EAX+8877672]
         cmp     EAX,  52
         je      @13658

wandrien ★★★
() автор топика