Optimization

General discussion on mikroPascal PRO for 8051.
Post Reply
Author
Message
zka67
Posts: 7
Joined: 30 Jun 2012 01:24

Optimization

#1 Post by zka67 » 04 Jul 2012 11:40

Your compiler is very poorly optimized code is generated.
This code will be so huge and slow.

Example 1:

Code: Select all

function calc(a : byte) : byte;
begin
  Result := a * 2;
end;

;test.mpas,6 :: 		begin
;test.mpas,7 :: 		Result := a * 2;
0x002E	0x7801    	MOV R0, #1
0x0030	0xE509    	MOV A, FARG_calc_a
0x0032	0x08      	INC R0
0x0033	0x8002    	SJMP L_test_calc2
L_test_calc3:
0x0035	0xC3      	CLR C
0x0036	0x33      	RLC A
L_test_calc2:
0x0037	0xD8FC    	DJNZ R0, L_test_calc3
0x0039	0xF50A    	MOV calc_local_result, A
;test.mpas,8 :: 		end;
0x003B	0xA80A    	MOV R0, calc_local_result
0x003D	0x22      	RET
The well-optimized code is as follows:

Code: Select all

  MOV A, FARG_calc_a
  ADD A,ACC
  MOV R0,A
  RET
Example 2:

Code: Select all

function calc(a : byte) : byte;
begin
  Result := a shr 4;
end;

;test.mpas,6 :: 		begin
;test.mpas,7 :: 		Result := a shr 4;
0x002E	0x7804    	MOV R0, #4
0x0030	0xE509    	MOV A, FARG_calc_a
0x0032	0x08      	INC R0
0x0033	0x8002    	SJMP L_test_calc2
L_test_calc3:
0x0035	0xC3      	CLR C
0x0036	0x13      	RRC A
L_test_calc2:
0x0037	0xD8FC    	DJNZ R0, L_test_calc3
0x0039	0xF50A    	MOV calc_local_result, A
;test.mpas,8 :: 		end;
0x003B	0xA80A    	MOV R0, calc_local_result
0x003D	0x22      	RET
The well-optimized code:

Code: Select all

  MOV A, FARG_calc_a
  SWAP A
  ANL A,#15
  MOV R0,A
  RET
Example 3:

Code: Select all

function calc : byte;
begin
  Result := 1;
end;

;test.mpas,6 :: 		begin
;test.mpas,7 :: 		Result := 1;
0x002E	0x750901  	MOV calc_local_result, #1
;test.mpas,8 :: 		end;
0x0031	0xA809    	MOV R0, calc_local_result
0x0033	0x22      	RET
I do not understand our need for additional instruction?
The well-optimized code:

Code: Select all

  MOV R0,#1
  RET

zka67
Posts: 7
Joined: 30 Jun 2012 01:24

Re: Optimization

#2 Post by zka67 » 14 Jul 2012 01:01

Can anyone answer this?
What kind of code, which is half kilobytes one string assignment?

I'm sorry, but this way I don't buy the compiler.

Code: Select all

;test1.mpas,4 :: 		begin
;test1.mpas,5 :: 		s := 'Can anyone answer this? What kind of code, which is half kilobytes one string assignment?';
0x0031	0x7808    	MOV R0, lo_addr(_s)
0x0033	0x7643    	MOV @R0, #67
0x0035	0x08      	INC R0
0x0036	0x7661    	MOV @R0, #97
0x0038	0x08      	INC R0
0x0039	0x766E    	MOV @R0, #110
0x003B	0x08      	INC R0
0x003C	0x7620    	MOV @R0, #32
0x003E	0x08      	INC R0
... and so it goes on forever ...
0x013B	0x763F    	MOV @R0, #63
0x013D	0x08      	INC R0
0x013E	0x7600    	MOV @R0, #0
0x0140	0x08      	INC R0
;test1.mpas,6 :: 		end.
0x0141	0x80FE    	SJMP #254
; end of _main

Post Reply

Return to “mikroPascal PRO for 8051 General”