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