cg386cnv.pas 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Generate i386 assembler for type converting nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$ifdef TP}
  19. {$E+,F+,N+,D+,L+,Y+}
  20. {$endif}
  21. unit cg386cnv;
  22. interface
  23. uses
  24. tree;
  25. procedure loadshortstring(p:ptree);
  26. procedure loadlongstring(p:ptree);
  27. procedure loadansi2short(source,dest : ptree);
  28. procedure secondtypeconv(var p : ptree);
  29. procedure secondas(var p : ptree);
  30. procedure secondis(var p : ptree);
  31. implementation
  32. uses
  33. cobjects,verbose,globtype,globals,systems,
  34. symconst,symtable,aasm,types,
  35. hcodegen,temp_gen,pass_2,pass_1,
  36. cpubase,cpuasm,
  37. cgai386,tgeni386;
  38. procedure push_shortstring_length(p:ptree);
  39. var
  40. hightree : ptree;
  41. begin
  42. if is_open_string(p^.resulttype) then
  43. begin
  44. getsymonlyin(p^.symtable,'high'+pvarsym(p^.symtableentry)^.name);
  45. hightree:=genloadnode(pvarsym(srsym),p^.symtable);
  46. firstpass(hightree);
  47. secondpass(hightree);
  48. push_value_para(hightree,false,0,4);
  49. disposetree(hightree);
  50. end
  51. else
  52. begin
  53. push_int(pstringdef(p^.resulttype)^.len);
  54. end;
  55. end;
  56. procedure loadshortstring(p:ptree);
  57. {
  58. Load a string, handles stringdef and orddef (char) types
  59. }
  60. begin
  61. case p^.right^.resulttype^.deftype of
  62. stringdef:
  63. begin
  64. if (p^.right^.treetype=stringconstn) and
  65. (str_length(p^.right)=0) then
  66. emit_const_ref(
  67. A_MOV,S_B,0,newreference(p^.left^.location.reference))
  68. else
  69. begin
  70. emitpushreferenceaddr(p^.left^.location.reference);
  71. emitpushreferenceaddr(p^.right^.location.reference);
  72. push_shortstring_length(p^.left);
  73. emitcall('FPC_SHORTSTR_COPY');
  74. maybe_loadesi;
  75. end;
  76. end;
  77. orddef:
  78. begin
  79. if p^.right^.treetype=ordconstn then
  80. emit_const_ref(
  81. A_MOV,S_W,p^.right^.value*256+1,newreference(p^.left^.location.reference))
  82. else
  83. begin
  84. { not so elegant (goes better with extra register }
  85. if (p^.right^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  86. begin
  87. emit_reg_reg(A_MOV,S_L,makereg32(p^.right^.location.register),R_EDI);
  88. ungetregister(p^.right^.location.register);
  89. end
  90. else
  91. begin
  92. emit_ref_reg(A_MOV,S_L,newreference(p^.right^.location.reference),R_EDI);
  93. del_reference(p^.right^.location.reference);
  94. end;
  95. emit_const_reg(A_SHL,S_L,8,R_EDI);
  96. emit_const_reg(A_OR,S_L,1,R_EDI);
  97. emit_reg_ref(A_MOV,S_W,R_DI,newreference(p^.left^.location.reference));
  98. end;
  99. end;
  100. else
  101. CGMessage(type_e_mismatch);
  102. end;
  103. end;
  104. procedure loadlongstring(p:ptree);
  105. {
  106. Load a string, handles stringdef and orddef (char) types
  107. }
  108. var
  109. r : preference;
  110. begin
  111. case p^.right^.resulttype^.deftype of
  112. stringdef:
  113. begin
  114. if (p^.right^.treetype=stringconstn) and
  115. (str_length(p^.right)=0) then
  116. emit_const_ref(A_MOV,S_L,0,newreference(p^.left^.location.reference))
  117. else
  118. begin
  119. emitpushreferenceaddr(p^.left^.location.reference);
  120. emitpushreferenceaddr(p^.right^.location.reference);
  121. push_shortstring_length(p^.left);
  122. emitcall('FPC_LONGSTR_COPY');
  123. maybe_loadesi;
  124. end;
  125. end;
  126. orddef:
  127. begin
  128. emit_const_ref(A_MOV,S_L,1,newreference(p^.left^.location.reference));
  129. r:=newreference(p^.left^.location.reference);
  130. inc(r^.offset,4);
  131. if p^.right^.treetype=ordconstn then
  132. emit_const_ref(A_MOV,S_B,p^.right^.value,r)
  133. else
  134. begin
  135. case p^.right^.location.loc of
  136. LOC_REGISTER,LOC_CREGISTER:
  137. begin
  138. emit_reg_ref(A_MOV,S_B,p^.right^.location.register,r);
  139. ungetregister(p^.right^.location.register);
  140. end;
  141. LOC_MEM,LOC_REFERENCE:
  142. begin
  143. if not(R_EAX in unused) then
  144. emit_reg(A_PUSH,S_L,R_EAX);
  145. emit_ref_reg(A_MOV,S_B,newreference(p^.right^.location.reference),R_AL);
  146. emit_reg_ref(A_MOV,S_B,R_AL,r);
  147. if not(R_EAX in unused) then
  148. emit_reg(A_POP,S_L,R_EAX);
  149. del_reference(p^.right^.location.reference);
  150. end
  151. else
  152. internalerror(20799);
  153. end;
  154. end;
  155. end;
  156. else
  157. CGMessage(type_e_mismatch);
  158. end;
  159. end;
  160. procedure loadansi2short(source,dest : ptree);
  161. var
  162. pushed : tpushed;
  163. begin
  164. del_reference(dest^.location.reference);
  165. case source^.location.loc of
  166. LOC_REFERENCE,LOC_MEM:
  167. begin
  168. ungetiftemp(source^.location.reference);
  169. {$IfNDef regallocfix}
  170. del_reference(source^.location.reference);
  171. pushusedregisters(pushed,$ff);
  172. emit_push_mem(source^.location.reference);
  173. {$Else regallocfix}
  174. pushusedregisters(pushed,$ff
  175. xor ($80 shr byte(source^.location.reference.base))
  176. xor ($80 shr byte(source^.location.reference.index)));
  177. emit_push_mem(source^.location.reference);
  178. del_reference(source^.location.reference);
  179. {$EndIf regallocfix}
  180. end;
  181. LOC_REGISTER,LOC_CREGISTER:
  182. begin
  183. {$IfNDef regallocfix}
  184. ungetregister32(source^.location.register);
  185. pushusedregisters(pushed,$ff);
  186. emit_reg(A_PUSH,S_L,source^.location.register);
  187. {$Else regallocfix}
  188. pushusedregisters(pushed, $ff xor ($80 shr byte(source^.location.register)));
  189. emit_reg(A_PUSH,S_L,source^.location.register);
  190. ungetregister32(source^.location.register);
  191. {$EndIf regallocfix}
  192. end;
  193. end;
  194. push_shortstring_length(dest);
  195. emitpushreferenceaddr(dest^.location.reference);
  196. emitcall('FPC_ANSISTR_TO_SHORTSTR');
  197. popusedregisters(pushed);
  198. maybe_loadesi;
  199. end;
  200. {*****************************************************************************
  201. SecondTypeConv
  202. *****************************************************************************}
  203. type
  204. tsecondconvproc = procedure(var pto,pfrom : ptree;convtyp : tconverttype);
  205. procedure second_int_to_int(var pto,pfrom : ptree;convtyp : tconverttype);
  206. var
  207. op : tasmop;
  208. opsize : topsize;
  209. hregister,
  210. hregister2 : tregister;
  211. l : pasmlabel;
  212. begin
  213. { insert range check if not explicit conversion }
  214. if not(pto^.explizit) then
  215. emitrangecheck(pfrom,pto^.resulttype);
  216. { is the result size smaller ? }
  217. if pto^.resulttype^.size<pfrom^.resulttype^.size then
  218. begin
  219. { only need to set the new size of a register }
  220. if (pfrom^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  221. begin
  222. case pto^.resulttype^.size of
  223. 1 : pto^.location.register:=makereg8(pfrom^.location.register);
  224. 2 : pto^.location.register:=makereg16(pfrom^.location.register);
  225. 4 : pto^.location.register:=makereg32(pfrom^.location.register);
  226. end;
  227. { we can release the upper register }
  228. if is_64bitint(pfrom^.resulttype) then
  229. ungetregister32(pfrom^.location.registerhigh);
  230. end;
  231. end
  232. { is the result size bigger ? }
  233. else if pto^.resulttype^.size>pfrom^.resulttype^.size then
  234. begin
  235. { remove reference }
  236. if not(pfrom^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  237. begin
  238. del_reference(pfrom^.location.reference);
  239. { we can do this here as we need no temp inside }
  240. ungetiftemp(pfrom^.location.reference);
  241. end;
  242. { get op and opsize, handle separate for constants, because
  243. movz doesn't support constant values }
  244. if (pfrom^.location.loc=LOC_MEM) and (pfrom^.location.reference.is_immediate) then
  245. begin
  246. if is_64bitint(pto^.resulttype) then
  247. opsize:=S_L
  248. else
  249. opsize:=def_opsize(pto^.resulttype);
  250. op:=A_MOV;
  251. end
  252. else
  253. begin
  254. opsize:=def2def_opsize(pfrom^.resulttype,pto^.resulttype);
  255. if opsize in [S_B,S_W,S_L] then
  256. op:=A_MOV
  257. else
  258. if is_signed(pfrom^.resulttype) then
  259. op:=A_MOVSX
  260. else
  261. op:=A_MOVZX;
  262. end;
  263. { load the register we need }
  264. if pfrom^.location.loc<>LOC_REGISTER then
  265. hregister:=getregister32
  266. else
  267. hregister:=pfrom^.location.register;
  268. { set the correct register size and location }
  269. clear_location(pto^.location);
  270. pto^.location.loc:=LOC_REGISTER;
  271. { do we need a second register for a 64 bit type ? }
  272. if is_64bitint(pto^.resulttype) then
  273. begin
  274. hregister2:=getregister32;
  275. pto^.location.registerhigh:=hregister2;
  276. end;
  277. case pto^.resulttype^.size of
  278. 1:
  279. pto^.location.register:=makereg8(hregister);
  280. 2:
  281. pto^.location.register:=makereg16(hregister);
  282. 4,8:
  283. pto^.location.register:=makereg32(hregister);
  284. end;
  285. { insert the assembler code }
  286. if pfrom^.location.loc in [LOC_CREGISTER,LOC_REGISTER] then
  287. emit_reg_reg(op,opsize,pfrom^.location.register,pto^.location.register)
  288. else
  289. emit_ref_reg(op,opsize,
  290. newreference(pfrom^.location.reference),pto^.location.register);
  291. { do we need a sign extension for int64? }
  292. if is_64bitint(pto^.resulttype) then
  293. begin
  294. emit_reg_reg(A_XOR,S_L,
  295. hregister2,hregister2);
  296. if (porddef(pto^.resulttype)^.typ=s64bit) and
  297. is_signed(pfrom^.resulttype) then
  298. begin
  299. getlabel(l);
  300. emit_const_reg(A_TEST,S_L,$80000000,makereg32(hregister));
  301. emitjmp(C_Z,l);
  302. emit_reg(A_NOT,S_L,
  303. hregister2);
  304. emitlab(l);
  305. end;
  306. end;
  307. end;
  308. end;
  309. procedure second_string_to_string(var pto,pfrom : ptree;convtyp : tconverttype);
  310. var
  311. pushed : tpushed;
  312. begin
  313. { does anybody know a better solution than this big case statement ? }
  314. { ok, a proc table would do the job }
  315. case pstringdef(pto^.resulttype)^.string_typ of
  316. st_shortstring:
  317. case pstringdef(pfrom^.resulttype)^.string_typ of
  318. st_shortstring:
  319. begin
  320. gettempofsizereference(pto^.resulttype^.size,pto^.location.reference);
  321. del_reference(pfrom^.location.reference);
  322. copyshortstring(pto^.location.reference,pfrom^.location.reference,
  323. pstringdef(pto^.resulttype)^.len,false);
  324. ungetiftemp(pfrom^.location.reference);
  325. end;
  326. st_longstring:
  327. begin
  328. {!!!!!!!}
  329. internalerror(8888);
  330. end;
  331. st_ansistring:
  332. begin
  333. gettempofsizereference(pto^.resulttype^.size,pto^.location.reference);
  334. loadansi2short(pfrom,pto);
  335. { this is done in secondtypeconv (FK)
  336. removetemps(exprasmlist,temptoremove);
  337. destroys:=true;
  338. }
  339. end;
  340. st_widestring:
  341. begin
  342. {!!!!!!!}
  343. internalerror(8888);
  344. end;
  345. end;
  346. st_longstring:
  347. case pstringdef(pfrom^.resulttype)^.string_typ of
  348. st_shortstring:
  349. begin
  350. {!!!!!!!}
  351. internalerror(8888);
  352. end;
  353. st_ansistring:
  354. begin
  355. {!!!!!!!}
  356. internalerror(8888);
  357. end;
  358. st_widestring:
  359. begin
  360. {!!!!!!!}
  361. internalerror(8888);
  362. end;
  363. end;
  364. st_ansistring:
  365. case pstringdef(pfrom^.resulttype)^.string_typ of
  366. st_shortstring:
  367. begin
  368. clear_location(pto^.location);
  369. pto^.location.loc:=LOC_REFERENCE;
  370. gettempansistringreference(pto^.location.reference);
  371. decrstringref(cansistringdef,pto^.location.reference);
  372. pushusedregisters(pushed,$ff);
  373. emit_push_lea_loc(pfrom^.location,true);
  374. emit_push_lea_loc(pto^.location,false);
  375. emitcall('FPC_SHORTSTR_TO_ANSISTR');
  376. maybe_loadesi;
  377. popusedregisters(pushed);
  378. end;
  379. st_longstring:
  380. begin
  381. {!!!!!!!}
  382. internalerror(8888);
  383. end;
  384. st_widestring:
  385. begin
  386. {!!!!!!!}
  387. internalerror(8888);
  388. end;
  389. end;
  390. st_widestring:
  391. case pstringdef(pfrom^.resulttype)^.string_typ of
  392. st_shortstring:
  393. begin
  394. {!!!!!!!}
  395. internalerror(8888);
  396. end;
  397. st_longstring:
  398. begin
  399. {!!!!!!!}
  400. internalerror(8888);
  401. end;
  402. st_ansistring:
  403. begin
  404. {!!!!!!!}
  405. internalerror(8888);
  406. end;
  407. st_widestring:
  408. begin
  409. {!!!!!!!}
  410. internalerror(8888);
  411. end;
  412. end;
  413. end;
  414. end;
  415. procedure second_cstring_to_pchar(var pto,pfrom : ptree;convtyp : tconverttype);
  416. var
  417. hr : preference;
  418. begin
  419. clear_location(pto^.location);
  420. pto^.location.loc:=LOC_REGISTER;
  421. pto^.location.register:=getregister32;
  422. case pstringdef(pfrom^.resulttype)^.string_typ of
  423. st_shortstring :
  424. begin
  425. inc(pfrom^.location.reference.offset);
  426. emit_ref_reg(A_LEA,S_L,newreference(pfrom^.location.reference),
  427. pto^.location.register);
  428. end;
  429. st_ansistring :
  430. begin
  431. if (pfrom^.treetype=stringconstn) and
  432. (str_length(pfrom)=0) then
  433. begin
  434. new(hr);
  435. reset_reference(hr^);
  436. hr^.symbol:=newasmsymbol('FPC_EMPTYCHAR');
  437. emit_ref_reg(A_LEA,S_L,hr,pto^.location.register);
  438. end
  439. else
  440. emit_ref_reg(A_MOV,S_L,newreference(pfrom^.location.reference),
  441. pto^.location.register);
  442. end;
  443. st_longstring:
  444. begin
  445. {!!!!!!!}
  446. internalerror(8888);
  447. end;
  448. st_widestring:
  449. begin
  450. {!!!!!!!}
  451. internalerror(8888);
  452. end;
  453. end;
  454. end;
  455. procedure second_string_to_chararray(var pto,pfrom : ptree;convtyp : tconverttype);
  456. var
  457. l1 : pasmlabel;
  458. hr : preference;
  459. begin
  460. case pstringdef(pfrom^.resulttype)^.string_typ of
  461. st_shortstring :
  462. begin
  463. inc(pto^.location.reference.offset);
  464. end;
  465. st_ansistring :
  466. begin
  467. clear_location(pto^.location);
  468. pto^.location.loc:=LOC_REFERENCE;
  469. reset_reference(pto^.location.reference);
  470. getlabel(l1);
  471. case pfrom^.location.loc of
  472. LOC_CREGISTER,LOC_REGISTER:
  473. pto^.location.reference.base:=pfrom^.location.register;
  474. LOC_MEM,LOC_REFERENCE:
  475. begin
  476. pto^.location.reference.base:=getregister32;
  477. emit_ref_reg(A_MOV,S_L,newreference(pfrom^.location.reference),
  478. pto^.location.reference.base);
  479. del_reference(pfrom^.location.reference);
  480. end;
  481. end;
  482. emit_const_reg(A_CMP,S_L,0,pto^.location.reference.base);
  483. emitjmp(C_NZ,l1);
  484. new(hr);
  485. reset_reference(hr^);
  486. hr^.symbol:=newasmsymbol('FPC_EMPTYCHAR');
  487. emit_ref_reg(A_LEA,S_L,hr,pto^.location.reference.base);
  488. emitlab(l1);
  489. end;
  490. st_longstring:
  491. begin
  492. {!!!!!!!}
  493. internalerror(8888);
  494. end;
  495. st_widestring:
  496. begin
  497. {!!!!!!!}
  498. internalerror(8888);
  499. end;
  500. end;
  501. end;
  502. procedure second_array_to_pointer(var pto,pfrom : ptree;convtyp : tconverttype);
  503. begin
  504. del_reference(pfrom^.location.reference);
  505. clear_location(pto^.location);
  506. pto^.location.loc:=LOC_REGISTER;
  507. pto^.location.register:=getregister32;
  508. emit_ref_reg(A_LEA,S_L,newreference(pfrom^.location.reference),
  509. pto^.location.register);
  510. end;
  511. procedure second_pointer_to_array(var pto,pfrom : ptree;convtyp : tconverttype);
  512. begin
  513. clear_location(pto^.location);
  514. pto^.location.loc:=LOC_REFERENCE;
  515. reset_reference(pto^.location.reference);
  516. case pfrom^.location.loc of
  517. LOC_REGISTER :
  518. pto^.location.reference.base:=pfrom^.location.register;
  519. LOC_CREGISTER :
  520. begin
  521. pto^.location.reference.base:=getregister32;
  522. emit_reg_reg(A_MOV,S_L,pfrom^.location.register,pto^.location.reference.base);
  523. end
  524. else
  525. begin
  526. del_reference(pfrom^.location.reference);
  527. pto^.location.reference.base:=getregister32;
  528. emit_ref_reg(A_MOV,S_L,newreference(pfrom^.location.reference),
  529. pto^.location.reference.base);
  530. end;
  531. end;
  532. end;
  533. { generates the code for the type conversion from an array of char }
  534. { to a string }
  535. procedure second_chararray_to_string(var pto,pfrom : ptree;convtyp : tconverttype);
  536. var
  537. pushed : tpushed;
  538. l : longint;
  539. begin
  540. { calc the length of the array }
  541. l:=parraydef(pfrom^.resulttype)^.highrange-parraydef(pfrom^.resulttype)^.lowrange+1;
  542. { this is a type conversion which copies the data, so we can't }
  543. { return a reference }
  544. clear_location(pto^.location);
  545. pto^.location.loc:=LOC_MEM;
  546. case pstringdef(pto^.resulttype)^.string_typ of
  547. st_shortstring :
  548. begin
  549. if l>255 then
  550. begin
  551. CGMessage(type_e_mismatch);
  552. l:=255;
  553. end;
  554. gettempofsizereference(pto^.resulttype^.size,pto^.location.reference);
  555. pushusedregisters(pushed,$ff);
  556. if l>=pto^.resulttype^.size then
  557. push_int(pto^.resulttype^.size-1)
  558. else
  559. push_int(l);
  560. emit_push_lea_loc(pfrom^.location,true);
  561. emitpushreferenceaddr(pto^.location.reference);
  562. emitcall('FPC_CHARARRAY_TO_SHORTSTR');
  563. maybe_loadesi;
  564. popusedregisters(pushed);
  565. end;
  566. st_ansistring :
  567. begin
  568. gettempansistringreference(pto^.location.reference);
  569. decrstringref(cansistringdef,pto^.location.reference);
  570. release_loc(pfrom^.location);
  571. pushusedregisters(pushed,$ff);
  572. push_int(l);
  573. emitpushreferenceaddr(pfrom^.location.reference);
  574. emitpushreferenceaddr(pto^.location.reference);
  575. emitcall('FPC_CHARARRAY_TO_ANSISTR');
  576. popusedregisters(pushed);
  577. maybe_loadesi;
  578. end;
  579. st_longstring:
  580. begin
  581. {!!!!!!!}
  582. internalerror(8888);
  583. end;
  584. st_widestring:
  585. begin
  586. {!!!!!!!}
  587. internalerror(8888);
  588. end;
  589. end;
  590. end;
  591. procedure second_char_to_string(var pto,pfrom : ptree;convtyp : tconverttype);
  592. var
  593. pushed : tpushed;
  594. begin
  595. clear_location(pto^.location);
  596. pto^.location.loc:=LOC_MEM;
  597. case pstringdef(pto^.resulttype)^.string_typ of
  598. st_shortstring :
  599. begin
  600. gettempofsizereference(256,pto^.location.reference);
  601. { call loadstring with correct left and right }
  602. pto^.right:=pfrom;
  603. pto^.left:=pto;
  604. loadshortstring(pto);
  605. pto^.left:=nil; { reset left tree, which is empty }
  606. { pto^.right is not disposed for typeconv !! PM }
  607. disposetree(pto^.right);
  608. pto^.right:=nil;
  609. end;
  610. st_ansistring :
  611. begin
  612. gettempansistringreference(pto^.location.reference);
  613. decrstringref(cansistringdef,pto^.location.reference);
  614. release_loc(pfrom^.location);
  615. pushusedregisters(pushed,$ff);
  616. emit_pushw_loc(pfrom^.location);
  617. emitpushreferenceaddr(pto^.location.reference);
  618. emitcall('FPC_CHAR_TO_ANSISTR');
  619. popusedregisters(pushed);
  620. maybe_loadesi;
  621. end;
  622. else
  623. internalerror(4179);
  624. end;
  625. end;
  626. procedure second_int_to_real(var pto,pfrom : ptree;convtyp : tconverttype);
  627. var
  628. r : preference;
  629. hregister : tregister;
  630. l1,l2 : pasmlabel;
  631. begin
  632. { for u32bit a solution is to push $0 and to load a comp }
  633. { does this first, it destroys maybe EDI }
  634. hregister:=R_EDI;
  635. if porddef(pfrom^.resulttype)^.typ=u32bit then
  636. push_int(0);
  637. if (pfrom^.location.loc=LOC_REGISTER) or
  638. (pfrom^.location.loc=LOC_CREGISTER) then
  639. begin
  640. case porddef(pfrom^.resulttype)^.typ of
  641. s8bit : emit_reg_reg(A_MOVSX,S_BL,pfrom^.location.register,R_EDI);
  642. u8bit : emit_reg_reg(A_MOVZX,S_BL,pfrom^.location.register,R_EDI);
  643. s16bit : emit_reg_reg(A_MOVSX,S_WL,pfrom^.location.register,R_EDI);
  644. u16bit : emit_reg_reg(A_MOVZX,S_WL,pfrom^.location.register,R_EDI);
  645. u32bit,s32bit:
  646. hregister:=pfrom^.location.register;
  647. u64bit,s64bit:
  648. begin
  649. emit_reg(A_PUSH,S_L,pfrom^.location.registerhigh);
  650. hregister:=pfrom^.location.registerlow;
  651. end;
  652. end;
  653. ungetregister(pfrom^.location.register);
  654. end
  655. else
  656. begin
  657. r:=newreference(pfrom^.location.reference);
  658. case porddef(pfrom^.resulttype)^.typ of
  659. s8bit:
  660. emit_ref_reg(A_MOVSX,S_BL,r,R_EDI);
  661. u8bit:
  662. emit_ref_reg(A_MOVZX,S_BL,r,R_EDI);
  663. s16bit:
  664. emit_ref_reg(A_MOVSX,S_WL,r,R_EDI);
  665. u16bit:
  666. emit_ref_reg(A_MOVZX,S_WL,r,R_EDI);
  667. u32bit,s32bit:
  668. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  669. u64bit,s64bit:
  670. begin
  671. inc(r^.offset,4);
  672. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  673. emit_reg(A_PUSH,S_L,R_EDI);
  674. r:=newreference(pfrom^.location.reference);
  675. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  676. end;
  677. end;
  678. del_reference(pfrom^.location.reference);
  679. ungetiftemp(pfrom^.location.reference);
  680. end;
  681. { for 64 bit integers, the high dword is already pushed }
  682. emit_reg(A_PUSH,S_L,hregister);
  683. r:=new_reference(R_ESP,0);
  684. case porddef(pfrom^.resulttype)^.typ of
  685. u32bit:
  686. begin
  687. emit_ref(A_FILD,S_IQ,r);
  688. emit_const_reg(A_ADD,S_L,8,R_ESP);
  689. end;
  690. s64bit:
  691. begin
  692. emit_ref(A_FILD,S_IQ,r);
  693. emit_const_reg(A_ADD,S_L,8,R_ESP);
  694. end;
  695. u64bit:
  696. begin
  697. { unsigned 64 bit ints are harder to handle: }
  698. { we load bits 0..62 and then check bit 63: }
  699. { if it is 1 then we add $80000000 000000000 }
  700. { as double }
  701. inc(r^.offset,4);
  702. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  703. r:=new_reference(R_ESP,4);
  704. emit_const_ref(A_AND,S_L,$7fffffff,r);
  705. emit_const_reg(A_AND,S_L,$80000000,R_EDI);
  706. r:=new_reference(R_ESP,0);
  707. emit_ref(A_FILD,S_IQ,r);
  708. getdatalabel(l1);
  709. getlabel(l2);
  710. emitjmp(C_Z,l2);
  711. consts^.concat(new(pai_label,init(l1)));
  712. { I got this constant from a test progtram (FK) }
  713. consts^.concat(new(pai_const,init_32bit(0)));
  714. consts^.concat(new(pai_const,init_32bit(1138753536)));
  715. r:=new_reference(R_NO,0);
  716. r^.symbol:=l1;
  717. emit_ref(A_FADD,S_FL,r);
  718. emitlab(l2);
  719. emit_const_reg(A_ADD,S_L,8,R_ESP);
  720. end
  721. else
  722. begin
  723. emit_ref(A_FILD,S_IL,r);
  724. emit_reg(A_POP,S_L,R_EDI);
  725. end;
  726. end;
  727. inc(fpuvaroffset);
  728. clear_location(pto^.location);
  729. pto^.location.loc:=LOC_FPU;
  730. end;
  731. procedure second_real_to_fix(var pto,pfrom : ptree;convtyp : tconverttype);
  732. var
  733. rreg : tregister;
  734. ref : treference;
  735. begin
  736. { real must be on fpu stack }
  737. if (pfrom^.location.loc<>LOC_FPU) then
  738. emit_ref(A_FLD,S_FL,newreference(pfrom^.location.reference));
  739. push_int($1f3f);
  740. push_int(65536);
  741. reset_reference(ref);
  742. ref.base:=R_ESP;
  743. emit_ref(A_FIMUL,S_IL,newreference(ref));
  744. ref.offset:=4;
  745. emit_ref(A_FSTCW,S_NO,newreference(ref));
  746. ref.offset:=6;
  747. emit_ref(A_FLDCW,S_NO,newreference(ref));
  748. ref.offset:=0;
  749. emit_ref(A_FISTP,S_IL,newreference(ref));
  750. ref.offset:=4;
  751. emit_ref(A_FLDCW,S_NO,newreference(ref));
  752. rreg:=getregister32;
  753. emit_reg(A_POP,S_L,rreg);
  754. { better than an add on all processors }
  755. emit_reg(A_POP,S_L,R_EDI);
  756. clear_location(pto^.location);
  757. pto^.location.loc:=LOC_REGISTER;
  758. pto^.location.register:=rreg;
  759. inc(fpuvaroffset);
  760. end;
  761. procedure second_real_to_real(var pto,pfrom : ptree;convtyp : tconverttype);
  762. begin
  763. case pfrom^.location.loc of
  764. LOC_FPU : ;
  765. LOC_CFPUREGISTER:
  766. begin
  767. pto^.location:=pfrom^.location;
  768. exit;
  769. end;
  770. LOC_MEM,
  771. LOC_REFERENCE:
  772. begin
  773. floatload(pfloatdef(pfrom^.resulttype)^.typ,
  774. pfrom^.location.reference);
  775. { we have to free the reference }
  776. del_reference(pfrom^.location.reference);
  777. end;
  778. end;
  779. clear_location(pto^.location);
  780. pto^.location.loc:=LOC_FPU;
  781. end;
  782. procedure second_fix_to_real(var pto,pfrom : ptree;convtyp : tconverttype);
  783. var
  784. popeax,popebx,popecx,popedx : boolean;
  785. startreg : tregister;
  786. hl : pasmlabel;
  787. r : treference;
  788. begin
  789. if (pfrom^.location.loc=LOC_REGISTER) or
  790. (pfrom^.location.loc=LOC_CREGISTER) then
  791. begin
  792. startreg:=pfrom^.location.register;
  793. ungetregister(startreg);
  794. popeax:=(startreg<>R_EAX) and not (R_EAX in unused);
  795. if popeax then
  796. emit_reg(A_PUSH,S_L,R_EAX);
  797. { mov eax,eax is removed by emit_reg_reg }
  798. emit_reg_reg(A_MOV,S_L,startreg,R_EAX);
  799. end
  800. else
  801. begin
  802. emit_ref_reg(A_MOV,S_L,newreference(
  803. pfrom^.location.reference),R_EAX);
  804. del_reference(pfrom^.location.reference);
  805. startreg:=R_NO;
  806. end;
  807. popebx:=(startreg<>R_EBX) and not (R_EBX in unused);
  808. if popebx then
  809. emit_reg(A_PUSH,S_L,R_EBX);
  810. popecx:=(startreg<>R_ECX) and not (R_ECX in unused);
  811. if popecx then
  812. emit_reg(A_PUSH,S_L,R_ECX);
  813. popedx:=(startreg<>R_EDX) and not (R_EDX in unused);
  814. if popedx then
  815. emit_reg(A_PUSH,S_L,R_EDX);
  816. emit_none(A_CDQ,S_NO);
  817. emit_reg_reg(A_XOR,S_L,R_EDX,R_EAX);
  818. emit_reg_reg(A_MOV,S_L,R_EAX,R_EBX);
  819. emit_reg_reg(A_SUB,S_L,R_EDX,R_EAX);
  820. getlabel(hl);
  821. emitjmp(C_Z,hl);
  822. emit_const_reg(A_RCL,S_L,1,R_EBX);
  823. emit_reg_reg(A_BSR,S_L,R_EAX,R_EDX);
  824. emit_const_reg(A_MOV,S_B,32,R_CL);
  825. emit_reg_reg(A_SUB,S_B,R_DL,R_CL);
  826. emit_reg_reg(A_SHL,S_L,R_CL,R_EAX);
  827. emit_const_reg(A_ADD,S_W,1007,R_DX);
  828. emit_const_reg(A_SHL,S_W,5,R_DX);
  829. emit_const_reg_reg(A_SHLD,S_W,11,R_DX,R_BX);
  830. emit_const_reg_reg(A_SHLD,S_L,20,R_EAX,R_EBX);
  831. emit_const_reg(A_SHL,S_L,20,R_EAX);
  832. emitlab(hl);
  833. { better than an add on all processors }
  834. emit_reg(A_PUSH,S_L,R_EBX);
  835. emit_reg(A_PUSH,S_L,R_EAX);
  836. reset_reference(r);
  837. r.base:=R_ESP;
  838. emit_ref(A_FLD,S_FL,newreference(r));
  839. emit_const_reg(A_ADD,S_L,8,R_ESP);
  840. if popedx then
  841. emit_reg(A_POP,S_L,R_EDX);
  842. if popecx then
  843. emit_reg(A_POP,S_L,R_ECX);
  844. if popebx then
  845. emit_reg(A_POP,S_L,R_EBX);
  846. if popeax then
  847. emit_reg(A_POP,S_L,R_EAX);
  848. clear_location(pto^.location);
  849. pto^.location.loc:=LOC_FPU;
  850. end;
  851. procedure second_cord_to_pointer(var pto,pfrom : ptree;convtyp : tconverttype);
  852. begin
  853. { this can't happend, because constants are already processed in
  854. pass 1 }
  855. internalerror(47423985);
  856. end;
  857. procedure second_int_to_fix(var pto,pfrom : ptree;convtyp : tconverttype);
  858. var
  859. hregister : tregister;
  860. begin
  861. if (pfrom^.location.loc=LOC_REGISTER) then
  862. hregister:=pfrom^.location.register
  863. else if (pfrom^.location.loc=LOC_CREGISTER) then
  864. hregister:=getregister32
  865. else
  866. begin
  867. del_reference(pfrom^.location.reference);
  868. hregister:=getregister32;
  869. case porddef(pfrom^.resulttype)^.typ of
  870. s8bit : emit_ref_reg(A_MOVSX,S_BL,newreference(pfrom^.location.reference),
  871. hregister);
  872. u8bit : emit_ref_reg(A_MOVZX,S_BL,newreference(pfrom^.location.reference),
  873. hregister);
  874. s16bit : emit_ref_reg(A_MOVSX,S_WL,newreference(pfrom^.location.reference),
  875. hregister);
  876. u16bit : emit_ref_reg(A_MOVZX,S_WL,newreference(pfrom^.location.reference),
  877. hregister);
  878. u32bit,s32bit : emit_ref_reg(A_MOV,S_L,newreference(pfrom^.location.reference),
  879. hregister);
  880. {!!!! u32bit }
  881. end;
  882. end;
  883. emit_const_reg(A_SHL,S_L,16,hregister);
  884. clear_location(pto^.location);
  885. pto^.location.loc:=LOC_REGISTER;
  886. pto^.location.register:=hregister;
  887. end;
  888. procedure second_proc_to_procvar(var pto,pfrom : ptree;convtyp : tconverttype);
  889. begin
  890. { method pointer ? }
  891. if assigned(pfrom^.left) then
  892. begin
  893. set_location(pto^.location,pfrom^.location);
  894. end
  895. else
  896. begin
  897. clear_location(pto^.location);
  898. pto^.location.loc:=LOC_REGISTER;
  899. pto^.location.register:=getregister32;
  900. del_reference(pfrom^.location.reference);
  901. emit_ref_reg(A_LEA,S_L,
  902. newreference(pfrom^.location.reference),pto^.location.register);
  903. end;
  904. end;
  905. procedure second_bool_to_int(var pto,pfrom : ptree;convtyp : tconverttype);
  906. var
  907. oldtruelabel,oldfalselabel,hlabel : pasmlabel;
  908. hregister : tregister;
  909. newsize,
  910. opsize : topsize;
  911. op : tasmop;
  912. begin
  913. oldtruelabel:=truelabel;
  914. oldfalselabel:=falselabel;
  915. getlabel(truelabel);
  916. getlabel(falselabel);
  917. secondpass(pfrom);
  918. { byte(boolean) or word(wordbool) or longint(longbool) must
  919. be accepted for var parameters }
  920. if (pto^.explizit) and
  921. (pfrom^.resulttype^.size=pto^.resulttype^.size) and
  922. (pfrom^.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  923. begin
  924. set_location(pto^.location,pfrom^.location);
  925. truelabel:=oldtruelabel;
  926. falselabel:=oldfalselabel;
  927. exit;
  928. end;
  929. clear_location(pto^.location);
  930. pto^.location.loc:=LOC_REGISTER;
  931. del_reference(pfrom^.location.reference);
  932. case pfrom^.resulttype^.size of
  933. 1 : begin
  934. case pto^.resulttype^.size of
  935. 1 : opsize:=S_B;
  936. 2 : opsize:=S_BW;
  937. 4 : opsize:=S_BL;
  938. end;
  939. end;
  940. 2 : begin
  941. case pto^.resulttype^.size of
  942. 1 : begin
  943. if pfrom^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  944. pfrom^.location.register:=reg16toreg8(pfrom^.location.register);
  945. opsize:=S_B;
  946. end;
  947. 2 : opsize:=S_W;
  948. 4 : opsize:=S_WL;
  949. end;
  950. end;
  951. 4 : begin
  952. case pto^.resulttype^.size of
  953. 1 : begin
  954. if pfrom^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  955. pfrom^.location.register:=reg32toreg8(pfrom^.location.register);
  956. opsize:=S_B;
  957. end;
  958. 2 : begin
  959. if pfrom^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  960. pfrom^.location.register:=reg32toreg16(pfrom^.location.register);
  961. opsize:=S_W;
  962. end;
  963. 4 : opsize:=S_L;
  964. end;
  965. end;
  966. end;
  967. if opsize in [S_B,S_W,S_L] then
  968. op:=A_MOV
  969. else
  970. if is_signed(pto^.resulttype) then
  971. op:=A_MOVSX
  972. else
  973. op:=A_MOVZX;
  974. hregister:=getregister32;
  975. case pto^.resulttype^.size of
  976. 1 : begin
  977. pto^.location.register:=reg32toreg8(hregister);
  978. newsize:=S_B;
  979. end;
  980. 2 : begin
  981. pto^.location.register:=reg32toreg16(hregister);
  982. newsize:=S_W;
  983. end;
  984. 4 : begin
  985. pto^.location.register:=hregister;
  986. newsize:=S_L;
  987. end;
  988. else
  989. internalerror(10060);
  990. end;
  991. case pfrom^.location.loc of
  992. LOC_MEM,
  993. LOC_REFERENCE : emit_ref_reg(op,opsize,
  994. newreference(pfrom^.location.reference),pto^.location.register);
  995. LOC_REGISTER,
  996. LOC_CREGISTER : begin
  997. { remove things like movb %al,%al }
  998. if pfrom^.location.register<>pto^.location.register then
  999. emit_reg_reg(op,opsize,
  1000. pfrom^.location.register,pto^.location.register);
  1001. end;
  1002. LOC_FLAGS : begin
  1003. emit_flag2reg(pfrom^.location.resflags,pto^.location.register);
  1004. end;
  1005. LOC_JUMP : begin
  1006. getlabel(hlabel);
  1007. emitlab(truelabel);
  1008. emit_const_reg(A_MOV,newsize,1,pto^.location.register);
  1009. emitjmp(C_None,hlabel);
  1010. emitlab(falselabel);
  1011. emit_reg_reg(A_XOR,newsize,pto^.location.register,
  1012. pto^.location.register);
  1013. emitlab(hlabel);
  1014. end;
  1015. else
  1016. internalerror(10061);
  1017. end;
  1018. truelabel:=oldtruelabel;
  1019. falselabel:=oldfalselabel;
  1020. end;
  1021. procedure second_int_to_bool(var pto,pfrom : ptree;convtyp : tconverttype);
  1022. var
  1023. hregister : tregister;
  1024. flags : tresflags;
  1025. opsize : topsize;
  1026. begin
  1027. clear_location(pto^.location);
  1028. { byte(boolean) or word(wordbool) or longint(longbool) must
  1029. be accepted for var parameters }
  1030. if (pto^.explizit) and
  1031. (pfrom^.resulttype^.size=pto^.resulttype^.size) and
  1032. (pfrom^.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  1033. begin
  1034. set_location(pto^.location,pfrom^.location);
  1035. exit;
  1036. end;
  1037. pto^.location.loc:=LOC_REGISTER;
  1038. del_reference(pfrom^.location.reference);
  1039. opsize:=def_opsize(pfrom^.resulttype);
  1040. case pfrom^.location.loc of
  1041. LOC_MEM,LOC_REFERENCE :
  1042. begin
  1043. hregister:=def_getreg(pfrom^.resulttype);
  1044. emit_ref_reg(A_MOV,opsize,
  1045. newreference(pfrom^.location.reference),hregister);
  1046. emit_reg_reg(A_OR,opsize,hregister,hregister);
  1047. flags:=F_NE;
  1048. end;
  1049. LOC_FLAGS :
  1050. begin
  1051. hregister:=getregister32;
  1052. flags:=pfrom^.location.resflags;
  1053. end;
  1054. LOC_REGISTER,LOC_CREGISTER :
  1055. begin
  1056. hregister:=pfrom^.location.register;
  1057. emit_reg_reg(A_OR,opsize,hregister,hregister);
  1058. flags:=F_NE;
  1059. end;
  1060. else
  1061. internalerror(10062);
  1062. end;
  1063. case pto^.resulttype^.size of
  1064. 1 : pto^.location.register:=makereg8(hregister);
  1065. 2 : pto^.location.register:=makereg16(hregister);
  1066. 4 : pto^.location.register:=makereg32(hregister);
  1067. else
  1068. internalerror(10064);
  1069. end;
  1070. emit_flag2reg(flags,pto^.location.register);
  1071. end;
  1072. procedure second_load_smallset(var pto,pfrom : ptree;convtyp : tconverttype);
  1073. var
  1074. href : treference;
  1075. pushedregs : tpushed;
  1076. begin
  1077. href.symbol:=nil;
  1078. pushusedregisters(pushedregs,$ff);
  1079. gettempofsizereference(32,href);
  1080. emitpushreferenceaddr(pfrom^.location.reference);
  1081. emitpushreferenceaddr(href);
  1082. emitcall('FPC_SET_LOAD_SMALL');
  1083. maybe_loadesi;
  1084. popusedregisters(pushedregs);
  1085. clear_location(pto^.location);
  1086. pto^.location.loc:=LOC_MEM;
  1087. pto^.location.reference:=href;
  1088. end;
  1089. procedure second_ansistring_to_pchar(var pto,pfrom : ptree;convtyp : tconverttype);
  1090. var
  1091. l1 : pasmlabel;
  1092. hr : preference;
  1093. begin
  1094. clear_location(pto^.location);
  1095. pto^.location.loc:=LOC_REGISTER;
  1096. getlabel(l1);
  1097. case pfrom^.location.loc of
  1098. LOC_CREGISTER,LOC_REGISTER:
  1099. pto^.location.register:=pfrom^.location.register;
  1100. LOC_MEM,LOC_REFERENCE:
  1101. begin
  1102. pto^.location.register:=getregister32;
  1103. emit_ref_reg(A_MOV,S_L,newreference(pfrom^.location.reference),
  1104. pto^.location.register);
  1105. del_reference(pfrom^.location.reference);
  1106. end;
  1107. end;
  1108. emit_const_reg(A_CMP,S_L,0,pto^.location.register);
  1109. emitjmp(C_NZ,l1);
  1110. new(hr);
  1111. reset_reference(hr^);
  1112. hr^.symbol:=newasmsymbol('FPC_EMPTYCHAR');
  1113. emit_ref_reg(A_LEA,S_L,hr,pto^.location.register);
  1114. emitlab(l1);
  1115. end;
  1116. procedure second_pchar_to_string(var pto,pfrom : ptree;convtyp : tconverttype);
  1117. var
  1118. pushed : tpushed;
  1119. begin
  1120. case pstringdef(pto^.resulttype)^.string_typ of
  1121. st_shortstring:
  1122. begin
  1123. pto^.location.loc:=LOC_REFERENCE;
  1124. gettempofsizereference(pto^.resulttype^.size,pto^.location.reference);
  1125. pushusedregisters(pushed,$ff);
  1126. case pfrom^.location.loc of
  1127. LOC_REGISTER,LOC_CREGISTER:
  1128. begin
  1129. emit_reg(A_PUSH,S_L,pfrom^.location.register);
  1130. ungetregister32(pfrom^.location.register);
  1131. end;
  1132. LOC_REFERENCE,LOC_MEM:
  1133. begin
  1134. emit_push_mem(pfrom^.location.reference);
  1135. del_reference(pfrom^.location.reference);
  1136. end;
  1137. end;
  1138. emitpushreferenceaddr(pto^.location.reference);
  1139. emitcall('FPC_PCHAR_TO_SHORTSTR');
  1140. maybe_loadesi;
  1141. popusedregisters(pushed);
  1142. end;
  1143. st_ansistring:
  1144. begin
  1145. pto^.location.loc:=LOC_REFERENCE;
  1146. gettempansistringreference(pto^.location.reference);
  1147. decrstringref(cansistringdef,pto^.location.reference);
  1148. case pfrom^.location.loc of
  1149. LOC_REFERENCE,LOC_MEM:
  1150. begin
  1151. {$IfNDef regallocfix}
  1152. del_reference(pfrom^.location.reference);
  1153. pushusedregisters(pushed,$ff);
  1154. emit_push_mem(pfrom^.location.reference);
  1155. {$Else regallocfix}
  1156. pushusedregisters(pushed,$ff
  1157. xor ($80 shr byte(pfrom^.location.reference.base))
  1158. xor ($80 shr byte(pfrom^.location.reference.index)));
  1159. emit_push_mem(pfrom^.location.reference);
  1160. del_reference(pfrom^.location.reference);
  1161. {$EndIf regallocfix}
  1162. end;
  1163. LOC_REGISTER,LOC_CREGISTER:
  1164. begin
  1165. {$IfNDef regallocfix}
  1166. ungetregister32(pfrom^.location.register);
  1167. pushusedregisters(pushed,$ff);
  1168. emit_reg(A_PUSH,S_L,pfrom^.location.register);
  1169. {$Else regallocfix}
  1170. pushusedregisters(pushed, $ff xor ($80 shr byte(pfrom^.location.register)));
  1171. emit_reg(A_PUSH,S_L,pfrom^.location.register);
  1172. ungetregister32(pfrom^.location.register);
  1173. {$EndIf regallocfix}
  1174. end;
  1175. end;
  1176. emitpushreferenceaddr(pto^.location.reference);
  1177. emitcall('FPC_PCHAR_TO_ANSISTR');
  1178. maybe_loadesi;
  1179. popusedregisters(pushed);
  1180. end;
  1181. else
  1182. begin
  1183. internalerror(12121);
  1184. end;
  1185. end;
  1186. end;
  1187. procedure second_nothing(var pto,pfrom : ptree;convtyp : tconverttype);
  1188. begin
  1189. end;
  1190. {****************************************************************************
  1191. SecondTypeConv
  1192. ****************************************************************************}
  1193. procedure secondtypeconv(var p : ptree);
  1194. const
  1195. secondconvert : array[tconverttype] of tsecondconvproc = (
  1196. second_nothing, {equal}
  1197. second_nothing, {not_possible}
  1198. second_string_to_string,
  1199. second_char_to_string,
  1200. second_pchar_to_string,
  1201. second_nothing, {cchar_to_pchar}
  1202. second_cstring_to_pchar,
  1203. second_ansistring_to_pchar,
  1204. second_string_to_chararray,
  1205. second_chararray_to_string,
  1206. second_array_to_pointer,
  1207. second_pointer_to_array,
  1208. second_int_to_int,
  1209. second_int_to_bool,
  1210. second_bool_to_int, { bool_to_bool }
  1211. second_bool_to_int,
  1212. second_real_to_real,
  1213. second_int_to_real,
  1214. second_int_to_fix,
  1215. second_real_to_fix,
  1216. second_fix_to_real,
  1217. second_proc_to_procvar,
  1218. second_nothing, {arrayconstructor_to_set}
  1219. second_load_smallset,
  1220. second_cord_to_pointer
  1221. );
  1222. {$ifdef TESTOBJEXT2}
  1223. var
  1224. r : preference;
  1225. nillabel : plabel;
  1226. {$endif TESTOBJEXT2}
  1227. begin
  1228. { this isn't good coding, I think tc_bool_2_int, shouldn't be }
  1229. { type conversion (FK) }
  1230. if not(p^.convtyp in [tc_bool_2_int,tc_bool_2_bool]) then
  1231. begin
  1232. secondpass(p^.left);
  1233. set_location(p^.location,p^.left^.location);
  1234. if codegenerror then
  1235. exit;
  1236. end;
  1237. { the second argument only is for maybe_range_checking !}
  1238. secondconvert[p^.convtyp](p,p^.left,p^.convtyp);
  1239. {$ifdef TESTOBJEXT2}
  1240. { Check explicit conversions to objects pointers !! }
  1241. if p^.explizit and
  1242. (p^.resulttype^.deftype=pointerdef) and
  1243. (ppointerdef(p^.resulttype)^.definition^.deftype=objectdef) and not
  1244. (pobjectdef(ppointerdef(p^.resulttype)^.definition)^.isclass) and
  1245. ((pobjectdef(ppointerdef(p^.resulttype)^.definition)^.options and oo_hasvmt)<>0) and
  1246. (cs_check_range in aktlocalswitches) then
  1247. begin
  1248. new(r);
  1249. reset_reference(r^);
  1250. if p^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  1251. r^.base:=p^.location.register
  1252. else
  1253. begin
  1254. emit_mov_loc_reg(p^.location,R_EDI);
  1255. r^.base:=R_EDI;
  1256. end;
  1257. { NIL must be accepted !! }
  1258. emit_reg_reg(A_OR,S_L,r^.base,r^.base);
  1259. getlabel(nillabel);
  1260. emitjmp(C_E,nillabel);
  1261. { this is one point where we need vmt_offset (PM) }
  1262. r^.offset:= pobjectdef(ppointerdef(p^.resulttype)^.definition)^.vmt_offset;
  1263. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  1264. emit_sym(A_PUSH,S_L,
  1265. newasmsymbol(pobjectdef(ppointerdef(p^.resulttype)^.definition)^.vmt_mangledname));
  1266. emit_reg(A_PUSH,S_L,R_EDI);
  1267. emitcall('FPC_CHECK_OBJECT_EXT');
  1268. emitlab(nillabel);
  1269. end;
  1270. {$endif TESTOBJEXT2}
  1271. end;
  1272. {*****************************************************************************
  1273. SecondIs
  1274. *****************************************************************************}
  1275. procedure secondis(var p : ptree);
  1276. var
  1277. pushed : tpushed;
  1278. begin
  1279. { save all used registers }
  1280. pushusedregisters(pushed,$ff);
  1281. secondpass(p^.left);
  1282. clear_location(p^.location);
  1283. p^.location.loc:=LOC_FLAGS;
  1284. p^.location.resflags:=F_NE;
  1285. { push instance to check: }
  1286. case p^.left^.location.loc of
  1287. LOC_REGISTER,LOC_CREGISTER:
  1288. begin
  1289. emit_reg(A_PUSH,
  1290. S_L,p^.left^.location.register);
  1291. ungetregister32(p^.left^.location.register);
  1292. end;
  1293. LOC_MEM,LOC_REFERENCE:
  1294. begin
  1295. emit_ref(A_PUSH,
  1296. S_L,newreference(p^.left^.location.reference));
  1297. del_reference(p^.left^.location.reference);
  1298. end;
  1299. else internalerror(100);
  1300. end;
  1301. { generate type checking }
  1302. secondpass(p^.right);
  1303. case p^.right^.location.loc of
  1304. LOC_REGISTER,LOC_CREGISTER:
  1305. begin
  1306. emit_reg(A_PUSH,
  1307. S_L,p^.right^.location.register);
  1308. ungetregister32(p^.right^.location.register);
  1309. end;
  1310. LOC_MEM,LOC_REFERENCE:
  1311. begin
  1312. emit_ref(A_PUSH,
  1313. S_L,newreference(p^.right^.location.reference));
  1314. del_reference(p^.right^.location.reference);
  1315. end;
  1316. else internalerror(100);
  1317. end;
  1318. emitcall('FPC_DO_IS');
  1319. emit_reg_reg(A_OR,S_B,R_AL,R_AL);
  1320. popusedregisters(pushed);
  1321. maybe_loadesi;
  1322. end;
  1323. {*****************************************************************************
  1324. SecondAs
  1325. *****************************************************************************}
  1326. procedure secondas(var p : ptree);
  1327. var
  1328. pushed : tpushed;
  1329. begin
  1330. secondpass(p^.left);
  1331. { save all used registers }
  1332. pushusedregisters(pushed,$ff);
  1333. { push instance to check: }
  1334. case p^.left^.location.loc of
  1335. LOC_REGISTER,LOC_CREGISTER:
  1336. emit_reg(A_PUSH,
  1337. S_L,p^.left^.location.register);
  1338. LOC_MEM,LOC_REFERENCE:
  1339. emit_ref(A_PUSH,
  1340. S_L,newreference(p^.left^.location.reference));
  1341. else internalerror(100);
  1342. end;
  1343. { we doesn't modifiy the left side, we check only the type }
  1344. set_location(p^.location,p^.left^.location);
  1345. { generate type checking }
  1346. secondpass(p^.right);
  1347. case p^.right^.location.loc of
  1348. LOC_REGISTER,LOC_CREGISTER:
  1349. begin
  1350. emit_reg(A_PUSH,
  1351. S_L,p^.right^.location.register);
  1352. ungetregister32(p^.right^.location.register);
  1353. end;
  1354. LOC_MEM,LOC_REFERENCE:
  1355. begin
  1356. emit_ref(A_PUSH,
  1357. S_L,newreference(p^.right^.location.reference));
  1358. del_reference(p^.right^.location.reference);
  1359. end;
  1360. else internalerror(100);
  1361. end;
  1362. emitcall('FPC_DO_AS');
  1363. { restore register, this restores automatically the }
  1364. { result }
  1365. popusedregisters(pushed);
  1366. maybe_loadesi;
  1367. end;
  1368. end.
  1369. {
  1370. $Log$
  1371. Revision 1.98 2000-01-07 01:14:20 peter
  1372. * updated copyright to 2000
  1373. Revision 1.97 1999/12/22 01:01:46 peter
  1374. - removed freelabel()
  1375. * added undefined label detection in internal assembler, this prevents
  1376. a lot of ld crashes and wrong .o files
  1377. * .o files aren't written anymore if errors have occured
  1378. * inlining of assembler labels is now correct
  1379. Revision 1.96 1999/12/21 11:49:51 pierre
  1380. * array of char to short string bug fixed
  1381. Revision 1.95 1999/12/01 12:42:31 peter
  1382. * fixed bug 698
  1383. * removed some notes about unused vars
  1384. Revision 1.94 1999/11/29 22:15:25 pierre
  1385. * fix for internalerror(12) on ord(booleanarray[1])
  1386. Revision 1.93 1999/11/06 14:34:17 peter
  1387. * truncated log to 20 revs
  1388. Revision 1.92 1999/10/25 10:32:43 peter
  1389. * ansistring 2 chararray support
  1390. * optimized ansitring 2 pchar
  1391. Revision 1.91 1999/10/22 14:36:04 peter
  1392. * fixed esi reload with as
  1393. Revision 1.90 1999/10/06 08:32:00 peter
  1394. * fixed empty const ansistring 2 pchar
  1395. Revision 1.89 1999/09/26 21:30:15 peter
  1396. + constant pointer support which can happend with typecasting like
  1397. const p=pointer(1)
  1398. * better procvar parsing in typed consts
  1399. Revision 1.88 1999/09/26 13:26:04 florian
  1400. * exception patch of Romio nevertheless the excpetion handling
  1401. needs some corections regarding register saving
  1402. * gettempansistring is again a procedure
  1403. Revision 1.87 1999/09/23 21:20:37 peter
  1404. * fixed temp allocation for short->ansi
  1405. Revision 1.86 1999/09/01 09:42:13 peter
  1406. * update for new push_lea_loc
  1407. Revision 1.85 1999/08/19 13:08:46 pierre
  1408. * emit_??? used
  1409. Revision 1.84 1999/08/05 14:58:03 florian
  1410. * some fixes for the floating point registers
  1411. * more things for the new code generator
  1412. Revision 1.83 1999/08/04 13:45:19 florian
  1413. + floating point register variables !!
  1414. * pairegalloc is now generated for register variables
  1415. Revision 1.82 1999/08/04 00:22:43 florian
  1416. * renamed i386asm and i386base to cpuasm and cpubase
  1417. Revision 1.81 1999/08/03 22:02:36 peter
  1418. * moved bitmask constants to sets
  1419. * some other type/const renamings
  1420. Revision 1.80 1999/08/01 23:36:38 florian
  1421. * some changes to compile the new code generator
  1422. Revision 1.79 1999/07/22 09:37:34 florian
  1423. + resourcestring implemented
  1424. + start of longstring support
  1425. Revision 1.78 1999/07/05 20:13:07 peter
  1426. * removed temp defines
  1427. Revision 1.77 1999/07/04 16:37:08 florian
  1428. + qword/int64 -> floating point type cast
  1429. Revision 1.76 1999/06/28 22:29:10 florian
  1430. * qword division fixed
  1431. + code for qword/int64 type casting added:
  1432. range checking isn't implemented yet
  1433. Revision 1.75 1999/05/31 20:35:46 peter
  1434. * ansistring fixes, decr_ansistr called after all temp ansi reuses
  1435. Revision 1.74 1999/05/27 19:44:09 peter
  1436. * removed oldasm
  1437. * plabel -> pasmlabel
  1438. * -a switches to source writing automaticly
  1439. * assembler readers OOPed
  1440. * asmsymbol automaticly external
  1441. * jumptables and other label fixes for asm readers
  1442. Revision 1.73 1999/05/18 21:58:26 florian
  1443. * fixed some bugs related to temp. ansistrings and functions results
  1444. which return records/objects/arrays which need init/final.
  1445. }