cg386cnv.pas 58 KB

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