[829] | 1 | #include "stdafx.h"
|
---|
| 2 |
|
---|
| 3 | #include <Compiler.h>
|
---|
| 4 |
|
---|
| 5 | #include "../BasicCompiler_Common/common.h"
|
---|
| 6 | #include "Opcode.h"
|
---|
| 7 |
|
---|
| 8 | void ExtendStackTo32( const Type &oldType );
|
---|
| 9 |
|
---|
| 10 | void ExtendStackTo64( const Type &oldType )
|
---|
| 11 | {
|
---|
| 12 | if( oldType.Is64() )
|
---|
| 13 | {
|
---|
| 14 | return;
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | //32ビットに拡張
|
---|
| 18 | ExtendStackTo32( oldType );
|
---|
| 19 |
|
---|
| 20 | //64ビットに拡張
|
---|
| 21 | if( oldType.IsSigned() )
|
---|
| 22 | {
|
---|
| 23 | //符号あり
|
---|
| 24 |
|
---|
| 25 | //pop eax
|
---|
| 26 | compiler.codeGenerator.op_pop(REG_EAX);
|
---|
| 27 |
|
---|
| 28 | //cdq
|
---|
| 29 | compiler.codeGenerator.op_cdq();
|
---|
| 30 |
|
---|
| 31 | //push edx
|
---|
| 32 | compiler.codeGenerator.op_push(REG_EDX);
|
---|
| 33 |
|
---|
| 34 | //push eax
|
---|
| 35 | compiler.codeGenerator.op_push(REG_EAX);
|
---|
| 36 | }
|
---|
| 37 | else{
|
---|
| 38 | //符号なし
|
---|
| 39 |
|
---|
| 40 | //pop eax
|
---|
| 41 | compiler.codeGenerator.op_pop(REG_EAX);
|
---|
| 42 |
|
---|
| 43 | //push 0
|
---|
| 44 | compiler.codeGenerator.op_push_V(0);
|
---|
| 45 |
|
---|
| 46 | //push eax
|
---|
| 47 | compiler.codeGenerator.op_push(REG_EAX);
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 | void ExtendStackTo32( const Type &oldType )
|
---|
| 51 | {
|
---|
| 52 | if( oldType.GetSize() == sizeof(long) )
|
---|
| 53 | {
|
---|
| 54 | return;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | if( oldType.Is64() )
|
---|
| 58 | {
|
---|
| 59 | //pop eax
|
---|
| 60 | compiler.codeGenerator.op_pop(REG_EAX);
|
---|
| 61 |
|
---|
| 62 | //pop edx
|
---|
| 63 | compiler.codeGenerator.op_pop(REG_EDX);
|
---|
| 64 |
|
---|
| 65 | //push eax
|
---|
| 66 | compiler.codeGenerator.op_push(REG_EAX);
|
---|
| 67 | }
|
---|
| 68 | else if( oldType.IsInteger() )
|
---|
| 69 | {
|
---|
| 70 | //pop eax
|
---|
| 71 | compiler.codeGenerator.op_pop(REG_EAX);
|
---|
| 72 |
|
---|
| 73 | //movsx eax,ax
|
---|
| 74 | compiler.codeGenerator.op_movsx_R32R16( REG_EAX );
|
---|
| 75 |
|
---|
| 76 | //push eax
|
---|
| 77 | compiler.codeGenerator.op_push(REG_EAX);
|
---|
| 78 | }
|
---|
| 79 | else if( oldType.IsWord() )
|
---|
| 80 | {
|
---|
| 81 | //pop eax
|
---|
| 82 | compiler.codeGenerator.op_pop(REG_EAX);
|
---|
| 83 |
|
---|
| 84 | //and eax,0000FFFFh
|
---|
| 85 | compiler.codeGenerator.op_and_RV( REG_EAX, 0x0000FFFF );
|
---|
| 86 |
|
---|
| 87 | //push eax
|
---|
| 88 | compiler.codeGenerator.op_push(REG_EAX);
|
---|
| 89 | }
|
---|
| 90 | else if( oldType.IsSByte() )
|
---|
| 91 | {
|
---|
| 92 | //pop eax
|
---|
| 93 | compiler.codeGenerator.op_pop(REG_EAX);
|
---|
| 94 |
|
---|
| 95 | //movsx eax,al
|
---|
| 96 | compiler.codeGenerator.op_movsx_R32R8( REG_EAX );
|
---|
| 97 |
|
---|
| 98 | //push eax
|
---|
| 99 | compiler.codeGenerator.op_push(REG_EAX);
|
---|
| 100 | }
|
---|
| 101 | else if( oldType.IsByte() || oldType.IsBoolean() )
|
---|
| 102 | {
|
---|
| 103 | //pop eax
|
---|
| 104 | compiler.codeGenerator.op_pop(REG_EAX);
|
---|
| 105 |
|
---|
| 106 | //and eax,000000FFh
|
---|
| 107 | compiler.codeGenerator.op_and_RV( REG_EAX, 0x000000FF );
|
---|
| 108 |
|
---|
| 109 | //push eax
|
---|
| 110 | compiler.codeGenerator.op_push(REG_EAX);
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | void ExtendStackTo16( const Type &oldType ){
|
---|
| 114 | if( oldType.Is64() )
|
---|
| 115 | {
|
---|
| 116 | //pop eax
|
---|
| 117 | compiler.codeGenerator.op_pop(REG_EAX);
|
---|
| 118 |
|
---|
| 119 | //pop edx
|
---|
| 120 | compiler.codeGenerator.op_pop(REG_EDX);
|
---|
| 121 |
|
---|
| 122 | //push eax
|
---|
| 123 | compiler.codeGenerator.op_push(REG_EAX);
|
---|
| 124 | }
|
---|
| 125 | else if( oldType.IsSByte() )
|
---|
| 126 | {
|
---|
| 127 | //pop eax
|
---|
| 128 | compiler.codeGenerator.op_pop(REG_EAX);
|
---|
| 129 |
|
---|
| 130 | //movsx eax,al
|
---|
| 131 | compiler.codeGenerator.op_movsx_R32R8( REG_EAX );
|
---|
| 132 |
|
---|
| 133 | //push eax
|
---|
| 134 | compiler.codeGenerator.op_push(REG_EAX);
|
---|
| 135 | }
|
---|
| 136 | else if( oldType.IsByte() )
|
---|
| 137 | {
|
---|
| 138 | //pop eax
|
---|
| 139 | compiler.codeGenerator.op_pop(REG_EAX);
|
---|
| 140 |
|
---|
| 141 | //and eax,000000FFh
|
---|
| 142 | compiler.codeGenerator.op_and_RV( REG_EAX, 0x000000FF );
|
---|
| 143 |
|
---|
| 144 | //push eax
|
---|
| 145 | compiler.codeGenerator.op_push(REG_EAX);
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 | void ExtendStackTo8( const Type &oldType )
|
---|
| 149 | {
|
---|
| 150 | if( oldType.Is64() )
|
---|
| 151 | {
|
---|
| 152 | //pop eax
|
---|
| 153 | compiler.codeGenerator.op_pop(REG_EAX);
|
---|
| 154 |
|
---|
| 155 | //pop edx
|
---|
| 156 | compiler.codeGenerator.op_pop(REG_EDX);
|
---|
| 157 |
|
---|
| 158 | //push eax
|
---|
| 159 | compiler.codeGenerator.op_push(REG_EAX);
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 |
|
---|
| 164 | void ChangeTypeToWhole( const Type &oldType, const Type &newType ){
|
---|
| 165 | if( oldType.IsDouble() )
|
---|
| 166 | {
|
---|
| 167 | if( newType.Is64() )
|
---|
| 168 | {
|
---|
| 169 | //fld qword ptr[esp]
|
---|
| 170 | compiler.codeGenerator.op_fld_ptr_esp(DEF_DOUBLE);
|
---|
| 171 |
|
---|
| 172 | //fistp qword ptr[esp]
|
---|
| 173 | compiler.codeGenerator.op_fistp_ptr_esp( sizeof(_int64) );
|
---|
| 174 | }
|
---|
| 175 | else{
|
---|
| 176 | //fld qword ptr[esp]
|
---|
| 177 | compiler.codeGenerator.op_fld_ptr_esp(DEF_DOUBLE);
|
---|
| 178 |
|
---|
| 179 | //add esp,4
|
---|
| 180 | compiler.codeGenerator.op_add_esp(4);
|
---|
| 181 |
|
---|
| 182 | //fistp dword ptr[esp]
|
---|
| 183 | compiler.codeGenerator.op_fistp_ptr_esp( sizeof(long) );
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 | else if( oldType.IsSingle() )
|
---|
| 187 | {
|
---|
| 188 | if( newType.Is64() )
|
---|
| 189 | {
|
---|
| 190 | //fld dword ptr[esp]
|
---|
| 191 | compiler.codeGenerator.op_fld_ptr_esp(DEF_SINGLE);
|
---|
| 192 |
|
---|
| 193 | //sub esp,4
|
---|
| 194 | compiler.codeGenerator.op_sub_esp(4);
|
---|
| 195 |
|
---|
| 196 | //fistp qword ptr[esp]
|
---|
| 197 | compiler.codeGenerator.op_fistp_ptr_esp( sizeof(_int64) );
|
---|
| 198 | }
|
---|
| 199 | else{
|
---|
| 200 | //fld dword ptr[esp]
|
---|
| 201 | compiler.codeGenerator.op_fld_ptr_esp(DEF_SINGLE);
|
---|
| 202 |
|
---|
| 203 | //fistp dword ptr[esp]
|
---|
| 204 | compiler.codeGenerator.op_fistp_ptr_esp( sizeof(long) );
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 | else{
|
---|
| 208 | //整数から整数へ変換
|
---|
| 209 |
|
---|
| 210 | if( newType.Is64() )
|
---|
| 211 | {
|
---|
| 212 | ExtendStackTo64( oldType );
|
---|
| 213 | }
|
---|
| 214 | else if( newType.GetSize()==sizeof(long)){
|
---|
| 215 | ExtendStackTo32( oldType );
|
---|
| 216 | }
|
---|
| 217 | else if( newType.GetSize()==sizeof(short)){
|
---|
| 218 | ExtendStackTo16( oldType );
|
---|
| 219 | }
|
---|
| 220 | else if( newType.GetSize()==sizeof(char)){
|
---|
| 221 | ExtendStackTo8( oldType );
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 | }
|
---|