cg386cnv.pas 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  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 loadansi2short(source,dest : ptree);
  27. procedure secondtypeconv(var p : ptree);
  28. procedure secondas(var p : ptree);
  29. procedure secondis(var p : ptree);
  30. implementation
  31. uses
  32. cobjects,verbose,globals,systems,
  33. symtable,aasm,types,
  34. hcodegen,temp_gen,pass_2,pass_1,
  35. {$ifdef ag386bin}
  36. i386base,i386asm,
  37. {$else}
  38. i386,
  39. {$endif}
  40. cgai386,tgeni386;
  41. procedure push_shortstring_length(p:ptree);
  42. var
  43. hightree : ptree;
  44. begin
  45. if is_open_string(p^.resulttype) then
  46. begin
  47. getsymonlyin(p^.symtable,'high'+pvarsym(p^.symtableentry)^.name);
  48. hightree:=genloadnode(pvarsym(srsym),p^.symtable);
  49. firstpass(hightree);
  50. secondpass(hightree);
  51. push_value_para(hightree,false,0);
  52. disposetree(hightree);
  53. end
  54. else
  55. begin
  56. push_int(pstringdef(p^.resulttype)^.len);
  57. end;
  58. end;
  59. procedure loadshortstring(p:ptree);
  60. {
  61. Load a string, handles stringdef and orddef (char) types
  62. }
  63. begin
  64. case p^.right^.resulttype^.deftype of
  65. stringdef:
  66. begin
  67. if (p^.right^.treetype=stringconstn) and
  68. (str_length(p^.right)=0) then
  69. exprasmlist^.concat(new(pai386,op_const_ref(
  70. A_MOV,S_B,0,newreference(p^.left^.location.reference))))
  71. else
  72. begin
  73. emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
  74. emitpushreferenceaddr(exprasmlist,p^.right^.location.reference);
  75. push_shortstring_length(p^.left);
  76. emitcall('FPC_SHORTSTR_COPY',true);
  77. maybe_loadesi;
  78. end;
  79. end;
  80. orddef:
  81. begin
  82. if p^.right^.treetype=ordconstn then
  83. exprasmlist^.concat(new(pai386,op_const_ref(
  84. A_MOV,S_W,p^.right^.value*256+1,newreference(p^.left^.location.reference))))
  85. else
  86. begin
  87. { not so elegant (goes better with extra register }
  88. if (p^.right^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  89. begin
  90. exprasmlist^.concat(new(pai386,op_reg_reg(
  91. A_MOV,S_L,makereg32(p^.right^.location.register),R_EDI)));
  92. ungetregister(p^.right^.location.register);
  93. end
  94. else
  95. begin
  96. exprasmlist^.concat(new(pai386,op_ref_reg(
  97. A_MOV,S_L,newreference(p^.right^.location.reference),R_EDI)));
  98. del_reference(p^.right^.location.reference);
  99. end;
  100. exprasmlist^.concat(new(pai386,op_const_reg(A_SHL,S_L,8,R_EDI)));
  101. exprasmlist^.concat(new(pai386,op_const_reg(A_OR,S_L,1,R_EDI)));
  102. exprasmlist^.concat(new(pai386,op_reg_ref(
  103. A_MOV,S_W,R_DI,newreference(p^.left^.location.reference))));
  104. end;
  105. end;
  106. else
  107. CGMessage(type_e_mismatch);
  108. end;
  109. end;
  110. procedure loadansi2short(source,dest : ptree);
  111. var
  112. pushed : tpushed;
  113. begin
  114. del_reference(dest^.location.reference);
  115. case source^.location.loc of
  116. LOC_REFERENCE,LOC_MEM:
  117. begin
  118. ungetiftemp(source^.location.reference);
  119. del_reference(source^.location.reference);
  120. pushusedregisters(pushed,$ff);
  121. emit_push_mem(source^.location.reference);
  122. end;
  123. LOC_REGISTER,LOC_CREGISTER:
  124. begin
  125. ungetregister32(source^.location.register);
  126. pushusedregisters(pushed,$ff);
  127. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,source^.location.register)));
  128. end;
  129. end;
  130. push_shortstring_length(dest);
  131. emitpushreferenceaddr(exprasmlist,dest^.location.reference);
  132. emitcall('FPC_ANSISTR_TO_SHORTSTR',true);
  133. popusedregisters(pushed);
  134. maybe_loadesi;
  135. end;
  136. {*****************************************************************************
  137. SecondTypeConv
  138. *****************************************************************************}
  139. type
  140. tsecondconvproc = procedure(pto,pfrom : ptree;convtyp : tconverttype);
  141. procedure second_int_to_int(pto,pfrom : ptree;convtyp : tconverttype);
  142. var
  143. op : tasmop;
  144. opsize : topsize;
  145. hregister : tregister;
  146. begin
  147. { insert range check if not explicit conversion }
  148. if not(pto^.explizit) then
  149. emitrangecheck(pfrom,pto^.resulttype);
  150. { is the result size smaller ? }
  151. if pto^.resulttype^.size<pfrom^.resulttype^.size then
  152. begin
  153. { only need to set the new size of a register }
  154. if (pfrom^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  155. begin
  156. case pto^.resulttype^.size of
  157. 1 : pto^.location.register:=makereg8(pfrom^.location.register);
  158. 2 : pto^.location.register:=makereg16(pfrom^.location.register);
  159. 4 : pto^.location.register:=makereg32(pfrom^.location.register);
  160. end;
  161. end;
  162. end
  163. { is the result size bigger ? }
  164. else if pto^.resulttype^.size>pfrom^.resulttype^.size then
  165. begin
  166. { remove reference }
  167. if not(pfrom^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  168. begin
  169. del_reference(pfrom^.location.reference);
  170. { we can do this here as we need no temp inside }
  171. ungetiftemp(pfrom^.location.reference);
  172. end;
  173. { get op and opsize, handle separate for constants, becuase
  174. movz doesn't support constant values }
  175. if (pfrom^.location.loc=LOC_MEM) and (pfrom^.location.reference.is_immediate) then
  176. begin
  177. opsize:=def_opsize(pto^.resulttype);
  178. op:=A_MOV;
  179. end
  180. else
  181. begin
  182. opsize:=def2def_opsize(pfrom^.resulttype,pto^.resulttype);
  183. if opsize in [S_B,S_W,S_L] then
  184. op:=A_MOV
  185. else
  186. if is_signed(pfrom^.resulttype) then
  187. op:=A_MOVSX
  188. else
  189. op:=A_MOVZX;
  190. end;
  191. { load the register we need }
  192. if pfrom^.location.loc<>LOC_REGISTER then
  193. hregister:=getregister32
  194. else
  195. hregister:=pfrom^.location.register;
  196. { set the correct register size and location }
  197. clear_location(pto^.location);
  198. pto^.location.loc:=LOC_REGISTER;
  199. case pto^.resulttype^.size of
  200. 1 : pto^.location.register:=makereg8(hregister);
  201. 2 : pto^.location.register:=makereg16(hregister);
  202. 4 : pto^.location.register:=makereg32(hregister);
  203. end;
  204. { insert the assembler code }
  205. if pfrom^.location.loc in [LOC_CREGISTER,LOC_REGISTER] then
  206. emit_reg_reg(op,opsize,pfrom^.location.register,pto^.location.register)
  207. else
  208. exprasmlist^.concat(new(pai386,op_ref_reg(op,opsize,
  209. newreference(pfrom^.location.reference),pto^.location.register)));
  210. end;
  211. end;
  212. var
  213. ltemptoremove : plinkedlist;
  214. procedure second_string_to_string(pto,pfrom : ptree;convtyp : tconverttype);
  215. var
  216. pushed : tpushed;
  217. begin
  218. { does anybody know a better solution than this big case statement ? }
  219. { ok, a proc table would do the job }
  220. case pstringdef(pto^.resulttype)^.string_typ of
  221. st_shortstring:
  222. case pstringdef(pfrom^.resulttype)^.string_typ of
  223. st_shortstring:
  224. begin
  225. gettempofsizereference(pto^.resulttype^.size,pto^.location.reference);
  226. del_reference(pfrom^.location.reference);
  227. copyshortstring(pto^.location.reference,pfrom^.location.reference,
  228. pstringdef(pto^.resulttype)^.len,false);
  229. ungetiftemp(pfrom^.location.reference);
  230. end;
  231. st_longstring:
  232. begin
  233. {!!!!!!!}
  234. internalerror(8888);
  235. end;
  236. st_ansistring:
  237. begin
  238. gettempofsizereference(pto^.resulttype^.size,pto^.location.reference);
  239. loadansi2short(pfrom,pto);
  240. { this is done in secondtypeconv (FK)
  241. removetemps(exprasmlist,temptoremove);
  242. destroys:=true;
  243. }
  244. end;
  245. st_widestring:
  246. begin
  247. {!!!!!!!}
  248. internalerror(8888);
  249. end;
  250. end;
  251. st_longstring:
  252. case pstringdef(pfrom^.resulttype)^.string_typ of
  253. st_shortstring:
  254. begin
  255. {!!!!!!!}
  256. internalerror(8888);
  257. end;
  258. st_ansistring:
  259. begin
  260. {!!!!!!!}
  261. internalerror(8888);
  262. end;
  263. st_widestring:
  264. begin
  265. {!!!!!!!}
  266. internalerror(8888);
  267. end;
  268. end;
  269. st_ansistring:
  270. case pstringdef(pfrom^.resulttype)^.string_typ of
  271. st_shortstring:
  272. begin
  273. clear_location(pto^.location);
  274. pto^.location.loc:=LOC_REFERENCE;
  275. gettempofsizereference(pto^.resulttype^.size,pto^.location.reference);
  276. ltemptoremove^.concat(new(ptemptodestroy,init(pto^.location.reference,pto^.resulttype)));
  277. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_L,0,newreference(pto^.location.reference))));
  278. pushusedregisters(pushed,$ff);
  279. emit_push_lea_loc(pfrom^.location);
  280. emit_push_lea_loc(pto^.location);
  281. emitcall('FPC_SHORTSTR_TO_ANSISTR',true);
  282. maybe_loadesi;
  283. popusedregisters(pushed);
  284. ungetiftemp(pfrom^.location.reference);
  285. end;
  286. st_longstring:
  287. begin
  288. {!!!!!!!}
  289. internalerror(8888);
  290. end;
  291. st_widestring:
  292. begin
  293. {!!!!!!!}
  294. internalerror(8888);
  295. end;
  296. end;
  297. st_widestring:
  298. case pstringdef(pfrom^.resulttype)^.string_typ of
  299. st_shortstring:
  300. begin
  301. {!!!!!!!}
  302. internalerror(8888);
  303. end;
  304. st_longstring:
  305. begin
  306. {!!!!!!!}
  307. internalerror(8888);
  308. end;
  309. st_ansistring:
  310. begin
  311. {!!!!!!!}
  312. internalerror(8888);
  313. end;
  314. st_widestring:
  315. begin
  316. {!!!!!!!}
  317. internalerror(8888);
  318. end;
  319. end;
  320. end;
  321. end;
  322. procedure second_cstring_to_pchar(pto,pfrom : ptree;convtyp : tconverttype);
  323. begin
  324. clear_location(pto^.location);
  325. pto^.location.loc:=LOC_REGISTER;
  326. pto^.location.register:=getregister32;
  327. case pstringdef(pfrom^.resulttype)^.string_typ of
  328. st_shortstring :
  329. begin
  330. inc(pfrom^.location.reference.offset);
  331. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,newreference(pfrom^.location.reference),
  332. pto^.location.register)));
  333. end;
  334. st_ansistring :
  335. begin
  336. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(pfrom^.location.reference),
  337. pto^.location.register)));
  338. end;
  339. st_longstring:
  340. begin
  341. {!!!!!!!}
  342. internalerror(8888);
  343. end;
  344. st_widestring:
  345. begin
  346. {!!!!!!!}
  347. internalerror(8888);
  348. end;
  349. end;
  350. end;
  351. procedure second_string_to_chararray(pto,pfrom : ptree;convtyp : tconverttype);
  352. begin
  353. case pstringdef(pfrom^.resulttype)^.string_typ of
  354. st_shortstring :
  355. begin
  356. inc(pto^.location.reference.offset);
  357. end;
  358. st_ansistring :
  359. begin
  360. {!!!!!!!}
  361. internalerror(8888);
  362. end;
  363. st_longstring:
  364. begin
  365. {!!!!!!!}
  366. internalerror(8888);
  367. end;
  368. st_widestring:
  369. begin
  370. {!!!!!!!}
  371. internalerror(8888);
  372. end;
  373. end;
  374. end;
  375. procedure second_array_to_pointer(pto,pfrom : ptree;convtyp : tconverttype);
  376. begin
  377. del_reference(pfrom^.location.reference);
  378. clear_location(pto^.location);
  379. pto^.location.loc:=LOC_REGISTER;
  380. pto^.location.register:=getregister32;
  381. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,newreference(pfrom^.location.reference),
  382. pto^.location.register)));
  383. end;
  384. procedure second_pointer_to_array(pto,pfrom : ptree;convtyp : tconverttype);
  385. begin
  386. clear_location(pto^.location);
  387. pto^.location.loc:=LOC_REFERENCE;
  388. clear_reference(pto^.location.reference);
  389. case pfrom^.location.loc of
  390. LOC_REGISTER :
  391. pto^.location.reference.base:=pfrom^.location.register;
  392. LOC_CREGISTER :
  393. begin
  394. pto^.location.reference.base:=getregister32;
  395. emit_reg_reg(A_MOV,S_L,pfrom^.location.register,pto^.location.reference.base);
  396. end
  397. else
  398. begin
  399. del_reference(pfrom^.location.reference);
  400. pto^.location.reference.base:=getregister32;
  401. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(pfrom^.location.reference),
  402. pto^.location.reference.base)));
  403. end;
  404. end;
  405. end;
  406. { generates the code for the type conversion from an array of char }
  407. { to a string }
  408. procedure second_chararray_to_string(pto,pfrom : ptree;convtyp : tconverttype);
  409. var
  410. pushed : tpushed;
  411. l : longint;
  412. begin
  413. { calc the length of the array }
  414. l:=parraydef(pfrom^.resulttype)^.highrange-parraydef(pfrom^.resulttype)^.lowrange+1;
  415. { this is a type conversion which copies the data, so we can't }
  416. { return a reference }
  417. clear_location(pto^.location);
  418. pto^.location.loc:=LOC_MEM;
  419. case pstringdef(pto^.resulttype)^.string_typ of
  420. st_shortstring :
  421. begin
  422. if l>255 then
  423. begin
  424. CGMessage(type_e_mismatch);
  425. l:=255;
  426. end;
  427. { first get the memory for the string }
  428. gettempofsizereference(256,pto^.location.reference);
  429. { write the length }
  430. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_B,l,
  431. newreference(pto^.location.reference))));
  432. { copy to first char of string }
  433. inc(pto^.location.reference.offset);
  434. { generates the copy code }
  435. { and we need the source never }
  436. concatcopy(pfrom^.location.reference,pto^.location.reference,l,true,false);
  437. { correct the string location }
  438. dec(pto^.location.reference.offset);
  439. end;
  440. st_ansistring :
  441. begin
  442. gettempofsizereference(4,pto^.location.reference);
  443. ltemptoremove^.concat(new(ptemptodestroy,init(pto^.location.reference,pto^.resulttype)));
  444. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_L,0,newreference(pto^.location.reference))));
  445. release_loc(pfrom^.location);
  446. pushusedregisters(pushed,$ff);
  447. push_int(l);
  448. emitpushreferenceaddr(exprasmlist,pfrom^.location.reference);
  449. emitpushreferenceaddr(exprasmlist,pto^.location.reference);
  450. emitcall('FPC_CHARARRAY_TO_ANSISTR',true);
  451. popusedregisters(pushed);
  452. maybe_loadesi;
  453. end;
  454. st_longstring:
  455. begin
  456. {!!!!!!!}
  457. internalerror(8888);
  458. end;
  459. st_widestring:
  460. begin
  461. {!!!!!!!}
  462. internalerror(8888);
  463. end;
  464. end;
  465. end;
  466. procedure second_char_to_string(pto,pfrom : ptree;convtyp : tconverttype);
  467. var
  468. pushed : tpushed;
  469. begin
  470. clear_location(pto^.location);
  471. pto^.location.loc:=LOC_MEM;
  472. case pstringdef(pto^.resulttype)^.string_typ of
  473. st_shortstring :
  474. begin
  475. gettempofsizereference(256,pto^.location.reference);
  476. { call loadstring with correct left and right }
  477. pto^.right:=pfrom;
  478. pto^.left:=pto;
  479. loadshortstring(pto);
  480. pto^.left:=nil; { reset left tree, which is empty }
  481. { pto^.right is not disposed for typeconv !! PM }
  482. disposetree(pto^.right);
  483. pto^.right:=nil;
  484. end;
  485. st_ansistring :
  486. begin
  487. gettempofsizereference(4,pto^.location.reference);
  488. ltemptoremove^.concat(new(ptemptodestroy,init(pto^.location.reference,pto^.resulttype)));
  489. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_L,0,newreference(pto^.location.reference))));
  490. release_loc(pfrom^.location);
  491. pushusedregisters(pushed,$ff);
  492. emit_pushw_loc(pfrom^.location);
  493. emitpushreferenceaddr(exprasmlist,pto^.location.reference);
  494. emitcall('FPC_CHAR_TO_ANSISTR',true);
  495. popusedregisters(pushed);
  496. maybe_loadesi;
  497. end;
  498. else
  499. internalerror(4179);
  500. end;
  501. end;
  502. procedure second_int_to_real(pto,pfrom : ptree;convtyp : tconverttype);
  503. var
  504. r : preference;
  505. hregister : tregister;
  506. begin
  507. { for u32bit a solution is to push $0 and to load a comp }
  508. { does this first, it destroys maybe EDI }
  509. hregister:=R_EDI;
  510. if porddef(pfrom^.resulttype)^.typ=u32bit then
  511. push_int(0);
  512. if (pfrom^.location.loc=LOC_REGISTER) or
  513. (pfrom^.location.loc=LOC_CREGISTER) then
  514. begin
  515. case porddef(pfrom^.resulttype)^.typ of
  516. s8bit : exprasmlist^.concat(new(pai386,op_reg_reg(A_MOVSX,S_BL,pfrom^.location.register,R_EDI)));
  517. u8bit : exprasmlist^.concat(new(pai386,op_reg_reg(A_MOVZX,S_BL,pfrom^.location.register,R_EDI)));
  518. s16bit : exprasmlist^.concat(new(pai386,op_reg_reg(A_MOVSX,S_WL,pfrom^.location.register,R_EDI)));
  519. u16bit : exprasmlist^.concat(new(pai386,op_reg_reg(A_MOVZX,S_WL,pfrom^.location.register,R_EDI)));
  520. u32bit,s32bit:
  521. hregister:=pfrom^.location.register
  522. end;
  523. ungetregister(pfrom^.location.register);
  524. end
  525. else
  526. begin
  527. r:=newreference(pfrom^.location.reference);
  528. case porddef(pfrom^.resulttype)^.typ of
  529. s8bit : exprasmlist^.concat(new(pai386,op_ref_reg(A_MOVSX,S_BL,r,R_EDI)));
  530. u8bit : exprasmlist^.concat(new(pai386,op_ref_reg(A_MOVZX,S_BL,r,R_EDI)));
  531. s16bit : exprasmlist^.concat(new(pai386,op_ref_reg(A_MOVSX,S_WL,r,R_EDI)));
  532. u16bit : exprasmlist^.concat(new(pai386,op_ref_reg(A_MOVZX,S_WL,r,R_EDI)));
  533. u32bit,s32bit : exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_EDI)));
  534. end;
  535. del_reference(pfrom^.location.reference);
  536. ungetiftemp(pfrom^.location.reference);
  537. end;
  538. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,hregister)));
  539. r:=new_reference(R_ESP,0);
  540. if porddef(pfrom^.resulttype)^.typ=u32bit then
  541. exprasmlist^.concat(new(pai386,op_ref(A_FILD,S_IQ,r)))
  542. else
  543. exprasmlist^.concat(new(pai386,op_ref(A_FILD,S_IL,r)));
  544. { better than an add on all processors }
  545. if porddef(pfrom^.resulttype)^.typ=u32bit then
  546. exprasmlist^.concat(new(pai386,op_const_reg(A_ADD,S_L,8,R_ESP)))
  547. else
  548. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EDI)));
  549. clear_location(pto^.location);
  550. pto^.location.loc:=LOC_FPU;
  551. end;
  552. procedure second_real_to_fix(pto,pfrom : ptree;convtyp : tconverttype);
  553. var
  554. rreg : tregister;
  555. ref : treference;
  556. begin
  557. { real must be on fpu stack }
  558. if (pfrom^.location.loc<>LOC_FPU) then
  559. exprasmlist^.concat(new(pai386,op_ref(A_FLD,S_FL,newreference(pfrom^.location.reference))));
  560. push_int($1f3f);
  561. push_int(65536);
  562. reset_reference(ref);
  563. ref.base:=R_ESP;
  564. exprasmlist^.concat(new(pai386,op_ref(A_FIMUL,S_IL,newreference(ref))));
  565. ref.offset:=4;
  566. exprasmlist^.concat(new(pai386,op_ref(A_FSTCW,S_NO,newreference(ref))));
  567. ref.offset:=6;
  568. exprasmlist^.concat(new(pai386,op_ref(A_FLDCW,S_NO,newreference(ref))));
  569. ref.offset:=0;
  570. exprasmlist^.concat(new(pai386,op_ref(A_FISTP,S_IL,newreference(ref))));
  571. ref.offset:=4;
  572. exprasmlist^.concat(new(pai386,op_ref(A_FLDCW,S_NO,newreference(ref))));
  573. rreg:=getregister32;
  574. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,rreg)));
  575. { better than an add on all processors }
  576. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EDI)));
  577. clear_location(pto^.location);
  578. pto^.location.loc:=LOC_REGISTER;
  579. pto^.location.register:=rreg;
  580. end;
  581. procedure second_real_to_real(pto,pfrom : ptree;convtyp : tconverttype);
  582. begin
  583. case pfrom^.location.loc of
  584. LOC_FPU : ;
  585. LOC_MEM,
  586. LOC_REFERENCE:
  587. begin
  588. floatload(pfloatdef(pfrom^.resulttype)^.typ,
  589. pfrom^.location.reference);
  590. { we have to free the reference }
  591. del_reference(pfrom^.location.reference);
  592. end;
  593. end;
  594. clear_location(pto^.location);
  595. pto^.location.loc:=LOC_FPU;
  596. end;
  597. procedure second_fix_to_real(pto,pfrom : ptree;convtyp : tconverttype);
  598. var
  599. popeax,popebx,popecx,popedx : boolean;
  600. startreg : tregister;
  601. hl : plabel;
  602. r : treference;
  603. begin
  604. if (pfrom^.location.loc=LOC_REGISTER) or
  605. (pfrom^.location.loc=LOC_CREGISTER) then
  606. begin
  607. startreg:=pfrom^.location.register;
  608. ungetregister(startreg);
  609. popeax:=(startreg<>R_EAX) and not (R_EAX in unused);
  610. if popeax then
  611. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EAX)));
  612. { mov eax,eax is removed by emit_reg_reg }
  613. emit_reg_reg(A_MOV,S_L,startreg,R_EAX);
  614. end
  615. else
  616. begin
  617. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(
  618. pfrom^.location.reference),R_EAX)));
  619. del_reference(pfrom^.location.reference);
  620. startreg:=R_NO;
  621. end;
  622. popebx:=(startreg<>R_EBX) and not (R_EBX in unused);
  623. if popebx then
  624. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EBX)));
  625. popecx:=(startreg<>R_ECX) and not (R_ECX in unused);
  626. if popecx then
  627. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ECX)));
  628. popedx:=(startreg<>R_EDX) and not (R_EDX in unused);
  629. if popedx then
  630. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EDX)));
  631. exprasmlist^.concat(new(pai386,op_none(A_CDQ,S_NO)));
  632. emit_reg_reg(A_XOR,S_L,R_EDX,R_EAX);
  633. emit_reg_reg(A_MOV,S_L,R_EAX,R_EBX);
  634. emit_reg_reg(A_SUB,S_L,R_EDX,R_EAX);
  635. getlabel(hl);
  636. emitjmp(C_Z,hl);
  637. exprasmlist^.concat(new(pai386,op_const_reg(A_RCL,S_L,1,R_EBX)));
  638. emit_reg_reg(A_BSR,S_L,R_EAX,R_EDX);
  639. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_B,32,R_CL)));
  640. emit_reg_reg(A_SUB,S_B,R_DL,R_CL);
  641. emit_reg_reg(A_SHL,S_L,R_CL,R_EAX);
  642. exprasmlist^.concat(new(pai386,op_const_reg(A_ADD,S_W,1007,R_DX)));
  643. exprasmlist^.concat(new(pai386,op_const_reg(A_SHL,S_W,5,R_DX)));
  644. exprasmlist^.concat(new(pai386,op_const_reg_reg(A_SHLD,S_W,11,R_DX,R_BX)));
  645. exprasmlist^.concat(new(pai386,op_const_reg_reg(A_SHLD,S_L,20,R_EAX,R_EBX)));
  646. exprasmlist^.concat(new(pai386,op_const_reg(A_SHL,S_L,20,R_EAX)));
  647. emitlab(hl);
  648. { better than an add on all processors }
  649. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EBX)));
  650. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EAX)));
  651. reset_reference(r);
  652. r.base:=R_ESP;
  653. exprasmlist^.concat(new(pai386,op_ref(A_FLD,S_FL,newreference(r))));
  654. exprasmlist^.concat(new(pai386,op_const_reg(A_ADD,S_L,8,R_ESP)));
  655. if popedx then
  656. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EDX)));
  657. if popecx then
  658. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_ECX)));
  659. if popebx then
  660. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EBX)));
  661. if popeax then
  662. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EAX)));
  663. clear_location(pto^.location);
  664. pto^.location.loc:=LOC_FPU;
  665. end;
  666. procedure second_int_to_fix(pto,pfrom : ptree;convtyp : tconverttype);
  667. var
  668. hregister : tregister;
  669. begin
  670. if (pfrom^.location.loc=LOC_REGISTER) then
  671. hregister:=pfrom^.location.register
  672. else if (pfrom^.location.loc=LOC_CREGISTER) then
  673. hregister:=getregister32
  674. else
  675. begin
  676. del_reference(pfrom^.location.reference);
  677. hregister:=getregister32;
  678. case porddef(pfrom^.resulttype)^.typ of
  679. s8bit : exprasmlist^.concat(new(pai386,op_ref_reg(A_MOVSX,S_BL,newreference(pfrom^.location.reference),
  680. hregister)));
  681. u8bit : exprasmlist^.concat(new(pai386,op_ref_reg(A_MOVZX,S_BL,newreference(pfrom^.location.reference),
  682. hregister)));
  683. s16bit : exprasmlist^.concat(new(pai386,op_ref_reg(A_MOVSX,S_WL,newreference(pfrom^.location.reference),
  684. hregister)));
  685. u16bit : exprasmlist^.concat(new(pai386,op_ref_reg(A_MOVZX,S_WL,newreference(pfrom^.location.reference),
  686. hregister)));
  687. u32bit,s32bit : exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(pfrom^.location.reference),
  688. hregister)));
  689. {!!!! u32bit }
  690. end;
  691. end;
  692. exprasmlist^.concat(new(pai386,op_const_reg(A_SHL,S_L,16,hregister)));
  693. clear_location(pto^.location);
  694. pto^.location.loc:=LOC_REGISTER;
  695. pto^.location.register:=hregister;
  696. end;
  697. procedure second_proc_to_procvar(pto,pfrom : ptree;convtyp : tconverttype);
  698. begin
  699. { method pointer ? }
  700. if assigned(pfrom^.left) then
  701. begin
  702. set_location(pto^.location,pfrom^.location);
  703. end
  704. else
  705. begin
  706. clear_location(pto^.location);
  707. pto^.location.loc:=LOC_REGISTER;
  708. pto^.location.register:=getregister32;
  709. del_reference(pfrom^.location.reference);
  710. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  711. newreference(pfrom^.location.reference),pto^.location.register)));
  712. end;
  713. end;
  714. procedure second_bool_to_int(pto,pfrom : ptree;convtyp : tconverttype);
  715. var
  716. oldtruelabel,oldfalselabel,hlabel : plabel;
  717. hregister : tregister;
  718. newsize,
  719. opsize : topsize;
  720. op : tasmop;
  721. begin
  722. oldtruelabel:=truelabel;
  723. oldfalselabel:=falselabel;
  724. getlabel(truelabel);
  725. getlabel(falselabel);
  726. secondpass(pfrom);
  727. { byte(boolean) or word(wordbool) or longint(longbool) must
  728. be accepted for var parameters }
  729. if (pto^.explizit) and
  730. (pfrom^.resulttype^.size=pto^.resulttype^.size) and
  731. (pfrom^.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  732. begin
  733. set_location(pto^.location,pfrom^.location);
  734. freelabel(truelabel);
  735. freelabel(falselabel);
  736. truelabel:=oldtruelabel;
  737. falselabel:=oldfalselabel;
  738. exit;
  739. end;
  740. clear_location(pto^.location);
  741. pto^.location.loc:=LOC_REGISTER;
  742. del_reference(pfrom^.location.reference);
  743. case pfrom^.resulttype^.size of
  744. 1 : begin
  745. case pto^.resulttype^.size of
  746. 1 : opsize:=S_B;
  747. 2 : opsize:=S_BW;
  748. 4 : opsize:=S_BL;
  749. end;
  750. end;
  751. 2 : begin
  752. case pto^.resulttype^.size of
  753. 1 : begin
  754. if pfrom^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  755. pfrom^.location.register:=reg16toreg8(pfrom^.location.register);
  756. opsize:=S_B;
  757. end;
  758. 2 : opsize:=S_W;
  759. 4 : opsize:=S_WL;
  760. end;
  761. end;
  762. 4 : begin
  763. case pto^.resulttype^.size of
  764. 1 : begin
  765. if pfrom^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  766. pfrom^.location.register:=reg32toreg8(pfrom^.location.register);
  767. opsize:=S_B;
  768. end;
  769. 2 : begin
  770. if pfrom^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  771. pfrom^.location.register:=reg32toreg16(pfrom^.location.register);
  772. opsize:=S_W;
  773. end;
  774. 4 : opsize:=S_L;
  775. end;
  776. end;
  777. end;
  778. if opsize in [S_B,S_W,S_L] then
  779. op:=A_MOV
  780. else
  781. if is_signed(pto^.resulttype) then
  782. op:=A_MOVSX
  783. else
  784. op:=A_MOVZX;
  785. hregister:=getregister32;
  786. case pto^.resulttype^.size of
  787. 1 : begin
  788. pto^.location.register:=reg32toreg8(hregister);
  789. newsize:=S_B;
  790. end;
  791. 2 : begin
  792. pto^.location.register:=reg32toreg16(hregister);
  793. newsize:=S_W;
  794. end;
  795. 4 : begin
  796. pto^.location.register:=hregister;
  797. newsize:=S_L;
  798. end;
  799. else
  800. internalerror(10060);
  801. end;
  802. case pfrom^.location.loc of
  803. LOC_MEM,
  804. LOC_REFERENCE : exprasmlist^.concat(new(pai386,op_ref_reg(op,opsize,
  805. newreference(pfrom^.location.reference),pto^.location.register)));
  806. LOC_REGISTER,
  807. LOC_CREGISTER : begin
  808. { remove things like movb %al,%al }
  809. if pfrom^.location.register<>pto^.location.register then
  810. exprasmlist^.concat(new(pai386,op_reg_reg(op,opsize,
  811. pfrom^.location.register,pto^.location.register)));
  812. end;
  813. LOC_FLAGS : begin
  814. emit_flag2reg(pfrom^.location.resflags,pto^.location.register);
  815. end;
  816. LOC_JUMP : begin
  817. getlabel(hlabel);
  818. emitlab(truelabel);
  819. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,newsize,1,hregister)));
  820. emitjmp(C_None,hlabel);
  821. emitlab(falselabel);
  822. exprasmlist^.concat(new(pai386,op_reg_reg(A_XOR,newsize,hregister,hregister)));
  823. emitlab(hlabel);
  824. end;
  825. else
  826. internalerror(10061);
  827. end;
  828. freelabel(truelabel);
  829. freelabel(falselabel);
  830. truelabel:=oldtruelabel;
  831. falselabel:=oldfalselabel;
  832. end;
  833. procedure second_int_to_bool(pto,pfrom : ptree;convtyp : tconverttype);
  834. var
  835. hregister : tregister;
  836. flags : tresflags;
  837. begin
  838. clear_location(pto^.location);
  839. { byte(boolean) or word(wordbool) or longint(longbool) must
  840. be accepted for var parameters }
  841. if (pto^.explizit) and
  842. (pfrom^.resulttype^.size=pto^.resulttype^.size) and
  843. (pfrom^.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  844. begin
  845. set_location(pto^.location,pfrom^.location);
  846. exit;
  847. end;
  848. pto^.location.loc:=LOC_REGISTER;
  849. del_reference(pfrom^.location.reference);
  850. case pfrom^.location.loc of
  851. LOC_MEM,LOC_REFERENCE :
  852. begin
  853. hregister:=getregister32;
  854. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  855. newreference(pfrom^.location.reference),hregister)));
  856. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,S_L,hregister,hregister)));
  857. flags:=F_NE;
  858. end;
  859. LOC_FLAGS :
  860. begin
  861. hregister:=getregister32;
  862. flags:=pfrom^.location.resflags;
  863. end;
  864. LOC_REGISTER,LOC_CREGISTER :
  865. begin
  866. hregister:=pfrom^.location.register;
  867. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,S_L,hregister,hregister)));
  868. flags:=F_NE;
  869. end;
  870. else
  871. internalerror(10062);
  872. end;
  873. case pto^.resulttype^.size of
  874. 1 : pto^.location.register:=makereg8(hregister);
  875. 2 : pto^.location.register:=makereg16(hregister);
  876. 4 : pto^.location.register:=makereg32(hregister);
  877. else
  878. internalerror(10064);
  879. end;
  880. emit_flag2reg(flags,pto^.location.register);
  881. end;
  882. procedure second_load_smallset(pto,pfrom : ptree;convtyp : tconverttype);
  883. var
  884. href : treference;
  885. pushedregs : tpushed;
  886. begin
  887. href.symbol:=nil;
  888. pushusedregisters(pushedregs,$ff);
  889. gettempofsizereference(32,href);
  890. emitpushreferenceaddr(exprasmlist,pfrom^.location.reference);
  891. emitpushreferenceaddr(exprasmlist,href);
  892. emitcall('FPC_SET_LOAD_SMALL',true);
  893. maybe_loadesi;
  894. popusedregisters(pushedregs);
  895. clear_location(pto^.location);
  896. pto^.location.loc:=LOC_MEM;
  897. pto^.location.reference:=href;
  898. end;
  899. procedure second_ansistring_to_pchar(pto,pfrom : ptree;convtyp : tconverttype);
  900. var
  901. l1,l2 : plabel;
  902. hr : preference;
  903. begin
  904. clear_location(pto^.location);
  905. pto^.location.loc:=LOC_REGISTER;
  906. getlabel(l1);
  907. getlabel(l2);
  908. case pfrom^.location.loc of
  909. LOC_CREGISTER,LOC_REGISTER:
  910. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,S_L,0,
  911. pfrom^.location.register)));
  912. LOC_MEM,LOC_REFERENCE:
  913. begin
  914. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_L,0,
  915. newreference(pfrom^.location.reference))));
  916. del_reference(pfrom^.location.reference);
  917. pto^.location.register:=getregister32;
  918. end;
  919. end;
  920. emitjmp(C_Z,l1);
  921. if pfrom^.location.loc in [LOC_MEM,LOC_REFERENCE] then
  922. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(
  923. pfrom^.location.reference),
  924. pto^.location.register)));
  925. emitjmp(C_None,l2);
  926. emitlab(l1);
  927. new(hr);
  928. reset_reference(hr^);
  929. hr^.symbol:=newasmsymbol('FPC_EMPTYCHAR');
  930. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,hr,
  931. pto^.location.register)));
  932. emitlab(l2);
  933. end;
  934. procedure second_pchar_to_string(pto,pfrom : ptree;convtyp : tconverttype);
  935. var
  936. pushed : tpushed;
  937. begin
  938. case pstringdef(pto^.resulttype)^.string_typ of
  939. st_shortstring:
  940. begin
  941. pushusedregisters(pushed,$ff);
  942. gettempofsizereference(pto^.resulttype^.size,pto^.location.reference);
  943. case pfrom^.location.loc of
  944. LOC_REGISTER,LOC_CREGISTER:
  945. begin
  946. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,pfrom^.location.register)));
  947. ungetregister32(pfrom^.location.register);
  948. end;
  949. LOC_REFERENCE,LOC_MEM:
  950. begin
  951. emit_push_mem(pfrom^.location.reference);
  952. del_reference(pfrom^.location.reference);
  953. end;
  954. end;
  955. emitpushreferenceaddr(exprasmlist,pto^.location.reference);
  956. emitcall('FPC_PCHAR_TO_SHORTSTR',true);
  957. maybe_loadesi;
  958. popusedregisters(pushed);
  959. end;
  960. st_ansistring:
  961. begin
  962. gettempofsizereference(pto^.resulttype^.size,pto^.location.reference);
  963. ltemptoremove^.concat(new(ptemptodestroy,init(pto^.location.reference,pto^.resulttype)));
  964. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_L,0,newreference(pto^.location.reference))));
  965. case pfrom^.location.loc of
  966. LOC_REGISTER,LOC_CREGISTER:
  967. begin
  968. ungetregister32(pfrom^.location.register);
  969. pushusedregisters(pushed,$ff);
  970. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,pfrom^.location.register)));
  971. end;
  972. LOC_REFERENCE,LOC_MEM:
  973. begin
  974. del_reference(pfrom^.location.reference);
  975. pushusedregisters(pushed,$ff);
  976. emit_push_mem(pfrom^.location.reference);
  977. end;
  978. end;
  979. emitpushreferenceaddr(exprasmlist,pto^.location.reference);
  980. emitcall('FPC_PCHAR_TO_ANSISTR',true);
  981. maybe_loadesi;
  982. popusedregisters(pushed);
  983. end;
  984. else
  985. begin
  986. clear_location(pto^.location);
  987. pto^.location.loc:=LOC_REGISTER;
  988. internalerror(12121);
  989. end;
  990. end;
  991. end;
  992. procedure second_nothing(pto,pfrom : ptree;convtyp : tconverttype);
  993. begin
  994. end;
  995. {****************************************************************************
  996. SecondTypeConv
  997. ****************************************************************************}
  998. procedure secondtypeconv(var p : ptree);
  999. const
  1000. secondconvert : array[tconverttype] of tsecondconvproc = (
  1001. second_nothing, {equal}
  1002. second_nothing, {not_possible}
  1003. second_string_to_string,
  1004. second_char_to_string,
  1005. second_pchar_to_string,
  1006. second_nothing, {cchar_to_pchar}
  1007. second_cstring_to_pchar,
  1008. second_ansistring_to_pchar,
  1009. second_string_to_chararray,
  1010. second_chararray_to_string,
  1011. second_array_to_pointer,
  1012. second_pointer_to_array,
  1013. second_int_to_int,
  1014. second_bool_to_int,
  1015. second_int_to_bool,
  1016. second_real_to_real,
  1017. second_int_to_real,
  1018. second_int_to_fix,
  1019. second_real_to_fix,
  1020. second_fix_to_real,
  1021. second_proc_to_procvar,
  1022. second_nothing, {arrayconstructor_to_set}
  1023. second_load_smallset
  1024. );
  1025. var
  1026. oldrl,oldlrl : plinkedlist;
  1027. begin
  1028. { the ansi string disposing is a little bit hairy: }
  1029. oldrl:=temptoremove;
  1030. temptoremove:=new(plinkedlist,init);
  1031. { this isn't good coding, I think tc_bool_2_int, shouldn't be }
  1032. { type conversion (FK) }
  1033. { this is necessary, because second_bool_2_int, have to change }
  1034. { true- and false label before calling secondpass }
  1035. { the helper routines need access to the release list }
  1036. oldlrl:=ltemptoremove;
  1037. ltemptoremove:=oldrl;
  1038. if p^.convtyp<>tc_bool_2_int then
  1039. begin
  1040. secondpass(p^.left);
  1041. set_location(p^.location,p^.left^.location);
  1042. if codegenerror then
  1043. exit;
  1044. end;
  1045. { the second argument only is for maybe_range_checking !}
  1046. secondconvert[p^.convtyp](p,p^.left,p^.convtyp);
  1047. { clean up all temp. objects (ansi/widestrings) }
  1048. removetemps(exprasmlist,temptoremove);
  1049. dispose(temptoremove,done);
  1050. temptoremove:=oldrl;
  1051. ltemptoremove:=oldlrl;
  1052. end;
  1053. {*****************************************************************************
  1054. SecondIs
  1055. *****************************************************************************}
  1056. procedure secondis(var p : ptree);
  1057. var
  1058. pushed : tpushed;
  1059. begin
  1060. { save all used registers }
  1061. pushusedregisters(pushed,$ff);
  1062. secondpass(p^.left);
  1063. clear_location(p^.location);
  1064. p^.location.loc:=LOC_FLAGS;
  1065. p^.location.resflags:=F_NE;
  1066. { push instance to check: }
  1067. case p^.left^.location.loc of
  1068. LOC_REGISTER,LOC_CREGISTER:
  1069. begin
  1070. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,
  1071. S_L,p^.left^.location.register)));
  1072. ungetregister32(p^.left^.location.register);
  1073. end;
  1074. LOC_MEM,LOC_REFERENCE:
  1075. begin
  1076. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,
  1077. S_L,newreference(p^.left^.location.reference))));
  1078. del_reference(p^.left^.location.reference);
  1079. end;
  1080. else internalerror(100);
  1081. end;
  1082. { generate type checking }
  1083. secondpass(p^.right);
  1084. case p^.right^.location.loc of
  1085. LOC_REGISTER,LOC_CREGISTER:
  1086. begin
  1087. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,
  1088. S_L,p^.right^.location.register)));
  1089. ungetregister32(p^.right^.location.register);
  1090. end;
  1091. LOC_MEM,LOC_REFERENCE:
  1092. begin
  1093. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,
  1094. S_L,newreference(p^.right^.location.reference))));
  1095. del_reference(p^.right^.location.reference);
  1096. end;
  1097. else internalerror(100);
  1098. end;
  1099. emitcall('FPC_DO_IS',true);
  1100. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,S_B,R_AL,R_AL)));
  1101. popusedregisters(pushed);
  1102. end;
  1103. {*****************************************************************************
  1104. SecondAs
  1105. *****************************************************************************}
  1106. procedure secondas(var p : ptree);
  1107. var
  1108. pushed : tpushed;
  1109. begin
  1110. secondpass(p^.left);
  1111. { save all used registers }
  1112. pushusedregisters(pushed,$ff);
  1113. { push instance to check: }
  1114. case p^.left^.location.loc of
  1115. LOC_REGISTER,LOC_CREGISTER:
  1116. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,
  1117. S_L,p^.left^.location.register)));
  1118. LOC_MEM,LOC_REFERENCE:
  1119. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,
  1120. S_L,newreference(p^.left^.location.reference))));
  1121. else internalerror(100);
  1122. end;
  1123. { we doesn't modifiy the left side, we check only the type }
  1124. set_location(p^.location,p^.left^.location);
  1125. { generate type checking }
  1126. secondpass(p^.right);
  1127. case p^.right^.location.loc of
  1128. LOC_REGISTER,LOC_CREGISTER:
  1129. begin
  1130. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,
  1131. S_L,p^.right^.location.register)));
  1132. ungetregister32(p^.right^.location.register);
  1133. end;
  1134. LOC_MEM,LOC_REFERENCE:
  1135. begin
  1136. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,
  1137. S_L,newreference(p^.right^.location.reference))));
  1138. del_reference(p^.right^.location.reference);
  1139. end;
  1140. else internalerror(100);
  1141. end;
  1142. emitcall('FPC_DO_AS',true);
  1143. { restore register, this restores automatically the }
  1144. { result }
  1145. popusedregisters(pushed);
  1146. end;
  1147. end.
  1148. {
  1149. $Log$
  1150. Revision 1.61 1999-03-05 16:14:20 peter
  1151. * fixed boolean() typecast
  1152. Revision 1.60 1999/03/02 18:24:19 peter
  1153. * fixed overloading of array of char
  1154. Revision 1.59 1999/03/01 15:46:18 peter
  1155. * ag386bin finally make cycles correct
  1156. * prefixes are now also normal opcodes
  1157. Revision 1.58 1999/02/25 21:02:23 peter
  1158. * ag386bin updates
  1159. + coff writer
  1160. Revision 1.57 1999/02/22 02:15:06 peter
  1161. * updates for ag386bin
  1162. Revision 1.56 1999/02/15 11:30:39 pierre
  1163. * memory leaks removed
  1164. Revision 1.55 1999/02/12 10:43:57 florian
  1165. * internal error 10 with ansistrings fixed
  1166. Revision 1.54 1999/02/02 12:35:02 florian
  1167. * ltemptoremove handling corrected
  1168. Revision 1.53 1999/02/02 11:47:55 peter
  1169. * fixed ansi2short
  1170. Revision 1.52 1999/01/29 11:22:13 pierre
  1171. * saving and restoring old ltemptoremove
  1172. Revision 1.51 1999/01/28 19:50:15 peter
  1173. * removed warning
  1174. Revision 1.50 1999/01/28 14:06:45 florian
  1175. * small fix for method pointers
  1176. * found the annoying strpas bug, mainly nested call to type cast which
  1177. use ansistrings crash
  1178. Revision 1.49 1999/01/27 14:56:56 pierre
  1179. * typo error corrected solves bug0190 and bug0204
  1180. Revision 1.48 1999/01/27 13:03:27 pierre
  1181. boolean to int conversion problems bug0205 bug0208
  1182. Revision 1.47 1999/01/27 12:59:32 pierre
  1183. wrong commit info : tccnv.pas
  1184. Revision 1.46 1999/01/27 00:13:53 florian
  1185. * "procedure of object"-stuff fixed
  1186. Revision 1.45 1999/01/21 22:10:36 peter
  1187. * fixed array of const
  1188. * generic platform independent high() support
  1189. Revision 1.44 1999/01/19 10:18:59 florian
  1190. * bug with mul. of dwords fixed, reported by Alexander Stohr
  1191. * some changes to compile with TP
  1192. + small enhancements for the new code generator
  1193. Revision 1.43 1998/12/22 13:10:59 florian
  1194. * memory leaks for ansistring type casts fixed
  1195. Revision 1.42 1998/12/19 00:23:42 florian
  1196. * ansistring memory leaks fixed
  1197. Revision 1.41 1998/11/30 19:48:54 peter
  1198. * some more rangecheck fixes
  1199. Revision 1.40 1998/11/30 09:43:02 pierre
  1200. * some range check bugs fixed (still not working !)
  1201. + added DLL writing support for win32 (also accepts variables)
  1202. + TempAnsi for code that could be used for Temporary ansi strings
  1203. handling
  1204. Revision 1.39 1998/11/29 22:37:30 peter
  1205. * fixed constant ansistring -> pchar
  1206. Revision 1.38 1998/11/29 12:40:19 peter
  1207. * newcnv -> not oldcnv
  1208. Revision 1.37 1998/11/26 21:33:06 peter
  1209. * rangecheck updates
  1210. Revision 1.36 1998/11/26 14:39:11 peter
  1211. * ansistring -> pchar fixed
  1212. * ansistring constants fixed
  1213. * ansistring constants are now written once
  1214. Revision 1.35 1998/11/26 13:10:39 peter
  1215. * new int - int conversion -dNEWCNV
  1216. * some function renamings
  1217. Revision 1.34 1998/11/18 15:44:08 peter
  1218. * VALUEPARA for tp7 compatible value parameters
  1219. Revision 1.33 1998/11/17 00:36:39 peter
  1220. * more ansistring fixes
  1221. Revision 1.32 1998/11/16 15:35:38 peter
  1222. * rename laod/copystring -> load/copyshortstring
  1223. * fixed int-bool cnv bug
  1224. + char-ansistring conversion
  1225. Revision 1.31 1998/11/05 12:02:30 peter
  1226. * released useansistring
  1227. * removed -Sv, its now available in fpc modes
  1228. Revision 1.30 1998/10/27 11:12:45 peter
  1229. * fixed char_to_string which did not set the .loc
  1230. Revision 1.29 1998/10/26 15:18:41 peter
  1231. * fixed fldcw,fstcw for as 2.9.1
  1232. Revision 1.28 1998/10/08 17:17:11 pierre
  1233. * current_module old scanner tagged as invalid if unit is recompiled
  1234. + added ppheap for better info on tracegetmem of heaptrc
  1235. (adds line column and file index)
  1236. * several memory leaks removed ith help of heaptrc !!
  1237. Revision 1.27 1998/10/06 17:16:40 pierre
  1238. * some memory leaks fixed (thanks to Peter for heaptrc !)
  1239. Revision 1.26 1998/10/02 07:20:35 florian
  1240. * range checking in units doesn't work if the units are smartlinked, fixed
  1241. Revision 1.25 1998/09/30 12:14:24 peter
  1242. * fixed boolean(longbool) conversion
  1243. Revision 1.24 1998/09/27 10:16:22 florian
  1244. * type casts pchar<->ansistring fixed
  1245. * ansistring[..] calls does now an unique call
  1246. Revision 1.23 1998/09/23 12:03:51 peter
  1247. * overloading fix for array of const
  1248. Revision 1.22 1998/09/22 15:34:09 peter
  1249. + pchar -> string conversion
  1250. Revision 1.21 1998/09/20 17:46:47 florian
  1251. * some things regarding ansistrings fixed
  1252. Revision 1.20 1998/09/17 09:42:12 peter
  1253. + pass_2 for cg386
  1254. * Message() -> CGMessage() for pass_1/pass_2
  1255. Revision 1.19 1998/09/14 10:43:46 peter
  1256. * all internal RTL functions start with FPC_
  1257. Revision 1.18 1998/09/11 12:29:40 pierre
  1258. * removed explicit range_checking as it is buggy
  1259. Revision 1.17.2.1 1998/09/11 12:08:54 pierre
  1260. * removed explicit range_check was buggy
  1261. Revision 1.17 1998/09/04 08:41:38 peter
  1262. * updated some error CGMessages
  1263. Revision 1.16 1998/09/03 17:39:03 florian
  1264. + better code for type conversation longint/dword to real type
  1265. Revision 1.15 1998/09/03 16:24:50 florian
  1266. * bug of type conversation from dword to real fixed
  1267. * bug fix of Jonas applied
  1268. Revision 1.14 1998/08/28 12:51:39 florian
  1269. + ansistring to pchar type cast fixed
  1270. Revision 1.13 1998/08/28 10:56:56 peter
  1271. * removed warnings
  1272. Revision 1.12 1998/08/14 18:18:38 peter
  1273. + dynamic set contruction
  1274. * smallsets are now working (always longint size)
  1275. Revision 1.11 1998/08/10 23:59:59 peter
  1276. * fixed dup log
  1277. Revision 1.10 1998/08/10 14:49:47 peter
  1278. + localswitches, moduleswitches, globalswitches splitting
  1279. Revision 1.9 1998/08/05 16:00:09 florian
  1280. * some fixes for ansi strings
  1281. Revision 1.8 1998/07/18 22:54:24 florian
  1282. * some ansi/wide/longstring support fixed:
  1283. o parameter passing
  1284. o returning as result from functions
  1285. Revision 1.7 1998/06/12 13:10:34 peter
  1286. * small internalerror nr change
  1287. Revision 1.6 1998/06/12 10:43:12 michael
  1288. Fixed ansistrings : is_ansistring not found
  1289. Revision 1.5 1998/06/08 13:13:30 pierre
  1290. + temporary variables now in temp_gen.pas unit
  1291. because it is processor independent
  1292. * mppc68k.bat modified to undefine i386 and support_mmx
  1293. (which are defaults for i386)
  1294. Revision 1.4 1998/06/05 17:44:10 peter
  1295. * splitted cgi386
  1296. Revision 1.3 1998/06/03 22:48:50 peter
  1297. + wordbool,longbool
  1298. * rename bis,von -> high,low
  1299. * moved some systemunit loading/creating to psystem.pas
  1300. Revision 1.2 1998/06/02 10:52:10 peter
  1301. * fixed second_bool_to_int with bool8bit return
  1302. Revision 1.1 1998/06/01 16:50:18 peter
  1303. + boolean -> ord conversion
  1304. * fixed ord -> boolean conversion
  1305. }