cg386con.pas 21 KB

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