cg386cnv.pas 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  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. { we've also to release the registers ... }
  567. del_reference(pfrom^.location.reference);
  568. { ... here only the temp. location is released }
  569. emit_push_lea_loc(pfrom^.location,true);
  570. emitpushreferenceaddr(pto^.location.reference);
  571. emitcall('FPC_CHARARRAY_TO_SHORTSTR');
  572. maybe_loadesi;
  573. popusedregisters(pushed);
  574. end;
  575. st_ansistring :
  576. begin
  577. gettempansistringreference(pto^.location.reference);
  578. decrstringref(cansistringdef,pto^.location.reference);
  579. release_loc(pfrom^.location);
  580. pushusedregisters(pushed,$ff);
  581. push_int(l);
  582. emitpushreferenceaddr(pfrom^.location.reference);
  583. emitpushreferenceaddr(pto^.location.reference);
  584. emitcall('FPC_CHARARRAY_TO_ANSISTR');
  585. popusedregisters(pushed);
  586. maybe_loadesi;
  587. end;
  588. st_longstring:
  589. begin
  590. {!!!!!!!}
  591. internalerror(8888);
  592. end;
  593. st_widestring:
  594. begin
  595. {!!!!!!!}
  596. internalerror(8888);
  597. end;
  598. end;
  599. end;
  600. procedure second_char_to_string(var pto,pfrom : ptree;convtyp : tconverttype);
  601. var
  602. pushed : tpushed;
  603. begin
  604. clear_location(pto^.location);
  605. pto^.location.loc:=LOC_MEM;
  606. case pstringdef(pto^.resulttype)^.string_typ of
  607. st_shortstring :
  608. begin
  609. gettempofsizereference(256,pto^.location.reference);
  610. { call loadstring with correct left and right }
  611. pto^.right:=pfrom;
  612. pto^.left:=pto;
  613. loadshortstring(pto);
  614. pto^.left:=nil; { reset left tree, which is empty }
  615. { pto^.right is not disposed for typeconv !! PM }
  616. disposetree(pto^.right);
  617. pto^.right:=nil;
  618. end;
  619. st_ansistring :
  620. begin
  621. gettempansistringreference(pto^.location.reference);
  622. decrstringref(cansistringdef,pto^.location.reference);
  623. release_loc(pfrom^.location);
  624. pushusedregisters(pushed,$ff);
  625. emit_pushw_loc(pfrom^.location);
  626. emitpushreferenceaddr(pto^.location.reference);
  627. emitcall('FPC_CHAR_TO_ANSISTR');
  628. popusedregisters(pushed);
  629. maybe_loadesi;
  630. end;
  631. else
  632. internalerror(4179);
  633. end;
  634. end;
  635. procedure second_int_to_real(var pto,pfrom : ptree;convtyp : tconverttype);
  636. var
  637. r : preference;
  638. hregister : tregister;
  639. l1,l2 : pasmlabel;
  640. begin
  641. { for u32bit a solution is to push $0 and to load a comp }
  642. { does this first, it destroys maybe EDI }
  643. hregister:=R_EDI;
  644. if porddef(pfrom^.resulttype)^.typ=u32bit then
  645. push_int(0);
  646. if (pfrom^.location.loc=LOC_REGISTER) or
  647. (pfrom^.location.loc=LOC_CREGISTER) then
  648. begin
  649. {$ifndef noAllocEdi}
  650. if not (porddef(pfrom^.resulttype)^.typ in [u32bit,s32bit,u64bit,s64bit]) then
  651. getexplicitregister32(R_EDI);
  652. {$endif noAllocEdi}
  653. case porddef(pfrom^.resulttype)^.typ of
  654. s8bit : emit_reg_reg(A_MOVSX,S_BL,pfrom^.location.register,R_EDI);
  655. u8bit : emit_reg_reg(A_MOVZX,S_BL,pfrom^.location.register,R_EDI);
  656. s16bit : emit_reg_reg(A_MOVSX,S_WL,pfrom^.location.register,R_EDI);
  657. u16bit : emit_reg_reg(A_MOVZX,S_WL,pfrom^.location.register,R_EDI);
  658. u32bit,s32bit:
  659. hregister:=pfrom^.location.register;
  660. u64bit,s64bit:
  661. begin
  662. emit_reg(A_PUSH,S_L,pfrom^.location.registerhigh);
  663. hregister:=pfrom^.location.registerlow;
  664. end;
  665. end;
  666. ungetregister(pfrom^.location.register);
  667. end
  668. else
  669. begin
  670. r:=newreference(pfrom^.location.reference);
  671. {$ifndef noAllocEdi}
  672. getexplicitregister32(R_EDI);
  673. {$endif noAllocEdi}
  674. case porddef(pfrom^.resulttype)^.typ of
  675. s8bit:
  676. emit_ref_reg(A_MOVSX,S_BL,r,R_EDI);
  677. u8bit:
  678. emit_ref_reg(A_MOVZX,S_BL,r,R_EDI);
  679. s16bit:
  680. emit_ref_reg(A_MOVSX,S_WL,r,R_EDI);
  681. u16bit:
  682. emit_ref_reg(A_MOVZX,S_WL,r,R_EDI);
  683. u32bit,s32bit:
  684. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  685. u64bit,s64bit:
  686. begin
  687. inc(r^.offset,4);
  688. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  689. emit_reg(A_PUSH,S_L,R_EDI);
  690. r:=newreference(pfrom^.location.reference);
  691. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  692. end;
  693. end;
  694. del_reference(pfrom^.location.reference);
  695. ungetiftemp(pfrom^.location.reference);
  696. end;
  697. { for 64 bit integers, the high dword is already pushed }
  698. emit_reg(A_PUSH,S_L,hregister);
  699. {$ifndef noAllocEdi}
  700. if hregister = R_EDI then
  701. ungetregister32(R_EDI);
  702. {$endif noAllocEdi}
  703. r:=new_reference(R_ESP,0);
  704. case porddef(pfrom^.resulttype)^.typ of
  705. u32bit:
  706. begin
  707. emit_ref(A_FILD,S_IQ,r);
  708. emit_const_reg(A_ADD,S_L,8,R_ESP);
  709. end;
  710. s64bit:
  711. begin
  712. emit_ref(A_FILD,S_IQ,r);
  713. emit_const_reg(A_ADD,S_L,8,R_ESP);
  714. end;
  715. u64bit:
  716. begin
  717. { unsigned 64 bit ints are harder to handle: }
  718. { we load bits 0..62 and then check bit 63: }
  719. { if it is 1 then we add $80000000 000000000 }
  720. { as double }
  721. inc(r^.offset,4);
  722. {$ifndef noAllocEdi}
  723. getexplicitregister32(R_EDI);
  724. {$endif noAllocEdi}
  725. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  726. r:=new_reference(R_ESP,4);
  727. emit_const_ref(A_AND,S_L,$7fffffff,r);
  728. emit_const_reg(A_TEST,S_L,$80000000,R_EDI);
  729. {$ifndef noAllocEdi}
  730. ungetregister32(R_EDI);
  731. {$endif noAllocEdi}
  732. r:=new_reference(R_ESP,0);
  733. emit_ref(A_FILD,S_IQ,r);
  734. getdatalabel(l1);
  735. getlabel(l2);
  736. emitjmp(C_Z,l2);
  737. consts^.concat(new(pai_label,init(l1)));
  738. { I got this constant from a test progtram (FK) }
  739. consts^.concat(new(pai_const,init_32bit(0)));
  740. consts^.concat(new(pai_const,init_32bit(1138753536)));
  741. r:=new_reference(R_NO,0);
  742. r^.symbol:=l1;
  743. emit_ref(A_FADD,S_FL,r);
  744. emitlab(l2);
  745. emit_const_reg(A_ADD,S_L,8,R_ESP);
  746. end
  747. else
  748. begin
  749. emit_ref(A_FILD,S_IL,r);
  750. {$ifndef noAllocEdi}
  751. getexplicitregister32(R_EDI);
  752. {$endif noAllocEdi}
  753. emit_reg(A_POP,S_L,R_EDI);
  754. {$ifndef noAllocEdi}
  755. ungetregister32(R_EDI);
  756. {$endif noAllocEdi}
  757. end;
  758. end;
  759. inc(fpuvaroffset);
  760. clear_location(pto^.location);
  761. pto^.location.loc:=LOC_FPU;
  762. end;
  763. procedure second_real_to_fix(var pto,pfrom : ptree;convtyp : tconverttype);
  764. var
  765. rreg : tregister;
  766. ref : treference;
  767. begin
  768. { real must be on fpu stack }
  769. if (pfrom^.location.loc<>LOC_FPU) then
  770. emit_ref(A_FLD,S_FL,newreference(pfrom^.location.reference));
  771. push_int($1f3f);
  772. push_int(65536);
  773. reset_reference(ref);
  774. ref.base:=R_ESP;
  775. emit_ref(A_FIMUL,S_IL,newreference(ref));
  776. ref.offset:=4;
  777. emit_ref(A_FSTCW,S_NO,newreference(ref));
  778. ref.offset:=6;
  779. emit_ref(A_FLDCW,S_NO,newreference(ref));
  780. ref.offset:=0;
  781. emit_ref(A_FISTP,S_IL,newreference(ref));
  782. ref.offset:=4;
  783. emit_ref(A_FLDCW,S_NO,newreference(ref));
  784. rreg:=getregister32;
  785. emit_reg(A_POP,S_L,rreg);
  786. { better than an add on all processors }
  787. {$ifndef noAllocEdi}
  788. getexplicitregister32(R_EDI);
  789. {$endif noAllocEdi}
  790. emit_reg(A_POP,S_L,R_EDI);
  791. {$ifndef noAllocEdi}
  792. ungetregister32(R_EDI);
  793. {$endif noAllocEdi}
  794. clear_location(pto^.location);
  795. pto^.location.loc:=LOC_REGISTER;
  796. pto^.location.register:=rreg;
  797. inc(fpuvaroffset);
  798. end;
  799. procedure second_real_to_real(var pto,pfrom : ptree;convtyp : tconverttype);
  800. begin
  801. case pfrom^.location.loc of
  802. LOC_FPU : ;
  803. LOC_CFPUREGISTER:
  804. begin
  805. pto^.location:=pfrom^.location;
  806. exit;
  807. end;
  808. LOC_MEM,
  809. LOC_REFERENCE:
  810. begin
  811. floatload(pfloatdef(pfrom^.resulttype)^.typ,
  812. pfrom^.location.reference);
  813. { we have to free the reference }
  814. del_reference(pfrom^.location.reference);
  815. end;
  816. end;
  817. clear_location(pto^.location);
  818. pto^.location.loc:=LOC_FPU;
  819. end;
  820. procedure second_fix_to_real(var pto,pfrom : ptree;convtyp : tconverttype);
  821. var
  822. popeax,popebx,popecx,popedx : boolean;
  823. startreg : tregister;
  824. hl : pasmlabel;
  825. r : treference;
  826. begin
  827. if (pfrom^.location.loc=LOC_REGISTER) or
  828. (pfrom^.location.loc=LOC_CREGISTER) then
  829. begin
  830. startreg:=pfrom^.location.register;
  831. ungetregister(startreg);
  832. popeax:=(startreg<>R_EAX) and not (R_EAX in unused);
  833. if popeax then
  834. emit_reg(A_PUSH,S_L,R_EAX);
  835. { mov eax,eax is removed by emit_reg_reg }
  836. emit_reg_reg(A_MOV,S_L,startreg,R_EAX);
  837. end
  838. else
  839. begin
  840. emit_ref_reg(A_MOV,S_L,newreference(
  841. pfrom^.location.reference),R_EAX);
  842. del_reference(pfrom^.location.reference);
  843. startreg:=R_NO;
  844. end;
  845. popebx:=(startreg<>R_EBX) and not (R_EBX in unused);
  846. if popebx then
  847. emit_reg(A_PUSH,S_L,R_EBX);
  848. popecx:=(startreg<>R_ECX) and not (R_ECX in unused);
  849. if popecx then
  850. emit_reg(A_PUSH,S_L,R_ECX);
  851. popedx:=(startreg<>R_EDX) and not (R_EDX in unused);
  852. if popedx then
  853. emit_reg(A_PUSH,S_L,R_EDX);
  854. emit_none(A_CDQ,S_NO);
  855. emit_reg_reg(A_XOR,S_L,R_EDX,R_EAX);
  856. emit_reg_reg(A_MOV,S_L,R_EAX,R_EBX);
  857. emit_reg_reg(A_SUB,S_L,R_EDX,R_EAX);
  858. getlabel(hl);
  859. emitjmp(C_Z,hl);
  860. emit_const_reg(A_RCL,S_L,1,R_EBX);
  861. emit_reg_reg(A_BSR,S_L,R_EAX,R_EDX);
  862. emit_const_reg(A_MOV,S_B,32,R_CL);
  863. emit_reg_reg(A_SUB,S_B,R_DL,R_CL);
  864. emit_reg_reg(A_SHL,S_L,R_CL,R_EAX);
  865. emit_const_reg(A_ADD,S_W,1007,R_DX);
  866. emit_const_reg(A_SHL,S_W,5,R_DX);
  867. emit_const_reg_reg(A_SHLD,S_W,11,R_DX,R_BX);
  868. emit_const_reg_reg(A_SHLD,S_L,20,R_EAX,R_EBX);
  869. emit_const_reg(A_SHL,S_L,20,R_EAX);
  870. emitlab(hl);
  871. { better than an add on all processors }
  872. emit_reg(A_PUSH,S_L,R_EBX);
  873. emit_reg(A_PUSH,S_L,R_EAX);
  874. reset_reference(r);
  875. r.base:=R_ESP;
  876. emit_ref(A_FLD,S_FL,newreference(r));
  877. emit_const_reg(A_ADD,S_L,8,R_ESP);
  878. if popedx then
  879. emit_reg(A_POP,S_L,R_EDX);
  880. if popecx then
  881. emit_reg(A_POP,S_L,R_ECX);
  882. if popebx then
  883. emit_reg(A_POP,S_L,R_EBX);
  884. if popeax then
  885. emit_reg(A_POP,S_L,R_EAX);
  886. clear_location(pto^.location);
  887. pto^.location.loc:=LOC_FPU;
  888. end;
  889. procedure second_cord_to_pointer(var pto,pfrom : ptree;convtyp : tconverttype);
  890. begin
  891. { this can't happend, because constants are already processed in
  892. pass 1 }
  893. internalerror(47423985);
  894. end;
  895. procedure second_int_to_fix(var pto,pfrom : ptree;convtyp : tconverttype);
  896. var
  897. hregister : tregister;
  898. begin
  899. if (pfrom^.location.loc=LOC_REGISTER) then
  900. hregister:=pfrom^.location.register
  901. else if (pfrom^.location.loc=LOC_CREGISTER) then
  902. hregister:=getregister32
  903. else
  904. begin
  905. del_reference(pfrom^.location.reference);
  906. hregister:=getregister32;
  907. case porddef(pfrom^.resulttype)^.typ of
  908. s8bit : emit_ref_reg(A_MOVSX,S_BL,newreference(pfrom^.location.reference),
  909. hregister);
  910. u8bit : emit_ref_reg(A_MOVZX,S_BL,newreference(pfrom^.location.reference),
  911. hregister);
  912. s16bit : emit_ref_reg(A_MOVSX,S_WL,newreference(pfrom^.location.reference),
  913. hregister);
  914. u16bit : emit_ref_reg(A_MOVZX,S_WL,newreference(pfrom^.location.reference),
  915. hregister);
  916. u32bit,s32bit : emit_ref_reg(A_MOV,S_L,newreference(pfrom^.location.reference),
  917. hregister);
  918. {!!!! u32bit }
  919. end;
  920. end;
  921. emit_const_reg(A_SHL,S_L,16,hregister);
  922. clear_location(pto^.location);
  923. pto^.location.loc:=LOC_REGISTER;
  924. pto^.location.register:=hregister;
  925. end;
  926. procedure second_proc_to_procvar(var pto,pfrom : ptree;convtyp : tconverttype);
  927. begin
  928. { method pointer ? }
  929. if assigned(pfrom^.left) then
  930. begin
  931. set_location(pto^.location,pfrom^.location);
  932. end
  933. else
  934. begin
  935. clear_location(pto^.location);
  936. pto^.location.loc:=LOC_REGISTER;
  937. pto^.location.register:=getregister32;
  938. del_reference(pfrom^.location.reference);
  939. emit_ref_reg(A_LEA,S_L,
  940. newreference(pfrom^.location.reference),pto^.location.register);
  941. end;
  942. end;
  943. procedure second_bool_to_int(var pto,pfrom : ptree;convtyp : tconverttype);
  944. var
  945. oldtruelabel,oldfalselabel,hlabel : pasmlabel;
  946. hregister : tregister;
  947. newsize,
  948. opsize : topsize;
  949. op : tasmop;
  950. begin
  951. oldtruelabel:=truelabel;
  952. oldfalselabel:=falselabel;
  953. getlabel(truelabel);
  954. getlabel(falselabel);
  955. secondpass(pfrom);
  956. { byte(boolean) or word(wordbool) or longint(longbool) must
  957. be accepted for var parameters }
  958. if (pto^.explizit) and
  959. (pfrom^.resulttype^.size=pto^.resulttype^.size) and
  960. (pfrom^.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  961. begin
  962. set_location(pto^.location,pfrom^.location);
  963. truelabel:=oldtruelabel;
  964. falselabel:=oldfalselabel;
  965. exit;
  966. end;
  967. clear_location(pto^.location);
  968. pto^.location.loc:=LOC_REGISTER;
  969. del_reference(pfrom^.location.reference);
  970. case pfrom^.resulttype^.size of
  971. 1 : begin
  972. case pto^.resulttype^.size of
  973. 1 : opsize:=S_B;
  974. 2 : opsize:=S_BW;
  975. 4 : opsize:=S_BL;
  976. end;
  977. end;
  978. 2 : begin
  979. case pto^.resulttype^.size of
  980. 1 : begin
  981. if pfrom^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  982. pfrom^.location.register:=reg16toreg8(pfrom^.location.register);
  983. opsize:=S_B;
  984. end;
  985. 2 : opsize:=S_W;
  986. 4 : opsize:=S_WL;
  987. end;
  988. end;
  989. 4 : begin
  990. case pto^.resulttype^.size of
  991. 1 : begin
  992. if pfrom^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  993. pfrom^.location.register:=reg32toreg8(pfrom^.location.register);
  994. opsize:=S_B;
  995. end;
  996. 2 : begin
  997. if pfrom^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  998. pfrom^.location.register:=reg32toreg16(pfrom^.location.register);
  999. opsize:=S_W;
  1000. end;
  1001. 4 : opsize:=S_L;
  1002. end;
  1003. end;
  1004. end;
  1005. if opsize in [S_B,S_W,S_L] then
  1006. op:=A_MOV
  1007. else
  1008. if is_signed(pto^.resulttype) then
  1009. op:=A_MOVSX
  1010. else
  1011. op:=A_MOVZX;
  1012. hregister:=getregister32;
  1013. case pto^.resulttype^.size of
  1014. 1 : begin
  1015. pto^.location.register:=reg32toreg8(hregister);
  1016. newsize:=S_B;
  1017. end;
  1018. 2 : begin
  1019. pto^.location.register:=reg32toreg16(hregister);
  1020. newsize:=S_W;
  1021. end;
  1022. 4 : begin
  1023. pto^.location.register:=hregister;
  1024. newsize:=S_L;
  1025. end;
  1026. else
  1027. internalerror(10060);
  1028. end;
  1029. case pfrom^.location.loc of
  1030. LOC_MEM,
  1031. LOC_REFERENCE : emit_ref_reg(op,opsize,
  1032. newreference(pfrom^.location.reference),pto^.location.register);
  1033. LOC_REGISTER,
  1034. LOC_CREGISTER : begin
  1035. { remove things like movb %al,%al }
  1036. if pfrom^.location.register<>pto^.location.register then
  1037. emit_reg_reg(op,opsize,
  1038. pfrom^.location.register,pto^.location.register);
  1039. end;
  1040. LOC_FLAGS : begin
  1041. emit_flag2reg(pfrom^.location.resflags,pto^.location.register);
  1042. end;
  1043. LOC_JUMP : begin
  1044. getlabel(hlabel);
  1045. emitlab(truelabel);
  1046. emit_const_reg(A_MOV,newsize,1,pto^.location.register);
  1047. emitjmp(C_None,hlabel);
  1048. emitlab(falselabel);
  1049. emit_reg_reg(A_XOR,newsize,pto^.location.register,
  1050. pto^.location.register);
  1051. emitlab(hlabel);
  1052. end;
  1053. else
  1054. internalerror(10061);
  1055. end;
  1056. truelabel:=oldtruelabel;
  1057. falselabel:=oldfalselabel;
  1058. end;
  1059. procedure second_int_to_bool(var pto,pfrom : ptree;convtyp : tconverttype);
  1060. var
  1061. hregister : tregister;
  1062. flags : tresflags;
  1063. opsize : topsize;
  1064. begin
  1065. clear_location(pto^.location);
  1066. { byte(boolean) or word(wordbool) or longint(longbool) must
  1067. be accepted for var parameters }
  1068. if (pto^.explizit) and
  1069. (pfrom^.resulttype^.size=pto^.resulttype^.size) and
  1070. (pfrom^.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  1071. begin
  1072. set_location(pto^.location,pfrom^.location);
  1073. exit;
  1074. end;
  1075. pto^.location.loc:=LOC_REGISTER;
  1076. del_reference(pfrom^.location.reference);
  1077. opsize:=def_opsize(pfrom^.resulttype);
  1078. case pfrom^.location.loc of
  1079. LOC_MEM,LOC_REFERENCE :
  1080. begin
  1081. hregister:=def_getreg(pfrom^.resulttype);
  1082. emit_ref_reg(A_MOV,opsize,
  1083. newreference(pfrom^.location.reference),hregister);
  1084. emit_reg_reg(A_OR,opsize,hregister,hregister);
  1085. flags:=F_NE;
  1086. end;
  1087. LOC_FLAGS :
  1088. begin
  1089. hregister:=getregister32;
  1090. flags:=pfrom^.location.resflags;
  1091. end;
  1092. LOC_REGISTER,LOC_CREGISTER :
  1093. begin
  1094. hregister:=pfrom^.location.register;
  1095. emit_reg_reg(A_OR,opsize,hregister,hregister);
  1096. flags:=F_NE;
  1097. end;
  1098. else
  1099. internalerror(10062);
  1100. end;
  1101. case pto^.resulttype^.size of
  1102. 1 : pto^.location.register:=makereg8(hregister);
  1103. 2 : pto^.location.register:=makereg16(hregister);
  1104. 4 : pto^.location.register:=makereg32(hregister);
  1105. else
  1106. internalerror(10064);
  1107. end;
  1108. emit_flag2reg(flags,pto^.location.register);
  1109. end;
  1110. procedure second_load_smallset(var pto,pfrom : ptree;convtyp : tconverttype);
  1111. var
  1112. href : treference;
  1113. pushedregs : tpushed;
  1114. begin
  1115. href.symbol:=nil;
  1116. pushusedregisters(pushedregs,$ff);
  1117. gettempofsizereference(32,href);
  1118. emitpushreferenceaddr(pfrom^.location.reference);
  1119. emitpushreferenceaddr(href);
  1120. emitcall('FPC_SET_LOAD_SMALL');
  1121. maybe_loadesi;
  1122. popusedregisters(pushedregs);
  1123. clear_location(pto^.location);
  1124. pto^.location.loc:=LOC_MEM;
  1125. pto^.location.reference:=href;
  1126. end;
  1127. procedure second_ansistring_to_pchar(var pto,pfrom : ptree;convtyp : tconverttype);
  1128. var
  1129. l1 : pasmlabel;
  1130. hr : preference;
  1131. begin
  1132. clear_location(pto^.location);
  1133. pto^.location.loc:=LOC_REGISTER;
  1134. getlabel(l1);
  1135. case pfrom^.location.loc of
  1136. LOC_CREGISTER,LOC_REGISTER:
  1137. pto^.location.register:=pfrom^.location.register;
  1138. LOC_MEM,LOC_REFERENCE:
  1139. begin
  1140. pto^.location.register:=getregister32;
  1141. emit_ref_reg(A_MOV,S_L,newreference(pfrom^.location.reference),
  1142. pto^.location.register);
  1143. del_reference(pfrom^.location.reference);
  1144. end;
  1145. end;
  1146. emit_const_reg(A_CMP,S_L,0,pto^.location.register);
  1147. emitjmp(C_NZ,l1);
  1148. new(hr);
  1149. reset_reference(hr^);
  1150. hr^.symbol:=newasmsymbol('FPC_EMPTYCHAR');
  1151. emit_ref_reg(A_LEA,S_L,hr,pto^.location.register);
  1152. emitlab(l1);
  1153. end;
  1154. procedure second_pchar_to_string(var pto,pfrom : ptree;convtyp : tconverttype);
  1155. var
  1156. pushed : tpushed;
  1157. begin
  1158. case pstringdef(pto^.resulttype)^.string_typ of
  1159. st_shortstring:
  1160. begin
  1161. pto^.location.loc:=LOC_REFERENCE;
  1162. gettempofsizereference(pto^.resulttype^.size,pto^.location.reference);
  1163. pushusedregisters(pushed,$ff);
  1164. case pfrom^.location.loc of
  1165. LOC_REGISTER,LOC_CREGISTER:
  1166. begin
  1167. emit_reg(A_PUSH,S_L,pfrom^.location.register);
  1168. ungetregister32(pfrom^.location.register);
  1169. end;
  1170. LOC_REFERENCE,LOC_MEM:
  1171. begin
  1172. emit_push_mem(pfrom^.location.reference);
  1173. del_reference(pfrom^.location.reference);
  1174. end;
  1175. end;
  1176. emitpushreferenceaddr(pto^.location.reference);
  1177. emitcall('FPC_PCHAR_TO_SHORTSTR');
  1178. maybe_loadesi;
  1179. popusedregisters(pushed);
  1180. end;
  1181. st_ansistring:
  1182. begin
  1183. pto^.location.loc:=LOC_REFERENCE;
  1184. gettempansistringreference(pto^.location.reference);
  1185. decrstringref(cansistringdef,pto^.location.reference);
  1186. case pfrom^.location.loc of
  1187. LOC_REFERENCE,LOC_MEM:
  1188. begin
  1189. {$IfNDef regallocfix}
  1190. del_reference(pfrom^.location.reference);
  1191. pushusedregisters(pushed,$ff);
  1192. emit_push_mem(pfrom^.location.reference);
  1193. {$Else regallocfix}
  1194. pushusedregisters(pushed,$ff
  1195. xor ($80 shr byte(pfrom^.location.reference.base))
  1196. xor ($80 shr byte(pfrom^.location.reference.index)));
  1197. emit_push_mem(pfrom^.location.reference);
  1198. del_reference(pfrom^.location.reference);
  1199. {$EndIf regallocfix}
  1200. end;
  1201. LOC_REGISTER,LOC_CREGISTER:
  1202. begin
  1203. {$IfNDef regallocfix}
  1204. ungetregister32(pfrom^.location.register);
  1205. pushusedregisters(pushed,$ff);
  1206. emit_reg(A_PUSH,S_L,pfrom^.location.register);
  1207. {$Else regallocfix}
  1208. pushusedregisters(pushed, $ff xor ($80 shr byte(pfrom^.location.register)));
  1209. emit_reg(A_PUSH,S_L,pfrom^.location.register);
  1210. ungetregister32(pfrom^.location.register);
  1211. {$EndIf regallocfix}
  1212. end;
  1213. end;
  1214. emitpushreferenceaddr(pto^.location.reference);
  1215. emitcall('FPC_PCHAR_TO_ANSISTR');
  1216. maybe_loadesi;
  1217. popusedregisters(pushed);
  1218. end;
  1219. else
  1220. begin
  1221. internalerror(12121);
  1222. end;
  1223. end;
  1224. end;
  1225. procedure second_nothing(var pto,pfrom : ptree;convtyp : tconverttype);
  1226. begin
  1227. end;
  1228. {****************************************************************************
  1229. SecondTypeConv
  1230. ****************************************************************************}
  1231. procedure secondtypeconv(var p : ptree);
  1232. const
  1233. secondconvert : array[tconverttype] of tsecondconvproc = (
  1234. second_nothing, {equal}
  1235. second_nothing, {not_possible}
  1236. second_string_to_string,
  1237. second_char_to_string,
  1238. second_pchar_to_string,
  1239. second_nothing, {cchar_to_pchar}
  1240. second_cstring_to_pchar,
  1241. second_ansistring_to_pchar,
  1242. second_string_to_chararray,
  1243. second_chararray_to_string,
  1244. second_array_to_pointer,
  1245. second_pointer_to_array,
  1246. second_int_to_int,
  1247. second_int_to_bool,
  1248. second_bool_to_int, { bool_to_bool }
  1249. second_bool_to_int,
  1250. second_real_to_real,
  1251. second_int_to_real,
  1252. second_int_to_fix,
  1253. second_real_to_fix,
  1254. second_fix_to_real,
  1255. second_proc_to_procvar,
  1256. second_nothing, {arrayconstructor_to_set}
  1257. second_load_smallset,
  1258. second_cord_to_pointer
  1259. );
  1260. {$ifdef TESTOBJEXT2}
  1261. var
  1262. r : preference;
  1263. nillabel : plabel;
  1264. {$endif TESTOBJEXT2}
  1265. begin
  1266. { this isn't good coding, I think tc_bool_2_int, shouldn't be }
  1267. { type conversion (FK) }
  1268. if not(p^.convtyp in [tc_bool_2_int,tc_bool_2_bool]) then
  1269. begin
  1270. secondpass(p^.left);
  1271. set_location(p^.location,p^.left^.location);
  1272. if codegenerror then
  1273. exit;
  1274. end;
  1275. { the second argument only is for maybe_range_checking !}
  1276. secondconvert[p^.convtyp](p,p^.left,p^.convtyp);
  1277. {$ifdef TESTOBJEXT2}
  1278. { Check explicit conversions to objects pointers !! }
  1279. if p^.explizit and
  1280. (p^.resulttype^.deftype=pointerdef) and
  1281. (ppointerdef(p^.resulttype)^.definition^.deftype=objectdef) and not
  1282. (pobjectdef(ppointerdef(p^.resulttype)^.definition)^.isclass) and
  1283. ((pobjectdef(ppointerdef(p^.resulttype)^.definition)^.options and oo_hasvmt)<>0) and
  1284. (cs_check_range in aktlocalswitches) then
  1285. begin
  1286. new(r);
  1287. reset_reference(r^);
  1288. if p^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  1289. r^.base:=p^.location.register
  1290. else
  1291. begin
  1292. {$ifndef noAllocEdi}
  1293. getexplicitregister32(R_EDI);
  1294. {$endif noAllocEdi}
  1295. emit_mov_loc_reg(p^.location,R_EDI);
  1296. r^.base:=R_EDI;
  1297. end;
  1298. { NIL must be accepted !! }
  1299. emit_reg_reg(A_OR,S_L,r^.base,r^.base);
  1300. {$ifndef noAllocEdi}
  1301. ungetregister32(R_EDI);
  1302. {$endif noAllocEdi}
  1303. getlabel(nillabel);
  1304. emitjmp(C_E,nillabel);
  1305. { this is one point where we need vmt_offset (PM) }
  1306. r^.offset:= pobjectdef(ppointerdef(p^.resulttype)^.definition)^.vmt_offset;
  1307. {$ifndef noAllocEdi}
  1308. getexplicitregister32(R_EDI);
  1309. {$endif noAllocEdi}
  1310. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  1311. emit_sym(A_PUSH,S_L,
  1312. newasmsymbol(pobjectdef(ppointerdef(p^.resulttype)^.definition)^.vmt_mangledname));
  1313. emit_reg(A_PUSH,S_L,R_EDI);
  1314. {$ifndef noAllocEdi}
  1315. ungetregister32(R_EDI);
  1316. {$endif noAllocEdi}
  1317. emitcall('FPC_CHECK_OBJECT_EXT');
  1318. emitlab(nillabel);
  1319. end;
  1320. {$endif TESTOBJEXT2}
  1321. end;
  1322. {*****************************************************************************
  1323. SecondIs
  1324. *****************************************************************************}
  1325. procedure secondis(var p : ptree);
  1326. var
  1327. pushed : tpushed;
  1328. begin
  1329. { save all used registers }
  1330. pushusedregisters(pushed,$ff);
  1331. secondpass(p^.left);
  1332. clear_location(p^.location);
  1333. p^.location.loc:=LOC_FLAGS;
  1334. p^.location.resflags:=F_NE;
  1335. { push instance to check: }
  1336. case p^.left^.location.loc of
  1337. LOC_REGISTER,LOC_CREGISTER:
  1338. begin
  1339. emit_reg(A_PUSH,
  1340. S_L,p^.left^.location.register);
  1341. ungetregister32(p^.left^.location.register);
  1342. end;
  1343. LOC_MEM,LOC_REFERENCE:
  1344. begin
  1345. emit_ref(A_PUSH,
  1346. S_L,newreference(p^.left^.location.reference));
  1347. del_reference(p^.left^.location.reference);
  1348. end;
  1349. else internalerror(100);
  1350. end;
  1351. { generate type checking }
  1352. secondpass(p^.right);
  1353. case p^.right^.location.loc of
  1354. LOC_REGISTER,LOC_CREGISTER:
  1355. begin
  1356. emit_reg(A_PUSH,
  1357. S_L,p^.right^.location.register);
  1358. ungetregister32(p^.right^.location.register);
  1359. end;
  1360. LOC_MEM,LOC_REFERENCE:
  1361. begin
  1362. emit_ref(A_PUSH,
  1363. S_L,newreference(p^.right^.location.reference));
  1364. del_reference(p^.right^.location.reference);
  1365. end;
  1366. else internalerror(100);
  1367. end;
  1368. emitcall('FPC_DO_IS');
  1369. emit_reg_reg(A_OR,S_B,R_AL,R_AL);
  1370. popusedregisters(pushed);
  1371. maybe_loadesi;
  1372. end;
  1373. {*****************************************************************************
  1374. SecondAs
  1375. *****************************************************************************}
  1376. procedure secondas(var p : ptree);
  1377. var
  1378. pushed : tpushed;
  1379. begin
  1380. secondpass(p^.left);
  1381. { save all used registers }
  1382. pushusedregisters(pushed,$ff);
  1383. { push instance to check: }
  1384. case p^.left^.location.loc of
  1385. LOC_REGISTER,LOC_CREGISTER:
  1386. emit_reg(A_PUSH,
  1387. S_L,p^.left^.location.register);
  1388. LOC_MEM,LOC_REFERENCE:
  1389. emit_ref(A_PUSH,
  1390. S_L,newreference(p^.left^.location.reference));
  1391. else internalerror(100);
  1392. end;
  1393. { we doesn't modifiy the left side, we check only the type }
  1394. set_location(p^.location,p^.left^.location);
  1395. { generate type checking }
  1396. secondpass(p^.right);
  1397. case p^.right^.location.loc of
  1398. LOC_REGISTER,LOC_CREGISTER:
  1399. begin
  1400. emit_reg(A_PUSH,
  1401. S_L,p^.right^.location.register);
  1402. ungetregister32(p^.right^.location.register);
  1403. end;
  1404. LOC_MEM,LOC_REFERENCE:
  1405. begin
  1406. emit_ref(A_PUSH,
  1407. S_L,newreference(p^.right^.location.reference));
  1408. del_reference(p^.right^.location.reference);
  1409. end;
  1410. else internalerror(100);
  1411. end;
  1412. emitcall('FPC_DO_AS');
  1413. { restore register, this restores automatically the }
  1414. { result }
  1415. popusedregisters(pushed);
  1416. maybe_loadesi;
  1417. end;
  1418. end.
  1419. {
  1420. $Log$
  1421. Revision 1.103 2000-02-19 10:12:47 florian
  1422. * fixed one more internalerror 10
  1423. Revision 1.102 2000/02/09 13:22:46 peter
  1424. * log truncated
  1425. Revision 1.101 2000/01/13 16:52:48 jonas
  1426. * moved deallocation of registers used in reference that points to string after
  1427. copyshortstring (this routine doesn't require extra regs)
  1428. Revision 1.100 2000/01/09 12:35:00 jonas
  1429. * changed edi allocation to use getexplicitregister32/ungetregister
  1430. (adapted tgeni386 a bit for this) and enabled it by default
  1431. * fixed very big and stupid bug of mine in cg386mat that broke the
  1432. include() code (and make cycle :( ) if you compiled without
  1433. -dnewoptimizations
  1434. Revision 1.99 2000/01/09 01:44:19 jonas
  1435. + (de)allocation info for EDI to fix reported bug on mailinglist.
  1436. Also some (de)allocation info for ESI added. Between -dallocEDI
  1437. because at this time of the night bugs could easily slip in ;)
  1438. Revision 1.98 2000/01/07 01:14:20 peter
  1439. * updated copyright to 2000
  1440. Revision 1.97 1999/12/22 01:01:46 peter
  1441. - removed freelabel()
  1442. * added undefined label detection in internal assembler, this prevents
  1443. a lot of ld crashes and wrong .o files
  1444. * .o files aren't written anymore if errors have occured
  1445. * inlining of assembler labels is now correct
  1446. Revision 1.96 1999/12/21 11:49:51 pierre
  1447. * array of char to short string bug fixed
  1448. Revision 1.95 1999/12/01 12:42:31 peter
  1449. * fixed bug 698
  1450. * removed some notes about unused vars
  1451. Revision 1.94 1999/11/29 22:15:25 pierre
  1452. * fix for internalerror(12) on ord(booleanarray[1])
  1453. Revision 1.93 1999/11/06 14:34:17 peter
  1454. * truncated log to 20 revs
  1455. Revision 1.92 1999/10/25 10:32:43 peter
  1456. * ansistring 2 chararray support
  1457. * optimized ansitring 2 pchar
  1458. Revision 1.91 1999/10/22 14:36:04 peter
  1459. * fixed esi reload with as
  1460. Revision 1.90 1999/10/06 08:32:00 peter
  1461. * fixed empty const ansistring 2 pchar
  1462. Revision 1.89 1999/09/26 21:30:15 peter
  1463. + constant pointer support which can happend with typecasting like
  1464. const p=pointer(1)
  1465. * better procvar parsing in typed consts
  1466. Revision 1.88 1999/09/26 13:26:04 florian
  1467. * exception patch of Romio nevertheless the excpetion handling
  1468. needs some corections regarding register saving
  1469. * gettempansistring is again a procedure
  1470. Revision 1.87 1999/09/23 21:20:37 peter
  1471. * fixed temp allocation for short->ansi
  1472. Revision 1.86 1999/09/01 09:42:13 peter
  1473. * update for new push_lea_loc
  1474. Revision 1.85 1999/08/19 13:08:46 pierre
  1475. * emit_??? used
  1476. Revision 1.84 1999/08/05 14:58:03 florian
  1477. * some fixes for the floating point registers
  1478. * more things for the new code generator
  1479. Revision 1.83 1999/08/04 13:45:19 florian
  1480. + floating point register variables !!
  1481. * pairegalloc is now generated for register variables
  1482. Revision 1.82 1999/08/04 00:22:43 florian
  1483. * renamed i386asm and i386base to cpuasm and cpubase
  1484. }