Open Watcom 16-Bit Optimization
Posted: Mon Sep 05, 2011 7:15 pm
Hey everyone,
I've been playing around with Open Watcom's 16-bit C++ compiler lately. I wanted to see what kind of assembly output it generates. From what I've seen so far, it seems like there's quite a bit of extra assembly code that comes out of the compiler. Below is a sample class and assembly output to illustrate.
Class header:
Class implementation:
Assembly output:
My real concern is creating a Foo object on the stack in the fake main() method. It reserves space on the stack for 14 bytes (sub sp, 0x000E) but the size of a Foo object is only 4 bytes - 2 bytes each for the two int variables. Wouldn't it be more efficient for the assembly code to just reserve 4 bytes on the stack and put the two constructor arguments there? Or am I missing something?
Please let me know if there's anything I need to clarify.
I've been playing around with Open Watcom's 16-bit C++ compiler lately. I wanted to see what kind of assembly output it generates. From what I've seen so far, it seems like there's quite a bit of extra assembly code that comes out of the compiler. Below is a sample class and assembly output to illustrate.
Class header:
Code: Select all
#include "stddef.h"
class Foo
{
private:
int x, y;
public:
cdecl Foo(int a = 0, int b = 0) : x(a), y(b) {}
cdecl ~Foo() {}
int cdecl bar();
int cdecl test(int, int);
};
Code: Select all
#include "test.hpp"
int Foo::bar()
{
return 0;
}
int Foo::test(int a, int b)
{
return a + b;
}
int main()
{
Foo f(1, 2);
foo.bar();
foo.test(1, 1);
}
Code: Select all
Module: C:\WATCOM\projects\test.cpp
GROUP: 'DGROUP' CONST,CONST2,_DATA,_BSS
File contains no line numbers.
Segment: _TEXT BYTE USE16 0000008D bytes
0000 int near Foo::bar():
0000 56 push si
0001 57 push di
0002 55 push bp
0003 89 E5 mov bp,sp
0005 81 EC 02 00 sub sp,0x0002
0009 C7 46 FE 00 00 mov word ptr -0x2[bp],0x0000
000E 8B 46 FE mov ax,word ptr -0x2[bp]
0011 89 EC mov sp,bp
0013 5D pop bp
0014 5F pop di
0015 5E pop si
0016 C3 ret
Routine Size: 23 bytes, Routine Base: _TEXT + 0000
0017 int near Foo::test( int, int ):
0017 56 push si
0018 57 push di
0019 55 push bp
001A 89 E5 mov bp,sp
001C 81 EC 02 00 sub sp,0x0002
0020 8B 46 0A mov ax,word ptr 0xa[bp]
0023 03 46 0C add ax,word ptr 0xc[bp]
0026 89 46 FE mov word ptr -0x2[bp],ax
0029 8B 46 FE mov ax,word ptr -0x2[bp]
002C 89 EC mov sp,bp
002E 5D pop bp
002F 5F pop di
0030 5E pop si
0031 C3 ret
Routine Size: 27 bytes, Routine Base: _TEXT + 0017
0032 main_:
0032 53 push bx
0033 51 push cx
0034 52 push dx
0035 56 push si
0036 57 push di
0037 55 push bp
0038 89 E5 mov bp,sp
003A 81 EC 0E 00 sub sp,0x000e
003E 8D 46 F2 lea ax,-0xe[bp]
0041 89 46 F8 mov word ptr -0x8[bp],ax
0044 C7 46 FA 01 00 mov word ptr -0x6[bp],0x0001
0049 C7 46 FC 02 00 mov word ptr -0x4[bp],0x0002
004E 8B 46 FA mov ax,word ptr -0x6[bp]
0051 89 46 F2 mov word ptr -0xe[bp],ax
0054 8B 46 FC mov ax,word ptr -0x4[bp]
0057 89 46 F4 mov word ptr -0xc[bp],ax
005A 8D 46 F2 lea ax,-0xe[bp]
005D 89 46 FE mov word ptr -0x2[bp],ax
0060 8D 46 F2 lea ax,-0xe[bp]
0063 50 push ax
0064 E8 00 00 call int near Foo::bar()
0067 83 C4 02 add sp,0x0002
006A B8 01 00 mov ax,0x0001
006D 50 push ax
006E B8 01 00 mov ax,0x0001
0071 50 push ax
0072 8D 46 F2 lea ax,-0xe[bp]
0075 50 push ax
0076 E8 00 00 call int near Foo::test( int, int )
0079 83 C4 06 add sp,0x0006
007C C7 46 F6 00 00 mov word ptr -0xa[bp],0x0000
0081 8B 46 F6 mov ax,word ptr -0xa[bp]
0084 89 EC mov sp,bp
0086 5D pop bp
0087 5F pop di
0088 5E pop si
0089 5A pop dx
008A 59 pop cx
008B 5B pop bx
008C C3 ret
Routine Size: 91 bytes, Routine Base: _TEXT + 0032
No disassembly errors
Segment: CONST BYTE USE16 00000000 bytes
Segment: CONST2 PARA USE16 00000000 bytes
Segment: _DATA BYTE USE16 00000000 bytes
Segment: _BSS BYTE USE16 00000000 bytes
BSS Size: 0 bytes
Please let me know if there's anything I need to clarify.