cg386cnv.pas 56 KB

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