cg386cnv.pas 57 KB

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