cg386con.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Generate i386 assembler for constants
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cg386con;
  19. interface
  20. uses
  21. tree;
  22. procedure secondrealconst(var p : ptree);
  23. procedure secondfixconst(var p : ptree);
  24. procedure secondordconst(var p : ptree);
  25. procedure secondstringconst(var p : ptree);
  26. procedure secondsetconst(var p : ptree);
  27. procedure secondniln(var p : ptree);
  28. implementation
  29. uses
  30. globtype,systems,
  31. cobjects,verbose,globals,
  32. symconst,symtable,aasm,types,
  33. hcodegen,temp_gen,pass_2,
  34. cpubase,cpuasm,
  35. cgai386,tgeni386;
  36. {*****************************************************************************
  37. SecondRealConst
  38. *****************************************************************************}
  39. procedure secondrealconst(var p : ptree);
  40. const
  41. floattype2ait:array[tfloattype] of tait=
  42. (ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_none,ait_none);
  43. var
  44. hp1 : pai;
  45. lastlabel : pasmlabel;
  46. realait : tait;
  47. begin
  48. if (p^.value_real=1.0) then
  49. begin
  50. emit_none(A_FLD1,S_NO);
  51. p^.location.loc:=LOC_FPU;
  52. inc(fpuvaroffset);
  53. end
  54. else if (p^.value_real=0.0) then
  55. begin
  56. emit_none(A_FLDZ,S_NO);
  57. p^.location.loc:=LOC_FPU;
  58. inc(fpuvaroffset);
  59. end
  60. else
  61. begin
  62. lastlabel:=nil;
  63. realait:=floattype2ait[pfloatdef(p^.resulttype)^.typ];
  64. { const already used ? }
  65. if not assigned(p^.lab_real) then
  66. begin
  67. { tries to found an old entry }
  68. hp1:=pai(consts^.first);
  69. while assigned(hp1) do
  70. begin
  71. if hp1^.typ=ait_label then
  72. lastlabel:=pai_label(hp1)^.l
  73. else
  74. begin
  75. if (hp1^.typ=realait) and (lastlabel<>nil) then
  76. begin
  77. if(
  78. ((realait=ait_real_32bit) and (pai_real_32bit(hp1)^.value=p^.value_real)) or
  79. ((realait=ait_real_64bit) and (pai_real_64bit(hp1)^.value=p^.value_real)) or
  80. ((realait=ait_real_80bit) and (pai_real_80bit(hp1)^.value=p^.value_real)) or
  81. ((realait=ait_comp_64bit) and (pai_comp_64bit(hp1)^.value=p^.value_real))
  82. ) then
  83. begin
  84. { found! }
  85. p^.lab_real:=lastlabel;
  86. break;
  87. end;
  88. end;
  89. lastlabel:=nil;
  90. end;
  91. hp1:=pai(hp1^.next);
  92. end;
  93. { :-(, we must generate a new entry }
  94. if not assigned(p^.lab_real) then
  95. begin
  96. getdatalabel(lastlabel);
  97. p^.lab_real:=lastlabel;
  98. if (cs_smartlink in aktmoduleswitches) then
  99. consts^.concat(new(pai_cut,init));
  100. consts^.concat(new(pai_label,init(lastlabel)));
  101. case realait of
  102. ait_real_32bit :
  103. consts^.concat(new(pai_real_32bit,init(p^.value_real)));
  104. ait_real_64bit :
  105. consts^.concat(new(pai_real_64bit,init(p^.value_real)));
  106. ait_real_80bit :
  107. consts^.concat(new(pai_real_80bit,init(p^.value_real)));
  108. ait_comp_64bit :
  109. consts^.concat(new(pai_comp_64bit,init(p^.value_real)));
  110. else
  111. internalerror(10120);
  112. end;
  113. end;
  114. end;
  115. reset_reference(p^.location.reference);
  116. p^.location.reference.symbol:=p^.lab_real;
  117. p^.location.loc:=LOC_MEM;
  118. end;
  119. end;
  120. {*****************************************************************************
  121. SecondFixConst
  122. *****************************************************************************}
  123. procedure secondfixconst(var p : ptree);
  124. begin
  125. { an fix comma const. behaves as a memory reference }
  126. p^.location.loc:=LOC_MEM;
  127. p^.location.reference.is_immediate:=true;
  128. p^.location.reference.offset:=p^.value_fix;
  129. end;
  130. {*****************************************************************************
  131. SecondOrdConst
  132. *****************************************************************************}
  133. procedure secondordconst(var p : ptree);
  134. begin
  135. { an integer const. behaves as a memory reference }
  136. p^.location.loc:=LOC_MEM;
  137. p^.location.reference.is_immediate:=true;
  138. p^.location.reference.offset:=p^.value;
  139. end;
  140. {*****************************************************************************
  141. SecondStringConst
  142. *****************************************************************************}
  143. procedure secondstringconst(var p : ptree);
  144. var
  145. hp1 : pai;
  146. l1,l2,
  147. lastlabel : pasmlabel;
  148. pc : pchar;
  149. same_string : boolean;
  150. l,j,
  151. i,mylength : longint;
  152. begin
  153. lastlabel:=nil;
  154. { const already used ? }
  155. if not assigned(p^.lab_str) then
  156. begin
  157. if is_shortstring(p^.resulttype) then
  158. mylength:=p^.length+2
  159. else
  160. mylength:=p^.length+1;
  161. { tries to found an old entry }
  162. hp1:=pai(consts^.first);
  163. while assigned(hp1) do
  164. begin
  165. if hp1^.typ=ait_label then
  166. lastlabel:=pai_label(hp1)^.l
  167. else
  168. begin
  169. { when changing that code, be careful that }
  170. { you don't use typed consts, which are }
  171. { are also written to consts }
  172. { currently, this is no problem, because }
  173. { typed consts have no leading length or }
  174. { they have no trailing zero }
  175. if (hp1^.typ=ait_string) and (lastlabel<>nil) and
  176. (pai_string(hp1)^.len=mylength) then
  177. begin
  178. same_string:=true;
  179. { if shortstring then check the length byte first and
  180. set the start index to 1 }
  181. if is_shortstring(p^.resulttype) then
  182. begin
  183. if p^.length<>ord(pai_string(hp1)^.str[0]) then
  184. same_string:=false;
  185. j:=1;
  186. end
  187. else
  188. j:=0;
  189. { don't check if the length byte was already wrong }
  190. if same_string then
  191. begin
  192. for i:=0 to p^.length do
  193. begin
  194. if pai_string(hp1)^.str[j]<>p^.value_str[i] then
  195. begin
  196. same_string:=false;
  197. break;
  198. end;
  199. inc(j);
  200. end;
  201. end;
  202. { found ? }
  203. if same_string then
  204. begin
  205. p^.lab_str:=lastlabel;
  206. { create a new entry for ansistrings, but reuse the data }
  207. if (p^.stringtype in [st_ansistring,st_widestring]) then
  208. begin
  209. getdatalabel(l2);
  210. consts^.concat(new(pai_label,init(l2)));
  211. consts^.concat(new(pai_const_symbol,init(p^.lab_str)));
  212. { return the offset of the real string }
  213. p^.lab_str:=l2;
  214. end;
  215. break;
  216. end;
  217. end;
  218. lastlabel:=nil;
  219. end;
  220. hp1:=pai(hp1^.next);
  221. end;
  222. { :-(, we must generate a new entry }
  223. if not assigned(p^.lab_str) then
  224. begin
  225. getdatalabel(lastlabel);
  226. p^.lab_str:=lastlabel;
  227. if (cs_smartlink in aktmoduleswitches) then
  228. consts^.concat(new(pai_cut,init));
  229. consts^.concat(new(pai_label,init(lastlabel)));
  230. { generate an ansi string ? }
  231. case p^.stringtype of
  232. st_ansistring:
  233. begin
  234. { an empty ansi string is nil! }
  235. if p^.length=0 then
  236. consts^.concat(new(pai_const,init_32bit(0)))
  237. else
  238. begin
  239. getdatalabel(l1);
  240. getdatalabel(l2);
  241. consts^.concat(new(pai_label,init(l2)));
  242. consts^.concat(new(pai_const_symbol,init(l1)));
  243. consts^.concat(new(pai_const,init_32bit(p^.length)));
  244. consts^.concat(new(pai_const,init_32bit(p^.length)));
  245. consts^.concat(new(pai_const,init_32bit(-1)));
  246. consts^.concat(new(pai_label,init(l1)));
  247. getmem(pc,p^.length+2);
  248. move(p^.value_str^,pc^,p^.length);
  249. pc[p^.length]:=#0;
  250. { to overcome this problem we set the length explicitly }
  251. { with the ending null char }
  252. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+1)));
  253. { return the offset of the real string }
  254. p^.lab_str:=l2;
  255. end;
  256. end;
  257. st_shortstring:
  258. begin
  259. { truncate strings larger than 255 chars }
  260. if p^.length>255 then
  261. l:=255
  262. else
  263. l:=p^.length;
  264. { also length and terminating zero }
  265. getmem(pc,l+3);
  266. move(p^.value_str^,pc[1],l+1);
  267. pc[0]:=chr(l);
  268. { to overcome this problem we set the length explicitly }
  269. { with the ending null char }
  270. pc[l+1]:=#0;
  271. consts^.concat(new(pai_string,init_length_pchar(pc,l+2)));
  272. end;
  273. end;
  274. end;
  275. end;
  276. reset_reference(p^.location.reference);
  277. p^.location.reference.symbol:=p^.lab_str;
  278. p^.location.loc:=LOC_MEM;
  279. end;
  280. {*****************************************************************************
  281. SecondSetCons
  282. *****************************************************************************}
  283. procedure secondsetconst(var p : ptree);
  284. var
  285. hp1 : pai;
  286. lastlabel : pasmlabel;
  287. i : longint;
  288. neededtyp : tait;
  289. begin
  290. { small sets are loaded as constants }
  291. if psetdef(p^.resulttype)^.settype=smallset then
  292. begin
  293. p^.location.loc:=LOC_MEM;
  294. p^.location.reference.is_immediate:=true;
  295. p^.location.reference.offset:=plongint(p^.value_set)^;
  296. exit;
  297. end;
  298. if psetdef(p^.resulttype)^.settype=smallset then
  299. neededtyp:=ait_const_32bit
  300. else
  301. neededtyp:=ait_const_8bit;
  302. lastlabel:=nil;
  303. { const already used ? }
  304. if not assigned(p^.lab_set) then
  305. begin
  306. { tries to found an old entry }
  307. hp1:=pai(consts^.first);
  308. while assigned(hp1) do
  309. begin
  310. if hp1^.typ=ait_label then
  311. lastlabel:=pai_label(hp1)^.l
  312. else
  313. begin
  314. if (lastlabel<>nil) and (hp1^.typ=neededtyp) then
  315. begin
  316. if (hp1^.typ=ait_const_8bit) then
  317. begin
  318. { compare normal set }
  319. i:=0;
  320. while assigned(hp1) and (i<32) do
  321. begin
  322. if pai_const(hp1)^.value<>p^.value_set^[i] then
  323. break;
  324. inc(i);
  325. hp1:=pai(hp1^.next);
  326. end;
  327. if i=32 then
  328. begin
  329. { found! }
  330. p^.lab_set:=lastlabel;
  331. break;
  332. end;
  333. { leave when the end of consts is reached, so no
  334. hp1^.next is done }
  335. if not assigned(hp1) then
  336. break;
  337. end
  338. else
  339. begin
  340. { compare small set }
  341. if plongint(p^.value_set)^=pai_const(hp1)^.value then
  342. begin
  343. { found! }
  344. p^.lab_set:=lastlabel;
  345. break;
  346. end;
  347. end;
  348. end;
  349. lastlabel:=nil;
  350. end;
  351. hp1:=pai(hp1^.next);
  352. end;
  353. { :-(, we must generate a new entry }
  354. if not assigned(p^.lab_set) then
  355. begin
  356. getdatalabel(lastlabel);
  357. p^.lab_set:=lastlabel;
  358. if (cs_smartlink in aktmoduleswitches) then
  359. consts^.concat(new(pai_cut,init));
  360. consts^.concat(new(pai_label,init(lastlabel)));
  361. if psetdef(p^.resulttype)^.settype=smallset then
  362. begin
  363. move(p^.value_set^,i,sizeof(longint));
  364. consts^.concat(new(pai_const,init_32bit(i)));
  365. end
  366. else
  367. begin
  368. for i:=0 to 31 do
  369. consts^.concat(new(pai_const,init_8bit(p^.value_set^[i])));
  370. end;
  371. end;
  372. end;
  373. reset_reference(p^.location.reference);
  374. p^.location.reference.symbol:=p^.lab_set;
  375. p^.location.loc:=LOC_MEM;
  376. end;
  377. {*****************************************************************************
  378. SecondNilN
  379. *****************************************************************************}
  380. procedure secondniln(var p : ptree);
  381. begin
  382. p^.location.loc:=LOC_MEM;
  383. p^.location.reference.is_immediate:=true;
  384. p^.location.reference.offset:=0;
  385. end;
  386. end.
  387. {
  388. $Log$
  389. Revision 1.40 1999-09-04 20:53:06 florian
  390. * bug 580 fixed
  391. Revision 1.39 1999/08/04 00:22:45 florian
  392. * renamed i386asm and i386base to cpuasm and cpubase
  393. Revision 1.38 1999/08/03 22:02:38 peter
  394. * moved bitmask constants to sets
  395. * some other type/const renamings
  396. Revision 1.37 1999/07/05 20:13:08 peter
  397. * removed temp defines
  398. Revision 1.36 1999/05/27 19:44:10 peter
  399. * removed oldasm
  400. * plabel -> pasmlabel
  401. * -a switches to source writing automaticly
  402. * assembler readers OOPed
  403. * asmsymbol automaticly external
  404. * jumptables and other label fixes for asm readers
  405. Revision 1.35 1999/05/21 13:54:47 peter
  406. * NEWLAB for label as symbol
  407. Revision 1.34 1999/05/12 00:19:41 peter
  408. * removed R_DEFAULT_SEG
  409. * uniform float names
  410. Revision 1.33 1999/05/06 09:05:12 peter
  411. * generic write_float and str_float
  412. * fixed constant float conversions
  413. Revision 1.32 1999/05/01 13:24:06 peter
  414. * merged nasm compiler
  415. * old asm moved to oldasm/
  416. Revision 1.31 1999/04/07 15:16:43 pierre
  417. * zero length string were generated multiple times
  418. Revision 1.30 1999/03/31 13:51:49 peter
  419. * shortstring reuse fixed
  420. Revision 1.29 1999/02/25 21:02:25 peter
  421. * ag386bin updates
  422. + coff writer
  423. Revision 1.28 1999/02/22 02:15:08 peter
  424. * updates for ag386bin
  425. Revision 1.27 1999/01/19 14:21:59 peter
  426. * shortstring truncated after 255 chars
  427. Revision 1.26 1998/12/11 00:02:49 peter
  428. + globtype,tokens,version unit splitted from globals
  429. Revision 1.25 1998/12/10 14:39:30 florian
  430. * bug with p(const a : ansistring) fixed
  431. * duplicate constant ansistrings were handled wrong, fixed
  432. Revision 1.24 1998/11/28 15:36:02 michael
  433. Fixed generation of constant ansistrings
  434. Revision 1.23 1998/11/26 14:39:12 peter
  435. * ansistring -> pchar fixed
  436. * ansistring constants fixed
  437. * ansistring constants are now written once
  438. Revision 1.22 1998/11/24 13:40:59 peter
  439. * release smallsetord, so small sets constant are handled like longints
  440. Revision 1.21 1998/11/24 12:52:41 peter
  441. * sets are not written twice anymore
  442. * optimize for emptyset+single element which uses a new routine from
  443. set.inc FPC_SET_CREATE_ELEMENT
  444. Revision 1.20 1998/11/16 12:11:29 peter
  445. * fixed ansistring crash
  446. Revision 1.19 1998/11/05 23:40:45 pierre
  447. * fix for const strings
  448. Revision 1.18 1998/11/05 15:26:38 pierre
  449. * fix for missing zero after string const
  450. Revision 1.17 1998/11/05 12:02:32 peter
  451. * released useansistring
  452. * removed -Sv, its now available in fpc modes
  453. Revision 1.16 1998/11/04 21:07:43 michael
  454. * undid peters change. Constant ansistrings should end on null too cd ..
  455. Revision 1.15 1998/11/04 10:11:36 peter
  456. * ansistring fixes
  457. Revision 1.14 1998/09/17 09:42:13 peter
  458. + pass_2 for cg386
  459. * Message() -> CGMessage() for pass_1/pass_2
  460. Revision 1.13 1998/09/07 18:45:53 peter
  461. * update smartlinking, uses getdatalabel
  462. * renamed ptree.value vars to value_str,value_real,value_set
  463. Revision 1.12 1998/08/28 10:56:57 peter
  464. * removed warnings
  465. Revision 1.11 1998/08/14 18:18:39 peter
  466. + dynamic set contruction
  467. * smallsets are now working (always longint size)
  468. Revision 1.10 1998/08/04 13:22:46 pierre
  469. * weird bug fixed :
  470. a pchar ' ' (simple space or any other letter) was found to
  471. be equal to a string of length zero !!!
  472. thus printing out non sense
  473. found that out while checking Control-C !!
  474. + added column info also in RHIDE format as
  475. it might be usefull later
  476. Revision 1.9 1998/07/20 18:40:10 florian
  477. * handling of ansi string constants should now work
  478. Revision 1.8 1998/07/20 10:23:00 florian
  479. * better ansi string assignement
  480. Revision 1.7 1998/07/18 22:54:25 florian
  481. * some ansi/wide/longstring support fixed:
  482. o parameter passing
  483. o returning as result from functions
  484. Revision 1.6 1998/07/18 17:11:07 florian
  485. + ansi string constants fixed
  486. + switch $H partial implemented
  487. Revision 1.5 1998/06/25 08:48:07 florian
  488. * first version of rtti support
  489. Revision 1.4 1998/06/08 13:13:31 pierre
  490. + temporary variables now in temp_gen.pas unit
  491. because it is processor independent
  492. * mppc68k.bat modified to undefine i386 and support_mmx
  493. (which are defaults for i386)
  494. Revision 1.3 1998/06/05 17:44:11 peter
  495. * splitted cgi386
  496. Revision 1.2 1998/06/05 16:13:31 pierre
  497. * fix for real and string consts inside inlined procs
  498. Revision 1.1 1998/05/23 01:21:02 peter
  499. + aktasmmode, aktoptprocessor, aktoutputformat
  500. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  501. + $LIBNAME to set the library name where the unit will be put in
  502. * splitted cgi386 a bit (codeseg to large for bp7)
  503. * nasm, tasm works again. nasm moved to ag386nsm.pas
  504. }