cg68k2.pas 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl, Carl Eric Codere
  4. This unit generates 68000 (or better) assembler from the parse tree
  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 Cg68k2;
  22. Interface
  23. uses
  24. objects,verbose,cobjects,systems,globals,tree,
  25. symtable,types,strings,pass_1,hcodegen,temp_gen,
  26. aasm,m68k,tgen68k,files,cga68k;
  27. const
  28. { process condition codes bit definitions }
  29. CARRY_FLAG = $01;
  30. OVFL_FLAG = $02;
  31. ZERO_FLAG = $04;
  32. NEG_FLAG = $08;
  33. { to set OR with flags }
  34. { to clear AND (NOT flag) }
  35. procedure secondadd(var p : ptree);
  36. procedure processcc(p: ptree);
  37. procedure secondfor(var p : ptree);
  38. procedure secondas(var p : ptree);
  39. procedure secondraise(var p : ptree);
  40. procedure secondin(var p : ptree);
  41. procedure secondexpr(var p : ptree);
  42. procedure secondblockn(var p : ptree);
  43. procedure second_while_repeatn(var p : ptree);
  44. procedure secondifn(var p : ptree);
  45. procedure secondbreakn(var p : ptree);
  46. { copies p a set element into the d0.b register }
  47. procedure loadsetelement(var p : ptree);
  48. Implementation
  49. uses cg68k;
  50. procedure secondadd(var p : ptree);
  51. { is also being used for xor, and "mul", "sub, or and comparative }
  52. { operators }
  53. label do_normal;
  54. var
  55. swapp : ptree;
  56. hregister : tregister;
  57. pushed,mboverflow,cmpop : boolean;
  58. op : tasmop;
  59. pushedregs : tpushed;
  60. flags : tresflags;
  61. otl,ofl : plabel;
  62. power : longint;
  63. href : treference;
  64. opsize : topsize;
  65. swapl : tlocation;
  66. tmpref: treference;
  67. { true, if unsigned types are compared }
  68. unsigned : boolean;
  69. { is_in_dest if the result is put directly into }
  70. { the resulting refernce or varregister }
  71. { true, if a small set is handled with the longint code }
  72. is_set : boolean;
  73. is_in_dest : boolean;
  74. { true, if for sets subtractions the extra not should generated }
  75. extra_not : boolean;
  76. begin
  77. unsigned:=false;
  78. is_in_dest := false;
  79. extra_not:=false;
  80. opsize:=S_L;
  81. { calculate the operator which is more difficult }
  82. firstcomplex(p);
  83. { handling boolean expressions extra: }
  84. if ((p^.left^.resulttype^.deftype=orddef) and
  85. (porddef(p^.left^.resulttype)^.typ=bool8bit)) then
  86. { ((p^.right^.resulttype^.deftype=orddef) and
  87. (porddef(p^.right^.resulttype)^.typ=bool8bit)) then }
  88. begin
  89. if (p^.treetype=andn) or (p^.treetype=orn) then
  90. begin
  91. p^.location.loc:=LOC_JUMP;
  92. cmpop:=false;
  93. case p^.treetype of
  94. andn : begin
  95. otl:=truelabel;
  96. getlabel(truelabel);
  97. secondpass(p^.left);
  98. maketojumpbool(p^.left);
  99. emitl(A_LABEL,truelabel);
  100. truelabel:=otl;
  101. end;
  102. orn : begin
  103. ofl:=falselabel;
  104. getlabel(falselabel);
  105. secondpass(p^.left);
  106. maketojumpbool(p^.left);
  107. emitl(A_LABEL,falselabel);
  108. falselabel:=ofl;
  109. end;
  110. else Message(sym_e_type_mismatch);
  111. end; { end case }
  112. secondpass(p^.right);
  113. maketojumpbool(p^.right);
  114. end { endif }
  115. else if p^.treetype in [unequaln,equaln,xorn] then
  116. begin
  117. opsize:=S_B;
  118. if p^.left^.treetype=ordconstn then
  119. begin
  120. swapp:=p^.right;
  121. p^.right:=p^.left;
  122. p^.left:=swapp;
  123. p^.swaped:=not(p^.swaped);
  124. end;
  125. secondpass(p^.left);
  126. p^.location:=p^.left^.location;
  127. (* register needed *)
  128. pushed:=maybe_push(p^.right^.registers32,p);
  129. secondpass(p^.right);
  130. if pushed then restore(p);
  131. goto do_normal;
  132. end { endif }
  133. else Message(sym_e_type_mismatch);
  134. end { endif }
  135. { also handle string operations seperately }
  136. else if (p^.left^.resulttype^.deftype=stringdef) then
  137. begin
  138. { string operations are not commutative }
  139. if p^.swaped then
  140. begin
  141. swapp:=p^.left;
  142. p^.left:=p^.right;
  143. p^.right:=swapp;
  144. { because of jump being produced at comparison below: }
  145. p^.swaped:=not(p^.swaped);
  146. end;
  147. case p^.treetype of
  148. addn : begin
  149. cmpop:=false;
  150. secondpass(p^.left);
  151. if (p^.left^.treetype<>addn) then
  152. begin
  153. { can only reference be }
  154. { string in register would be funny }
  155. { therefore produce a temporary string }
  156. { release the registers }
  157. del_reference(p^.left^.location.reference);
  158. gettempofsizereference(256,href);
  159. copystring(href,p^.left^.location.reference,255);
  160. ungetiftemp(p^.left^.location.reference);
  161. { does not hurt: }
  162. p^.left^.location.loc:=LOC_MEM;
  163. p^.left^.location.reference:=href;
  164. end;
  165. secondpass(p^.right);
  166. { on the right we do not need the register anymore too }
  167. del_reference(p^.right^.location.reference);
  168. pushusedregisters(pushedregs,$ffff);
  169. { WE INVERSE THE PARAMETERS!!! }
  170. { Because parameters are inversed in the rtl }
  171. emitpushreferenceaddr(p^.right^.location.reference);
  172. emitpushreferenceaddr(p^.left^.location.reference);
  173. emitcall('STRCONCAT',true);
  174. maybe_loadA5;
  175. popusedregisters(pushedregs);
  176. set_location(p^.location,p^.left^.location);
  177. ungetiftemp(p^.right^.location.reference);
  178. end; { this case }
  179. ltn,lten,gtn,gten,
  180. equaln,unequaln :
  181. begin
  182. secondpass(p^.left);
  183. { are too few registers free? }
  184. pushed:=maybe_push(p^.right^.registers32,p);
  185. secondpass(p^.right);
  186. if pushed then restore(p);
  187. cmpop:=true;
  188. del_reference(p^.right^.location.reference);
  189. del_reference(p^.left^.location.reference);
  190. { generates better code }
  191. { s='' and s<>'' }
  192. if (p^.treetype in [equaln,unequaln]) and
  193. (
  194. ((p^.left^.treetype=stringconstn) and
  195. (p^.left^.values^='')) or
  196. ((p^.right^.treetype=stringconstn) and
  197. (p^.right^.values^=''))
  198. ) then
  199. begin
  200. { only one node can be stringconstn }
  201. { else pass 1 would have evaluted }
  202. { this node }
  203. if p^.left^.treetype=stringconstn then
  204. exprasmlist^.concat(new(pai68k,op_ref(
  205. A_TST,S_B,newreference(p^.right^.location.reference))))
  206. else
  207. exprasmlist^.concat(new(pai68k,op_ref(
  208. A_TST,S_B,newreference(p^.left^.location.reference))));
  209. end
  210. else
  211. begin
  212. pushusedregisters(pushedregs,$ffff);
  213. { parameters are directly passed via registers }
  214. { this has several advantages, no loss of the flags }
  215. { on exit ,and MUCH faster on m68k machines }
  216. { speed difference (68000) }
  217. { normal routine: entry, exit code + push = 124 }
  218. { (best case) }
  219. { assembler routine: param setup (worst case) = 48 }
  220. exprasmlist^.concat(new(pai68k,op_ref_reg(
  221. A_LEA,S_L,newreference(p^.left^.location.reference),R_A0)));
  222. exprasmlist^.concat(new(pai68k,op_ref_reg(
  223. A_LEA,S_L,newreference(p^.right^.location.reference),R_A1)));
  224. {
  225. emitpushreferenceaddr(p^.left^.location.reference);
  226. emitpushreferenceaddr(p^.right^.location.reference); }
  227. emitcall('STRCMP',true);
  228. maybe_loada5;
  229. popusedregisters(pushedregs);
  230. end;
  231. ungetiftemp(p^.left^.location.reference);
  232. ungetiftemp(p^.right^.location.reference);
  233. end; { end this case }
  234. else Message(sym_e_type_mismatch);
  235. end; { end case }
  236. end { end else if }
  237. else
  238. begin
  239. { in case of constant put it to the left }
  240. if p^.left^.treetype=ordconstn then
  241. begin
  242. swapp:=p^.right;
  243. p^.right:=p^.left;
  244. p^.left:=swapp;
  245. p^.swaped:=not(p^.swaped);
  246. end;
  247. secondpass(p^.left);
  248. set_location(p^.location,p^.left^.location);
  249. { are to few registers free? }
  250. pushed:=maybe_push(p^.right^.registers32,p);
  251. secondpass(p^.right);
  252. if pushed then restore(p);
  253. if (p^.left^.resulttype^.deftype=pointerdef) or
  254. (p^.right^.resulttype^.deftype=pointerdef) or
  255. ((p^.right^.resulttype^.deftype=objectdef) and
  256. pobjectdef(p^.right^.resulttype)^.isclass and
  257. (p^.left^.resulttype^.deftype=objectdef) and
  258. pobjectdef(p^.left^.resulttype)^.isclass
  259. ) or
  260. (p^.left^.resulttype^.deftype=classrefdef) or
  261. (p^.left^.resulttype^.deftype=procvardef) or
  262. (p^.left^.resulttype^.deftype=enumdef) or
  263. ((p^.left^.resulttype^.deftype=orddef) and
  264. (porddef(p^.left^.resulttype)^.typ=s32bit)) or
  265. ((p^.right^.resulttype^.deftype=orddef) and
  266. (porddef(p^.right^.resulttype)^.typ=s32bit)) or
  267. ((p^.left^.resulttype^.deftype=orddef) and
  268. (porddef(p^.left^.resulttype)^.typ=u32bit)) or
  269. ((p^.right^.resulttype^.deftype=orddef) and
  270. (porddef(p^.right^.resulttype)^.typ=u32bit))
  271. { SMALL SETS DO NOT WORK BECAUSE OF ENDIAN! }
  272. or { as well as small sets }
  273. ((p^.left^.resulttype^.deftype=setdef) and
  274. (psetdef(p^.left^.resulttype)^.settype=smallset))
  275. then
  276. begin
  277. do_normal:
  278. mboverflow:=false;
  279. cmpop:=false;
  280. if (p^.left^.resulttype^.deftype=pointerdef) or
  281. (p^.right^.resulttype^.deftype=pointerdef) or
  282. ((p^.left^.resulttype^.deftype=orddef) and
  283. (porddef(p^.left^.resulttype)^.typ=u32bit)) or
  284. ((p^.right^.resulttype^.deftype=orddef) and
  285. (porddef(p^.right^.resulttype)^.typ=u32bit)) then
  286. unsigned:=true;
  287. is_set:=p^.resulttype^.deftype=setdef;
  288. case p^.treetype of
  289. addn : begin
  290. if is_set then
  291. begin
  292. op:=A_OR;
  293. mboverflow:=false;
  294. unsigned:=false;
  295. end
  296. else
  297. begin
  298. op:=A_ADD;
  299. mboverflow:=true;
  300. end;
  301. end; { end this case }
  302. symdifn : begin
  303. { the symetric diff is only for sets }
  304. if is_set then
  305. begin
  306. op:=A_EOR;
  307. mboverflow:=false;
  308. unsigned:=false;
  309. end
  310. else
  311. begin
  312. Message(sym_e_type_mismatch);
  313. end;
  314. end;
  315. muln : begin
  316. if is_set then
  317. begin
  318. op:=A_AND;
  319. mboverflow:=false;
  320. unsigned:=false;
  321. end
  322. else
  323. begin
  324. if unsigned then
  325. op:=A_MULU
  326. else
  327. op:=A_MULS;
  328. mboverflow:=true;
  329. end;
  330. end; { end this case }
  331. subn : begin
  332. if is_set then
  333. begin
  334. op:=A_AND;
  335. mboverflow:=false;
  336. unsigned:=false;
  337. extra_not:=true;
  338. end
  339. else
  340. begin
  341. op:=A_SUB;
  342. mboverflow:=true;
  343. end;
  344. end; {end this case }
  345. ltn,lten,gtn,gten,
  346. equaln,unequaln :
  347. begin
  348. op:=A_CMP;
  349. cmpop:=true;
  350. end;
  351. xorn : op:=A_EOR;
  352. orn : op:=A_OR;
  353. andn : op:=A_AND;
  354. else Message(sym_e_type_mismatch);
  355. end; {end case }
  356. { left and right no register? }
  357. { then one must be demanded }
  358. if (p^.left^.location.loc<>LOC_REGISTER) and
  359. (p^.right^.location.loc<>LOC_REGISTER) then
  360. begin
  361. { register variable ? }
  362. if (p^.left^.location.loc=LOC_CREGISTER) then
  363. begin
  364. { it is OK if this is the destination }
  365. if is_in_dest then
  366. begin
  367. hregister:=p^.location.register;
  368. emit_reg_reg(A_MOVE,opsize,p^.left^.location.register,
  369. hregister);
  370. end
  371. else
  372. if cmpop then
  373. begin
  374. { do not disturb the register }
  375. hregister:=p^.location.register;
  376. end
  377. else
  378. begin
  379. hregister := getregister32;
  380. emit_reg_reg(A_MOVE,opsize,p^.left^.location.register,
  381. hregister);
  382. end
  383. end
  384. else
  385. begin
  386. del_reference(p^.left^.location.reference);
  387. if is_in_dest then
  388. begin
  389. hregister:=p^.location.register;
  390. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,opsize,
  391. newreference(p^.left^.location.reference),hregister)));
  392. end
  393. else
  394. begin
  395. { first give free, then demand new register }
  396. hregister := getregister32;
  397. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,opsize,
  398. newreference(p^.left^.location.reference),
  399. hregister)));
  400. end;{ endif p^... }
  401. end;
  402. p^.location.loc:=LOC_REGISTER;
  403. p^.location.register:=hregister;
  404. end
  405. else
  406. { if on the right the register then swap }
  407. if (p^.right^.location.loc=LOC_REGISTER) then
  408. begin
  409. swap_location(p^.location,p^.right^.location);
  410. { newly swapped also set swapped flag }
  411. p^.swaped:=not(p^.swaped);
  412. end;
  413. { endif p^...<> LOC_REGISTER }
  414. { at this point, p^.location.loc should be LOC_REGISTER }
  415. { and p^.location.register should be a valid register }
  416. { containing the left result }
  417. if p^.right^.location.loc<>LOC_REGISTER then
  418. begin
  419. if (p^.treetype=subn) and p^.swaped then
  420. begin
  421. if p^.right^.location.loc=LOC_CREGISTER then
  422. begin
  423. if extra_not then
  424. exprasmlist^.concat(new(pai68k,op_reg(A_NOT,opsize,p^.location.register)));
  425. emit_reg_reg(A_MOVE,opsize,p^.right^.location.register,R_D6);
  426. emit_reg_reg(op,opsize,p^.location.register,R_D6);
  427. emit_reg_reg(A_MOVE,opsize,R_D6,p^.location.register);
  428. end
  429. else
  430. begin
  431. if extra_not then
  432. exprasmlist^.concat(new(pai68k,op_reg(A_NOT,opsize,p^.location.register)));
  433. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,opsize,
  434. newreference(p^.right^.location.reference),R_D6)));
  435. exprasmlist^.concat(new(pai68k,op_reg_reg(op,opsize,p^.location.register,R_D6)));
  436. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,opsize,R_D6,p^.location.register)));
  437. del_reference(p^.right^.location.reference);
  438. end;
  439. end
  440. { end subn ... }
  441. else
  442. begin
  443. if (p^.right^.treetype=ordconstn) and (op=A_CMP) and
  444. (p^.right^.value=0) then
  445. exprasmlist^.concat(new(pai68k,op_reg(A_TST,opsize,p^.location.register)))
  446. else
  447. if (p^.right^.treetype=ordconstn) and (op=A_MULS) and
  448. (ispowerof2(p^.right^.value,power)) then
  449. begin
  450. if (power <= 8) then
  451. exprasmlist^.concat(new(pai68k,op_const_reg(A_ASL,opsize,power,
  452. p^.location.register)))
  453. else
  454. begin
  455. exprasmlist^.concat(new(pai68k,op_const_reg(A_MOVE,S_L,power,
  456. R_D6)));
  457. exprasmlist^.concat(new(pai68k,op_reg_reg(A_ASL,opsize,R_D6,
  458. p^.location.register)))
  459. end;
  460. end
  461. else
  462. begin
  463. if (p^.right^.location.loc=LOC_CREGISTER) then
  464. begin
  465. if extra_not then
  466. begin
  467. emit_reg_reg(A_MOVE,S_L,p^.right^.location.register,R_D6);
  468. exprasmlist^.concat(new(pai68k,op_reg(A_NOT,S_L,R_D6)));
  469. emit_reg_reg(A_AND,S_L,R_D6,
  470. p^.location.register);
  471. end
  472. else
  473. begin
  474. if (op=A_MULS) and (opsize = S_L) and (aktoptprocessor=MC68000) then
  475. { Emulation for MC68000 }
  476. begin
  477. emit_reg_reg(A_MOVE,opsize,p^.right^.location.register,
  478. R_D0);
  479. emit_reg_reg(A_MOVE,opsize,p^.location.register,R_D1);
  480. emitcall('LONGMUL',true);
  481. emit_reg_reg(A_MOVE,opsize,R_D0,p^.location.register);
  482. end
  483. else
  484. if (op=A_MULU) and (opsize = S_L) and (aktoptprocessor=MC68000) then
  485. Message(cg_f_32bit_not_supported_in_68000)
  486. else
  487. emit_reg_reg(op,opsize,p^.right^.location.register,
  488. p^.location.register);
  489. end;
  490. end
  491. else
  492. begin
  493. if extra_not then
  494. begin
  495. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,newreference(
  496. p^.right^.location.reference),R_D6)));
  497. exprasmlist^.concat(new(pai68k,op_reg(A_NOT,S_L,R_D6)));
  498. emit_reg_reg(A_AND,S_L,R_D6,
  499. p^.location.register);
  500. end
  501. else
  502. begin
  503. if (op=A_MULS) and (opsize = S_L) and (aktoptprocessor=MC68000) then
  504. { Emulation for MC68000 }
  505. begin
  506. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE, opsize,
  507. newreference(p^.right^.location.reference),R_D1)));
  508. emit_reg_reg(A_MOVE,opsize,p^.location.register,R_D0);
  509. emitcall('LONGMUL',true);
  510. emit_reg_reg(A_MOVE,opsize,R_D0,p^.location.register);
  511. end
  512. else
  513. if (op=A_MULU) and (opsize = S_L) and (aktoptprocessor=MC68000) then
  514. Message(cg_f_32bit_not_supported_in_68000)
  515. else
  516. { When one of the source/destination is a memory reference }
  517. { and the operator is EOR, the we must load it into the }
  518. { value into a register first since only EOR reg,reg exists }
  519. { on the m68k }
  520. if (op=A_EOR) then
  521. begin
  522. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,opsize,newreference(
  523. p^.right^.location.reference),R_D0)));
  524. exprasmlist^.concat(new(pai68k,op_reg_reg(op,opsize,R_D0,
  525. p^.location.register)));
  526. end
  527. else
  528. exprasmlist^.concat(new(pai68k,op_ref_reg(op,opsize,newreference(
  529. p^.right^.location.reference),p^.location.register)));
  530. end;
  531. del_reference(p^.right^.location.reference);
  532. end;
  533. end;
  534. end;
  535. end
  536. else
  537. begin
  538. { when swapped another result register }
  539. if (p^.treetype=subn) and p^.swaped then
  540. begin
  541. if extra_not then
  542. exprasmlist^.concat(new(pai68k,op_reg(A_NOT,S_L,p^.location.register)));
  543. exprasmlist^.concat(new(pai68k,op_reg_reg(op,opsize,
  544. p^.location.register,p^.right^.location.register)));
  545. swap_location(p^.location,p^.right^.location);
  546. { newly swapped also set swapped flag }
  547. { just to maintain ordering }
  548. p^.swaped:=not(p^.swaped);
  549. end
  550. else
  551. begin
  552. if extra_not then
  553. exprasmlist^.concat(new(pai68k,op_reg(A_NOT,S_L,p^.right^.location.register)));
  554. if (op=A_MULS) and (opsize = S_L) and (aktoptprocessor=MC68000) then
  555. { Emulation for MC68000 }
  556. begin
  557. emit_reg_reg(A_MOVE,opsize,p^.right^.location.register,
  558. R_D0);
  559. emit_reg_reg(A_MOVE,opsize,p^.location.register,R_D1);
  560. emitcall('LONGMUL',true);
  561. emit_reg_reg(A_MOVE,opsize,R_D0,p^.location.register);
  562. end
  563. else
  564. if (op=A_MULU) and (opsize = S_L) and (aktoptprocessor=MC68000) then
  565. Message(cg_f_32bit_not_supported_in_68000)
  566. else
  567. exprasmlist^.concat(new(pai68k,op_reg_reg(op,opsize,
  568. p^.right^.location.register,
  569. p^.location.register)));
  570. end;
  571. ungetregister32(p^.right^.location.register);
  572. end;
  573. if cmpop then
  574. ungetregister32(p^.location.register);
  575. { only in case of overflow operations }
  576. { produce overflow code }
  577. if mboverflow then
  578. emitoverflowcheck(p);
  579. end
  580. {*********************************************************************}
  581. else if ((p^.left^.resulttype^.deftype=orddef) and
  582. (porddef(p^.left^.resulttype)^.typ=uchar)) then
  583. begin
  584. case p^.treetype of
  585. ltn,lten,gtn,gten,
  586. equaln,unequaln :
  587. cmpop:=true;
  588. else Message(sym_e_type_mismatch);
  589. end;
  590. unsigned:=true;
  591. { left and right no register? }
  592. { the one must be demanded }
  593. if (p^.location.loc<>LOC_REGISTER) and
  594. (p^.right^.location.loc<>LOC_REGISTER) then
  595. begin
  596. if p^.location.loc=LOC_CREGISTER then
  597. begin
  598. if cmpop then
  599. { do not disturb register }
  600. hregister:=p^.location.register
  601. else
  602. begin
  603. hregister:=getregister32;
  604. emit_reg_reg(A_MOVE,S_B,p^.location.register,
  605. hregister);
  606. end;
  607. end
  608. else
  609. begin
  610. del_reference(p^.location.reference);
  611. { first give free then demand new register }
  612. hregister:=getregister32;
  613. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_B,newreference(p^.location.reference),
  614. hregister)));
  615. end;
  616. p^.location.loc:=LOC_REGISTER;
  617. p^.location.register:=hregister;
  618. end;
  619. { now p always a register }
  620. if (p^.right^.location.loc=LOC_REGISTER) and
  621. (p^.location.loc<>LOC_REGISTER) then
  622. begin
  623. swap_location(p^.location,p^.right^.location);
  624. { newly swapped also set swapped flag }
  625. p^.swaped:=not(p^.swaped);
  626. end;
  627. if p^.right^.location.loc<>LOC_REGISTER then
  628. begin
  629. if p^.right^.location.loc=LOC_CREGISTER then
  630. begin
  631. emit_reg_reg(A_CMP,S_B,
  632. p^.right^.location.register,p^.location.register);
  633. end
  634. else
  635. begin
  636. exprasmlist^.concat(new(pai68k,op_ref_reg(A_CMP,S_B,newreference(
  637. p^.right^.location.reference),p^.location.register)));
  638. del_reference(p^.right^.location.reference);
  639. end;
  640. end
  641. else
  642. begin
  643. emit_reg_reg(A_CMP,S_B,p^.right^.location.register,
  644. p^.location.register);
  645. ungetregister32(p^.right^.location.register);
  646. end;
  647. ungetregister32(p^.location.register);
  648. end
  649. {*********************************************************************}
  650. else if (p^.left^.resulttype^.deftype=floatdef) and
  651. (pfloatdef(p^.left^.resulttype)^.typ<>f32bit) then
  652. begin
  653. { real constants to the left }
  654. if p^.left^.treetype=realconstn then
  655. begin
  656. swapp:=p^.right;
  657. p^.right:=p^.left;
  658. p^.left:=swapp;
  659. p^.swaped:=not(p^.swaped);
  660. end;
  661. cmpop:=false;
  662. case p^.treetype of
  663. addn : op:=A_FADD;
  664. muln : op:=A_FMUL;
  665. subn : op:=A_FSUB;
  666. slashn : op:=A_FDIV;
  667. ltn,lten,gtn,gten,
  668. equaln,unequaln : begin
  669. op:=A_FCMP;
  670. cmpop:=true;
  671. end;
  672. else Message(sym_e_type_mismatch);
  673. end;
  674. if (p^.left^.location.loc <> LOC_FPU) and
  675. (p^.right^.location.loc <> LOC_FPU) then
  676. begin
  677. { we suppose left in reference }
  678. del_reference(p^.left^.location.reference);
  679. { get a copy, since we don't want to modify the same }
  680. { node at the same time. }
  681. tmpref:=p^.left^.location.reference;
  682. if assigned(p^.left^.location.reference.symbol) then
  683. tmpref.symbol:=stringdup(p^.left^.location.reference.symbol^);
  684. floatload(pfloatdef(p^.left^.resulttype)^.typ, tmpref,
  685. p^.left^.location);
  686. clear_reference(tmpref);
  687. end
  688. else
  689. begin
  690. if (p^.right^.location.loc = LOC_FPU)
  691. and(p^.left^.location.loc <> LOC_FPU) then
  692. begin
  693. swap_location(p^.left^.location, p^.right^.location);
  694. p^.swaped := not(p^.swaped);
  695. end
  696. end;
  697. { ---------------- LEFT = LOC_FPUREG -------------------- }
  698. if ((p^.treetype =subn) or (p^.treetype = slashn)) and (p^.swaped) then
  699. { fpu_reg = right(FP1) / fpu_reg }
  700. { fpu_reg = right(FP1) - fpu_reg }
  701. begin
  702. if (cs_fp_emulation in aktmoduleswitches) then
  703. begin
  704. { fpu_reg = right / D1 }
  705. { fpu_reg = right - D1 }
  706. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,p^.left^.location.fpureg,R_D0)));
  707. { load value into D1 }
  708. if p^.right^.location.loc <> LOC_FPU then
  709. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,
  710. newreference(p^.right^.location.reference),R_D1)))
  711. else
  712. emit_reg_reg(A_MOVE,S_L,p^.right^.location.fpureg,R_D1);
  713. { probably a faster way to do this but... }
  714. case op of
  715. A_FADD: emitcall('SINGLE_ADD',true);
  716. A_FMUL: emitcall('SINGLE_MUL',true);
  717. A_FSUB: emitcall('SINGLE_SUB',true);
  718. A_FDIV: emitcall('SINGLE_DIV',true);
  719. A_FCMP: emitcall('SINGLE_CMP',true);
  720. end;
  721. if not cmpop then { only flags are affected with cmpop }
  722. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,R_D0,
  723. p^.left^.location.fpureg)));
  724. { if this was a reference, then delete as it }
  725. { it no longer required. }
  726. if p^.right^.location.loc <> LOC_FPU then
  727. del_reference(p^.right^.location.reference);
  728. end
  729. else
  730. begin
  731. if p^.right^.location.loc <> LOC_FPU then
  732. exprasmlist^.concat(new(pai68k,op_ref_reg(A_FMOVE,
  733. getfloatsize(pfloatdef(p^.left^.resulttype)^.typ),
  734. newreference(p^.right^.location.reference),
  735. R_FP1)))
  736. else
  737. { FPm --> FPn must use extended precision }
  738. emit_reg_reg(A_FMOVE,S_FX,p^.right^.location.fpureg,R_FP1);
  739. { arithmetic expression performed in extended mode }
  740. exprasmlist^.concat(new(pai68k,op_reg_reg(op,S_FX,
  741. p^.left^.location.fpureg,R_FP1)));
  742. { cmpop does not change any floating point register!! }
  743. if not cmpop then
  744. emit_reg_reg(A_FMOVE,S_FX,R_FP1,p^.left^.location.fpureg)
  745. { exprasmlist^.concat(new(pai68k,op_reg_reg(A_FMOVE,
  746. getfloatsize(pfloatdef(p^.left^.resulttype)^.typ),
  747. R_FP1,p^.left^.location.fpureg)))}
  748. else
  749. { process comparison, to make it compatible with the rest of the code }
  750. processcc(p);
  751. { if this was a reference, then delete as it }
  752. { it no longer required. }
  753. if p^.right^.location.loc <> LOC_FPU then
  754. del_reference(p^.right^.location.reference);
  755. end;
  756. end
  757. else { everything is in the right order }
  758. begin
  759. { fpu_reg = fpu_reg / right }
  760. { fpu_reg = fpu_reg - right }
  761. { + commutative ops }
  762. if cs_fp_emulation in aktmoduleswitches then
  763. begin
  764. { load value into D7 }
  765. if p^.right^.location.loc <> LOC_FPU then
  766. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,
  767. newreference(p^.right^.location.reference),R_D0)))
  768. else
  769. emit_reg_reg(A_MOVE,S_L,p^.right^.location.fpureg,R_D0);
  770. emit_reg_reg(A_MOVE,S_L,p^.left^.location.fpureg,R_D1);
  771. { probably a faster way to do this but... }
  772. case op of
  773. A_FADD: emitcall('SINGLE_ADD',true);
  774. A_FMUL: emitcall('SINGLE_MUL',true);
  775. A_FSUB: emitcall('SINGLE_SUB',true);
  776. A_FDIV: emitcall('SINGLE_DIV',true);
  777. A_FCMP: emitcall('SINGLE_CMP',true);
  778. end;
  779. if not cmpop then { only flags are affected with cmpop }
  780. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,R_D0,
  781. p^.left^.location.fpureg)));
  782. { if this was a reference, then delete as it }
  783. { it no longer required. }
  784. if p^.right^.location.loc <> LOC_FPU then
  785. del_reference(p^.right^.location.reference);
  786. end
  787. else
  788. begin
  789. if p^.right^.location.loc <> LOC_FPU then
  790. exprasmlist^.concat(new(pai68k,op_ref_reg(A_FMOVE,
  791. getfloatsize(pfloatdef(p^.left^.resulttype)^.typ),
  792. newreference(p^.right^.location.reference),R_FP1)))
  793. else
  794. emit_reg_reg(A_FMOVE,getfloatsize(pfloatdef(p^.left^.resulttype)^.typ),
  795. p^.right^.location.fpureg,R_FP1);
  796. emit_reg_reg(op,S_FX,R_FP1,p^.left^.location.fpureg);
  797. if cmpop then
  798. processcc(p);
  799. { if this was a reference, then delete as it }
  800. { it no longer required. }
  801. if p^.right^.location.loc <> LOC_FPU then
  802. del_reference(p^.right^.location.reference);
  803. end
  804. end; { endif treetype = .. }
  805. if cmpop then
  806. begin
  807. if p^.swaped then
  808. case p^.treetype of
  809. equaln: flags := F_E;
  810. unequaln: flags := F_NE;
  811. ltn : flags := F_G;
  812. lten : flags := F_GE;
  813. gtn : flags := F_L;
  814. gten: flags := F_LE;
  815. end
  816. else
  817. case p^.treetype of
  818. equaln: flags := F_E;
  819. unequaln : flags := F_NE;
  820. ltn: flags := F_L;
  821. lten : flags := F_LE;
  822. gtn : flags := F_G;
  823. gten: flags := F_GE;
  824. end;
  825. p^.location.loc := LOC_FLAGS;
  826. p^.location.resflags := flags;
  827. cmpop := false;
  828. end
  829. else
  830. begin
  831. p^.location.loc := LOC_FPU;
  832. if p^.left^.location.loc = LOC_FPU then
  833. { copy fpu register result . }
  834. { HERE ON EXIT FPU REGISTER IS IN EXTENDED MODE! }
  835. p^.location.fpureg := p^.left^.location.fpureg
  836. else
  837. begin
  838. InternalError(34);
  839. end;
  840. end;
  841. end
  842. {*********************************************************************}
  843. else if (p^.left^.resulttype^.deftype=setdef) then
  844. begin
  845. { not commutative }
  846. if p^.swaped then
  847. begin
  848. swapp:=p^.left;
  849. p^.left:=p^.right;
  850. p^.right:=swapp;
  851. { because of jump being produced by comparison }
  852. p^.swaped:=not(p^.swaped);
  853. end;
  854. case p^.treetype of
  855. equaln,unequaln : begin
  856. cmpop:=true;
  857. del_reference(p^.left^.location.reference);
  858. del_reference(p^.right^.location.reference);
  859. pushusedregisters(pushedregs,$ffff);
  860. { emitpushreferenceaddr(p^.right^.location.reference);
  861. emitpushreferenceaddr(p^.left^.location.reference);}
  862. exprasmlist^.concat(new(pai68k,op_ref_reg(A_LEA,S_L,
  863. newreference(p^.left^.location.reference),R_A0)));
  864. exprasmlist^.concat(new(pai68k,op_ref_reg(A_LEA,S_L,
  865. newreference(p^.right^.location.reference),R_A1)));
  866. emitcall('SET_COMP_SETS',true);
  867. maybe_loada5;
  868. popusedregisters(pushedregs);
  869. ungetiftemp(p^.left^.location.reference);
  870. ungetiftemp(p^.right^.location.reference);
  871. end;
  872. addn,subn,muln,symdifn : begin
  873. cmpop:=false;
  874. del_reference(p^.left^.location.reference);
  875. del_reference(p^.right^.location.reference);
  876. href.symbol:=nil;
  877. pushusedregisters(pushedregs,$ffff);
  878. gettempofsizereference(32,href);
  879. emitpushreferenceaddr(href);
  880. { wrong place !! was hard to find out
  881. pushusedregisters(pushedregs,$ff);}
  882. emitpushreferenceaddr(p^.right^.location.reference);
  883. emitpushreferenceaddr(p^.left^.location.reference);
  884. case p^.treetype of
  885. subn : exprasmlist^.concat(new(pai68k,op_csymbol(A_JSR,S_NO,
  886. newcsymbol('SET_SUB_SETS',0))));
  887. addn : exprasmlist^.concat(new(pai68k,op_csymbol(A_JSR,S_NO,
  888. newcsymbol('SET_ADD_SETS',0))));
  889. muln : exprasmlist^.concat(new(pai68k,op_csymbol(A_JSR,S_NO,
  890. newcsymbol('SET_MUL_SETS',0))));
  891. symdifn:
  892. emitcall('SET_SYMDIF_SETS',true);
  893. end;
  894. maybe_loada5;
  895. popusedregisters(pushedregs);
  896. ungetiftemp(p^.left^.location.reference);
  897. ungetiftemp(p^.right^.location.reference);
  898. p^.location.loc:=LOC_MEM;
  899. stringdispose(p^.location.reference.symbol);
  900. p^.location.reference:=href;
  901. end;
  902. else Message(sym_e_type_mismatch);
  903. end; { end case }
  904. end {else if begin }
  905. else Message(sym_e_type_mismatch);
  906. end; { endif }
  907. if (p^.left^.resulttype^.deftype<>stringdef) and
  908. not ((p^.left^.resulttype^.deftype=setdef) and
  909. (psetdef(p^.left^.resulttype)^.settype<>smallset)) then
  910. begin
  911. { this can be useful if for instance length(string) is called }
  912. if (p^.left^.location.loc=LOC_REFERENCE) or
  913. (p^.left^.location.loc=LOC_MEM) then
  914. ungetiftemp(p^.left^.location.reference);
  915. if (p^.right^.location.loc=LOC_REFERENCE) or
  916. (p^.right^.location.loc=LOC_MEM) then
  917. ungetiftemp(p^.right^.location.reference);
  918. end;
  919. { in case of comparison operation the put result in the flags }
  920. if cmpop then
  921. begin
  922. if not(unsigned) then
  923. begin
  924. if p^.swaped then
  925. case p^.treetype of
  926. equaln : flags:=F_E;
  927. unequaln : flags:=F_NE;
  928. ltn : flags:=F_G;
  929. lten : flags:=F_GE;
  930. gtn : flags:=F_L;
  931. gten : flags:=F_LE;
  932. end
  933. else
  934. case p^.treetype of
  935. equaln : flags:=F_E;
  936. unequaln : flags:=F_NE;
  937. ltn : flags:=F_L;
  938. lten : flags:=F_LE;
  939. gtn : flags:=F_G;
  940. gten : flags:=F_GE;
  941. end;
  942. end
  943. else
  944. begin
  945. if p^.swaped then
  946. case p^.treetype of
  947. equaln : flags:=F_E;
  948. unequaln : flags:=F_NE;
  949. ltn : flags:=F_A;
  950. lten : flags:=F_AE;
  951. gtn : flags:=F_B;
  952. gten : flags:=F_BE;
  953. end
  954. else
  955. case p^.treetype of
  956. equaln : flags:=F_E;
  957. unequaln : flags:=F_NE;
  958. ltn : flags:=F_B;
  959. lten : flags:=F_BE;
  960. gtn : flags:=F_A;
  961. gten : flags:=F_AE;
  962. end;
  963. end; { end begin }
  964. p^.location.loc:=LOC_FLAGS;
  965. p^.location.resflags:=flags;
  966. end; { endif cmpop }
  967. end;
  968. procedure processcc(p: ptree);
  969. var
  970. label1,label2: plabel;
  971. (*************************************************************************)
  972. (* Description: This routine handles the conversion of Floating point *)
  973. (* condition codes to normal cpu condition codes. *)
  974. (*************************************************************************)
  975. begin
  976. getlabel(label1);
  977. getlabel(label2);
  978. case p^.treetype of
  979. equaln,unequaln: begin
  980. { not equal clear zero flag }
  981. emitl(A_FBEQ,label1);
  982. exprasmlist^.concat(new(pai68k, op_const_reg(
  983. A_AND, S_B, NOT ZERO_FLAG, R_CCR)));
  984. emitl(A_BRA,label2);
  985. emitl(A_LABEL,label1);
  986. { equal - set zero flag }
  987. exprasmlist^.concat(new(pai68k, op_const_reg(
  988. A_OR,S_B, ZERO_FLAG, R_CCR)));
  989. emitl(A_LABEL,label2);
  990. end;
  991. ltn: begin
  992. emitl(A_FBLT,label1);
  993. { not less than }
  994. { clear N and V flags }
  995. exprasmlist^.concat(new(pai68k, op_const_reg(
  996. A_AND, S_B, NOT (NEG_FLAG OR OVFL_FLAG), R_CCR)));
  997. emitl(A_BRA,label2);
  998. emitl(A_LABEL,label1);
  999. { less than }
  1000. exprasmlist^.concat(new(pai68k, op_const_reg(
  1001. A_OR,S_B, NEG_FLAG, R_CCR)));
  1002. exprasmlist^.concat(new(pai68k, op_const_reg(
  1003. A_AND,S_B, NOT OVFL_FLAG, R_CCR)));
  1004. emitl(A_LABEL,label2);
  1005. end;
  1006. gtn: begin
  1007. emitl(A_FBGT,label1);
  1008. { not greater than }
  1009. { set Z flag }
  1010. exprasmlist^.concat(new(pai68k, op_const_reg(
  1011. A_OR, S_B, ZERO_FLAG, R_CCR)));
  1012. emitl(A_BRA,label2);
  1013. emitl(A_LABEL,label1);
  1014. { greater than }
  1015. { set N and V flags }
  1016. exprasmlist^.concat(new(pai68k, op_const_reg(
  1017. A_OR,S_B, NEG_FLAG OR OVFL_FLAG , R_CCR)));
  1018. emitl(A_LABEL,label2);
  1019. end;
  1020. gten: begin
  1021. emitl(A_FBGE,label1);
  1022. { not greater or equal }
  1023. { set N and clear V }
  1024. exprasmlist^.concat(new(pai68k, op_const_reg(
  1025. A_AND, S_B, NOT OVFL_FLAG, R_CCR)));
  1026. exprasmlist^.concat(new(pai68k, op_const_reg(
  1027. A_OR,S_B, NEG_FLAG, R_CCR)));
  1028. emitl(A_BRA,label2);
  1029. emitl(A_LABEL,label1);
  1030. { greater or equal }
  1031. { clear V and N flags }
  1032. exprasmlist^.concat(new(pai68k, op_const_reg(
  1033. A_AND, S_B, NOT (OVFL_FLAG OR NEG_FLAG), R_CCR)));
  1034. emitl(A_LABEL,label2);
  1035. end;
  1036. lten: begin
  1037. emitl(A_FBLE,label1);
  1038. { not less or equal }
  1039. { clear Z, N and V }
  1040. exprasmlist^.concat(new(pai68k, op_const_reg(
  1041. A_AND, S_B, NOT (ZERO_FLAG OR NEG_FLAG OR OVFL_FLAG), R_CCR)));
  1042. emitl(A_BRA,label2);
  1043. emitl(A_LABEL,label1);
  1044. { less or equal }
  1045. { set Z and N }
  1046. { and clear V }
  1047. exprasmlist^.concat(new(pai68k, op_const_reg(
  1048. A_OR,S_B, ZERO_FLAG OR NEG_FLAG, R_CCR)));
  1049. exprasmlist^.concat(new(pai68k, op_const_reg(
  1050. A_AND,S_B, NOT OVFL_FLAG, R_CCR)));
  1051. emitl(A_LABEL,label2);
  1052. end;
  1053. else
  1054. begin
  1055. InternalError(34);
  1056. end;
  1057. end; { end case }
  1058. end;
  1059. procedure secondfor(var p : ptree);
  1060. var
  1061. l1,l3,oldclabel,oldblabel : plabel;
  1062. omitfirstcomp,temptovalue : boolean;
  1063. hs : byte;
  1064. temp1 : treference;
  1065. hop : tasmop;
  1066. cmpreg,cmp32 : tregister;
  1067. opsize : topsize;
  1068. count_var_is_signed : boolean;
  1069. begin
  1070. oldclabel:=aktcontinuelabel;
  1071. oldblabel:=aktbreaklabel;
  1072. getlabel(aktcontinuelabel);
  1073. getlabel(aktbreaklabel);
  1074. getlabel(l3);
  1075. { could we spare the first comparison ? }
  1076. omitfirstcomp:=false;
  1077. if p^.right^.treetype=ordconstn then
  1078. if p^.left^.right^.treetype=ordconstn then
  1079. omitfirstcomp:=(p^.backward and (p^.left^.right^.value>=p^.right^.value))
  1080. or (not(p^.backward) and (p^.left^.right^.value<=p^.right^.value));
  1081. { only calculate reference }
  1082. cleartempgen;
  1083. secondpass(p^.t2);
  1084. if not(simple_loadn) then
  1085. Message(cg_e_illegal_count_var);
  1086. { produce start assignment }
  1087. cleartempgen;
  1088. secondpass(p^.left);
  1089. count_var_is_signed:=is_signed(porddef(p^.t2^.resulttype));
  1090. hs:=p^.t2^.resulttype^.size;
  1091. cmp32:=getregister32;
  1092. cmpreg:=cmp32;
  1093. case hs of
  1094. 1 : begin
  1095. opsize:=S_B;
  1096. end;
  1097. 2 : begin
  1098. opsize:=S_W;
  1099. end;
  1100. 4 : begin
  1101. opsize:=S_L;
  1102. end;
  1103. end;
  1104. cleartempgen;
  1105. secondpass(p^.right);
  1106. { calculate pointer value and check if changeable and if so }
  1107. { load into temporary variable }
  1108. if p^.right^.treetype<>ordconstn then
  1109. begin
  1110. temp1.symbol:=nil;
  1111. gettempofsizereference(hs,temp1);
  1112. temptovalue:=true;
  1113. if (p^.right^.location.loc=LOC_REGISTER) or
  1114. (p^.right^.location.loc=LOC_CREGISTER) then
  1115. begin
  1116. exprasmlist^.concat(new(pai68k,op_reg_ref(A_MOVE,opsize,p^.right^.location.register,
  1117. newreference(temp1))));
  1118. end
  1119. else
  1120. concatcopy(p^.right^.location.reference,temp1,hs,false);
  1121. end
  1122. else temptovalue:=false;
  1123. if temptovalue then
  1124. begin
  1125. if p^.t2^.location.loc=LOC_CREGISTER then
  1126. begin
  1127. exprasmlist^.concat(new(pai68k,op_ref_reg(A_CMP,opsize,newreference(temp1),
  1128. p^.t2^.location.register)));
  1129. end
  1130. else
  1131. begin
  1132. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,opsize,newreference(p^.t2^.location.reference),
  1133. cmpreg)));
  1134. exprasmlist^.concat(new(pai68k,op_ref_reg(A_CMP,opsize,newreference(temp1),
  1135. cmpreg)));
  1136. end;
  1137. end
  1138. else
  1139. begin
  1140. if not(omitfirstcomp) then
  1141. begin
  1142. if p^.t2^.location.loc=LOC_CREGISTER then
  1143. exprasmlist^.concat(new(pai68k,op_const_reg(A_CMP,opsize,p^.right^.value,
  1144. p^.t2^.location.register)))
  1145. else
  1146. exprasmlist^.concat(new(pai68k,op_const_ref(A_CMP,opsize,p^.right^.value,
  1147. newreference(p^.t2^.location.reference))));
  1148. end;
  1149. end;
  1150. if p^.backward then
  1151. begin
  1152. if count_var_is_signed then
  1153. hop:=A_BLT
  1154. else
  1155. hop:=A_BCS;
  1156. end
  1157. else
  1158. if count_var_is_signed then
  1159. hop:=A_BGT
  1160. else hop:=A_BHI;
  1161. if not(omitfirstcomp) or temptovalue then
  1162. emitl(hop,aktbreaklabel);
  1163. emitl(A_LABEL,l3);
  1164. { help register must not be in instruction block }
  1165. cleartempgen;
  1166. if assigned(p^.t1) then
  1167. secondpass(p^.t1);
  1168. emitl(A_LABEL,aktcontinuelabel);
  1169. { makes no problems there }
  1170. cleartempgen;
  1171. { demand help register again }
  1172. cmp32:=getregister32;
  1173. case hs of
  1174. 1 : begin
  1175. opsize:=S_B;
  1176. end;
  1177. 2 : begin
  1178. opsize:=S_W;
  1179. end;
  1180. 4 : opsize:=S_L;
  1181. end;
  1182. { produce comparison and the corresponding }
  1183. { jump }
  1184. if temptovalue then
  1185. begin
  1186. if p^.t2^.location.loc=LOC_CREGISTER then
  1187. begin
  1188. exprasmlist^.concat(new(pai68k,op_ref_reg(A_CMP,opsize,newreference(temp1),
  1189. p^.t2^.location.register)));
  1190. end
  1191. else
  1192. begin
  1193. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,opsize,newreference(p^.t2^.location.reference),
  1194. cmpreg)));
  1195. exprasmlist^.concat(new(pai68k,op_ref_reg(A_CMP,opsize,newreference(temp1),
  1196. cmpreg)));
  1197. end;
  1198. end
  1199. else
  1200. begin
  1201. if p^.t2^.location.loc=LOC_CREGISTER then
  1202. exprasmlist^.concat(new(pai68k,op_const_reg(A_CMP,opsize,p^.right^.value,
  1203. p^.t2^.location.register)))
  1204. else
  1205. exprasmlist^.concat(new(pai68k,op_const_ref(A_CMP,opsize,p^.right^.value,
  1206. newreference(p^.t2^.location.reference))));
  1207. end;
  1208. if p^.backward then
  1209. if count_var_is_signed then
  1210. hop:=A_BLE
  1211. else
  1212. hop :=A_BLS
  1213. else
  1214. if count_var_is_signed then
  1215. hop:=A_BGE
  1216. else
  1217. hop:=A_BCC;
  1218. emitl(hop,aktbreaklabel);
  1219. { according to count direction DEC or INC... }
  1220. { must be after the test because of 0to 255 for bytes !! }
  1221. if p^.backward then
  1222. hop:=A_SUB
  1223. else hop:=A_ADD;
  1224. if p^.t2^.location.loc=LOC_CREGISTER then
  1225. exprasmlist^.concat(new(pai68k,op_const_reg(hop,opsize,1,p^.t2^.location.register)))
  1226. else
  1227. exprasmlist^.concat(new(pai68k,op_const_ref(hop,opsize,1,newreference(p^.t2^.location.reference))));
  1228. emitl(A_JMP,l3);
  1229. { this is the break label: }
  1230. emitl(A_LABEL,aktbreaklabel);
  1231. ungetregister32(cmp32);
  1232. if temptovalue then
  1233. ungetiftemp(temp1);
  1234. aktcontinuelabel:=oldclabel;
  1235. aktbreaklabel:=oldblabel;
  1236. end;
  1237. procedure secondas(var p : ptree);
  1238. var
  1239. pushed : tpushed;
  1240. begin
  1241. secondpass(p^.left);
  1242. { save all used registers }
  1243. pushusedregisters(pushed,$ffff);
  1244. { push instance to check: }
  1245. case p^.left^.location.loc of
  1246. LOC_REGISTER,LOC_CREGISTER:
  1247. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,
  1248. S_L,p^.left^.location.register,R_SPPUSH)));
  1249. LOC_MEM,LOC_REFERENCE:
  1250. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,
  1251. S_L,newreference(p^.left^.location.reference),R_SPPUSH)));
  1252. else internalerror(100);
  1253. end;
  1254. { we doesn't modifiy the left side, we check only the type }
  1255. set_location(p^.location,p^.left^.location);
  1256. { generate type checking }
  1257. secondpass(p^.right);
  1258. case p^.right^.location.loc of
  1259. LOC_REGISTER,LOC_CREGISTER:
  1260. begin
  1261. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,
  1262. S_L,p^.right^.location.register,R_SPPUSH)));
  1263. ungetregister32(p^.right^.location.register);
  1264. end;
  1265. LOC_MEM,LOC_REFERENCE:
  1266. begin
  1267. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,
  1268. S_L,newreference(p^.right^.location.reference),R_SPPUSH)));
  1269. del_reference(p^.right^.location.reference);
  1270. end;
  1271. else internalerror(100);
  1272. end;
  1273. emitcall('DO_AS',true);
  1274. { restore register, this restores automatically the }
  1275. { result }
  1276. popusedregisters(pushed);
  1277. end;
  1278. { generates the code for a raise statement }
  1279. procedure secondraise(var p : ptree);
  1280. var
  1281. a : plabel;
  1282. begin
  1283. if assigned(p^.left) then
  1284. begin
  1285. { generate the address }
  1286. if assigned(p^.right) then
  1287. begin
  1288. secondpass(p^.right);
  1289. if codegenerror then
  1290. exit;
  1291. end
  1292. else
  1293. begin
  1294. getlabel(a);
  1295. emitl(A_LABEL,a);
  1296. exprasmlist^.concat(new(pai68k,
  1297. op_csymbol_reg(A_MOVE,S_L,newcsymbol(lab2str(a),0),R_SPPUSH)));
  1298. end;
  1299. secondpass(p^.left);
  1300. if codegenerror then
  1301. exit;
  1302. case p^.left^.location.loc of
  1303. LOC_MEM,LOC_REFERENCE : emitpushreferenceaddr(p^.left^.location.reference);
  1304. LOC_CREGISTER,LOC_REGISTER : exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,
  1305. p^.left^.location.register,R_SPPUSH)));
  1306. else Message(sym_e_type_mismatch);
  1307. end;
  1308. emitcall('DO_RAISE',true);
  1309. end
  1310. else
  1311. emitcall('DO_RERAISE',true);
  1312. end;
  1313. { This routine needs to be further checked to see if it works correctly }
  1314. { because contrary to the intel version, all large set elements are read }
  1315. { as 32-bit values, and then decomposed to find the correct byte. }
  1316. { CHECKED : Depending on the result size, if reference, a load may be }
  1317. { required on word, long or byte. }
  1318. procedure loadsetelement(var p : ptree);
  1319. var
  1320. hr : tregister;
  1321. opsize : topsize;
  1322. begin
  1323. { copy the element in the d0.b register, slightly complicated }
  1324. case p^.location.loc of
  1325. LOC_REGISTER,
  1326. LOC_CREGISTER : begin
  1327. hr:=p^.location.register;
  1328. emit_reg_reg(A_MOVE,S_L,hr,R_D0);
  1329. ungetregister32(hr);
  1330. end;
  1331. else
  1332. begin
  1333. { This is quite complicated, because of the endian on }
  1334. { the m68k! }
  1335. opsize:=S_NO;
  1336. case integer(p^.resulttype^.savesize) of
  1337. 1 : opsize:=S_B;
  1338. 2 : opsize:=S_W;
  1339. 4 : opsize:=S_L;
  1340. else
  1341. internalerror(19);
  1342. end;
  1343. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,opsize,
  1344. newreference(p^.location.reference),R_D0)));
  1345. exprasmlist^.concat(new(pai68k,op_const_reg(A_AND,S_L,
  1346. 255,R_D0)));
  1347. {
  1348. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,
  1349. newreference(p^.location.reference),R_D0))); }
  1350. { exprasmlist^.concat(new(pai68k,op_const_reg(A_AND,S_L,
  1351. $ff,R_D0))); }
  1352. del_reference(p^.location.reference);
  1353. end;
  1354. end;
  1355. end;
  1356. { could be built into secondadd but it }
  1357. { should be easy to read }
  1358. procedure secondin(var p : ptree);
  1359. type Tsetpart=record
  1360. range:boolean; {Part is a range.}
  1361. start,stop:byte; {Start/stop when range; Stop=element
  1362. when an element.}
  1363. end;
  1364. var
  1365. pushed,ranges : boolean;
  1366. hr : tregister;
  1367. setparts:array[1..8] of Tsetpart;
  1368. i,numparts:byte;
  1369. href,href2:Treference;
  1370. l,l2 : plabel;
  1371. hl,hl1 : plabel;
  1372. hl2, hl3: plabel;
  1373. opsize : topsize;
  1374. function swaplongint(l : longint): longint;
  1375. var
  1376. w1: word;
  1377. w2: word;
  1378. begin
  1379. w1:=l and $ffff;
  1380. w2:=l shr 16;
  1381. l:=swap(w2)+(longint(swap(w1)) shl 16);
  1382. swaplongint:=l;
  1383. end;
  1384. function analizeset(Aset:Pconstset):boolean;
  1385. type byteset=set of byte;
  1386. tlongset = array[0..7] of longint;
  1387. var compares,maxcompares:word;
  1388. someset : tlongset;
  1389. i:byte;
  1390. begin
  1391. analizeset:=false;
  1392. ranges:=false;
  1393. numparts:=0;
  1394. compares:=0;
  1395. {Lots of comparisions take a lot of time, so do not allow
  1396. too much comparisions. 8 comparisions are, however, still
  1397. smalller than emitting the set.}
  1398. maxcompares:=5;
  1399. if cs_littlesize in aktglobalswitches then
  1400. maxcompares:=8;
  1401. move(ASet^,someset,32);
  1402. { On Big endian machines sets are stored }
  1403. { as INTEL Little-endian format, therefore }
  1404. { we must convert it to the correct format }
  1405. {$IFDEF BIG_ENDIAN}
  1406. for I:=0 to 7 do
  1407. someset[i]:=swaplongint(someset[i]);
  1408. {$ENDIF}
  1409. for i:=0 to 255 do
  1410. if i in byteset(someset) then
  1411. begin
  1412. if (numparts=0) or
  1413. (i<>setparts[numparts].stop+1) then
  1414. begin
  1415. {Set element is a separate element.}
  1416. inc(compares);
  1417. if compares>maxcompares then
  1418. exit;
  1419. inc(numparts);
  1420. setparts[numparts].range:=false;
  1421. setparts[numparts].stop:=i;
  1422. end
  1423. else
  1424. {Set element is part of a range.}
  1425. if not setparts[numparts].range then
  1426. begin
  1427. {Transform an element into a range.}
  1428. setparts[numparts].range:=true;
  1429. setparts[numparts].start:=
  1430. setparts[numparts].stop;
  1431. setparts[numparts].stop:=i;
  1432. inc(compares);
  1433. if compares>maxcompares then
  1434. exit;
  1435. end
  1436. else
  1437. begin
  1438. {Extend a range.}
  1439. setparts[numparts].stop:=i;
  1440. {A range of two elements can better
  1441. be checked as two separate ones.
  1442. When extending a range, our range
  1443. becomes larger than two elements.}
  1444. ranges:=true;
  1445. end;
  1446. end;
  1447. analizeset:=true;
  1448. end; { end analizeset }
  1449. begin
  1450. if psetdef(p^.right^.resulttype)^.settype=smallset then
  1451. begin
  1452. if p^.left^.treetype=ordconstn then
  1453. begin
  1454. { only compulsory }
  1455. secondpass(p^.left);
  1456. secondpass(p^.right);
  1457. if codegenerror then
  1458. exit;
  1459. p^.location.resflags:=F_NE;
  1460. { Because of the Endian of the m68k, we have to consider this as a }
  1461. { normal set and load it byte per byte, otherwise we will never get }
  1462. { the correct result. }
  1463. case p^.right^.location.loc of
  1464. LOC_REGISTER,LOC_CREGISTER :
  1465. begin
  1466. emit_reg_reg(A_MOVE,S_L,p^.right^.location.register,R_D1);
  1467. exprasmlist^.concat(new(pai68k,
  1468. op_const_reg(A_AND,S_L, 1 shl (p^.left^.value and 31),R_D1)));
  1469. end;
  1470. else
  1471. begin
  1472. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,newreference(
  1473. p^.right^.location.reference),R_D1)));
  1474. exprasmlist^.concat(new(pai68k,op_const_reg(
  1475. A_AND,S_L,1 shl (p^.left^.value and 31) ,R_D1)));
  1476. end;
  1477. end;
  1478. del_reference(p^.right^.location.reference);
  1479. end
  1480. else
  1481. begin
  1482. { calculate both operators }
  1483. { the complex one first }
  1484. firstcomplex(p);
  1485. secondpass(p^.left);
  1486. { are too few registers free? }
  1487. pushed:=maybe_push(p^.right^.registers32,p^.left);
  1488. secondpass(p^.right);
  1489. if pushed then
  1490. restore(p^.left);
  1491. { of course not commutative }
  1492. if p^.swaped then
  1493. swaptree(p);
  1494. { load index into register }
  1495. case p^.left^.location.loc of
  1496. LOC_REGISTER,
  1497. LOC_CREGISTER :
  1498. hr:=p^.left^.location.register;
  1499. else
  1500. begin
  1501. { Small sets are always 32 bit values, there is no }
  1502. { way they can be anything else, so no problems here}
  1503. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,
  1504. newreference(p^.left^.location.reference),R_D1)));
  1505. hr:=R_D1;
  1506. del_reference(p^.left^.location.reference);
  1507. end;
  1508. end;
  1509. case p^.right^.location.loc of
  1510. LOC_REGISTER,
  1511. LOC_CREGISTER : exprasmlist^.concat(new(pai68k, op_reg_reg(A_BTST,S_L,hr,p^.right^.location.register)));
  1512. else
  1513. begin
  1514. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,S_L,newreference(p^.right^.location.reference),
  1515. R_D0)));
  1516. exprasmlist^.concat(new(pai68k,op_reg_reg(A_BTST,S_L,hr,R_D0)));
  1517. del_reference(p^.right^.location.reference);
  1518. end;
  1519. end;
  1520. { support carry routines }
  1521. { sets the carry flags according to the result of BTST }
  1522. { i.e the Z flag. }
  1523. getlabel(hl);
  1524. emitl(A_BNE,hl);
  1525. { leave all bits unchanged except Carry = 0 }
  1526. exprasmlist^.concat(new(pai68k, op_const_reg(A_AND, S_B, $FE, R_CCR)));
  1527. getlabel(hl1);
  1528. emitl(A_BRA,hl1);
  1529. emitl(A_LABEL, hl);
  1530. { set carry to 1 }
  1531. exprasmlist^.concat(new(pai68k, op_const_reg(A_OR, S_B, $01, R_CCR)));
  1532. emitl(A_LABEL, hl1);
  1533. { end support carry routines }
  1534. p^.location.loc:=LOC_FLAGS;
  1535. p^.location.resflags:=F_C;
  1536. end;
  1537. end
  1538. else { //// NOT a small set //// }
  1539. begin
  1540. if p^.left^.treetype=ordconstn then
  1541. begin
  1542. { only compulsory }
  1543. secondpass(p^.left);
  1544. secondpass(p^.right);
  1545. if codegenerror then
  1546. exit;
  1547. p^.location.resflags:=F_NE;
  1548. inc(p^.right^.location.reference.offset,(p^.left^.value div 32)*4);
  1549. exprasmlist^.concat(new(pai68k, op_ref_reg(A_MOVE, S_L,
  1550. newreference(p^.right^.location.reference), R_D1)));
  1551. exprasmlist^.concat(new(pai68k, op_const_reg(A_AND, S_L,
  1552. 1 shl (p^.left^.value mod 32),R_D1)));
  1553. del_reference(p^.right^.location.reference);
  1554. end
  1555. else
  1556. begin
  1557. if (p^.right^.treetype=setconstrn) and
  1558. analizeset(p^.right^.constset) then
  1559. begin
  1560. {It gives us advantage to check for the set elements
  1561. separately instead of using the SET_IN_BYTE procedure.
  1562. To do: Build in support for LOC_JUMP.}
  1563. secondpass(p^.left);
  1564. {We won't do a second pass on p^.right, because
  1565. this will emit the constant set.}
  1566. case p^.left^.location.loc of
  1567. LOC_REGISTER,
  1568. LOC_CREGISTER :
  1569. exprasmlist^.concat(new(pai68k,op_const_reg(A_AND,S_L,
  1570. 255,p^.left^.location.register)));
  1571. else
  1572. Begin
  1573. { Because of the m68k endian, then we must LOAD normally the }
  1574. { value into a register first, all depending on the source }
  1575. { size! }
  1576. opsize:=S_NO;
  1577. case integer(p^.left^.resulttype^.savesize) of
  1578. 1 : opsize:=S_B;
  1579. 2 : opsize:=S_W;
  1580. 4 : opsize:=S_L;
  1581. else
  1582. internalerror(19);
  1583. end;
  1584. exprasmlist^.concat(new(pai68k,op_ref_reg(A_MOVE,opsize,
  1585. newreference(p^.left^.location.reference),R_D0)));
  1586. exprasmlist^.concat(new(pai68k,op_const_reg(A_AND,S_L,
  1587. 255,R_D0)));
  1588. end;
  1589. end;
  1590. {Get a label to jump to the end.}
  1591. p^.location.loc:=LOC_FLAGS;
  1592. {It's better to use the zero flag when there are no ranges.}
  1593. if ranges then
  1594. p^.location.resflags:=F_C
  1595. else
  1596. p^.location.resflags:=F_E;
  1597. href.symbol := nil;
  1598. clear_reference(href);
  1599. getlabel(l);
  1600. href.symbol:=stringdup(lab2str(l));
  1601. for i:=1 to numparts do
  1602. if setparts[i].range then
  1603. begin
  1604. {Check if left is in a range.}
  1605. {Get a label to jump over the check.}
  1606. href2.symbol := nil;
  1607. clear_reference(href2);
  1608. getlabel(l2);
  1609. href.symbol:=stringdup(lab2str(l2));
  1610. if setparts[i].start=setparts[i].stop-1 then
  1611. begin
  1612. case p^.left^.location.loc of
  1613. LOC_REGISTER,
  1614. LOC_CREGISTER :
  1615. exprasmlist^.concat(new(pai68k,op_const_reg(A_CMP,S_W,
  1616. setparts[i].start,p^.left^.location.register)));
  1617. else
  1618. exprasmlist^.concat(new(pai68k,op_const_reg(A_CMP,S_W,
  1619. setparts[i].start,R_D0)));
  1620. { exprasmlist^.concat(new(pai68k,op_const_ref(A_CMP,S_B,
  1621. setparts[i].start,newreference(p^.left^.location.reference))));}
  1622. end;
  1623. {Result should be in carry flag when ranges are used.}
  1624. { Here the m68k does not affect any flag except the }
  1625. { flag which is OR'ed }
  1626. if ranges then
  1627. exprasmlist^.concat(new(pai68k,op_const_reg(A_OR,S_B,$01,R_CCR)));
  1628. {If found, jump to end.}
  1629. emitl(A_BEQ,l);
  1630. case p^.left^.location.loc of
  1631. LOC_REGISTER,
  1632. LOC_CREGISTER :
  1633. exprasmlist^.concat(new(pai68k,op_const_reg(A_CMP,S_W,
  1634. setparts[i].stop,p^.left^.location.register)));
  1635. else
  1636. exprasmlist^.concat(new(pai68k,op_const_reg(A_CMP,S_W,
  1637. setparts[i].stop,R_D0)));
  1638. { exprasmlist^.concat(new(pai68k,op_const_ref(A_CMP,S_B,
  1639. setparts[i].stop,newreference(p^.left^.location.reference))));}
  1640. end;
  1641. {Result should be in carry flag when ranges are used.}
  1642. { Here the m68k does not affect any flag except the }
  1643. { flag which is OR'ed }
  1644. if ranges then
  1645. exprasmlist^.concat(new(pai68k,op_const_reg(A_OR,S_B,$01,R_CCR)));
  1646. {If found, jump to end.}
  1647. emitl(A_BEQ,l);
  1648. end
  1649. else
  1650. begin
  1651. if setparts[i].start<>0 then
  1652. begin
  1653. {We only check for the lower bound if it is > 0, because
  1654. set elements lower than 0 do nt exist.}
  1655. case p^.left^.location.loc of
  1656. LOC_REGISTER,
  1657. LOC_CREGISTER :
  1658. exprasmlist^.concat(new(pai68k,op_const_reg(A_CMP,S_W,
  1659. setparts[i].start,p^.left^.location.register)));
  1660. else
  1661. exprasmlist^.concat(new(pai68k,op_const_reg(A_CMP,S_W,
  1662. setparts[i].start,R_D0)));
  1663. { exprasmlist^.concat(new(pai68k,op_const_ref(A_CMP,S_B,
  1664. setparts[i].start,newreference(p^.left^.location.reference)))); }
  1665. end;
  1666. {If lower, jump to next check.}
  1667. emitl(A_BCS,l2);
  1668. end;
  1669. if setparts[i].stop<>255 then
  1670. begin
  1671. {We only check for the high bound if it is < 255, because
  1672. set elements higher than 255 do nt exist.}
  1673. case p^.left^.location.loc of
  1674. LOC_REGISTER,
  1675. LOC_CREGISTER :
  1676. exprasmlist^.concat(new(pai68k,op_const_reg(A_CMP,S_W,
  1677. setparts[i].stop+1,p^.left^.location.register)));
  1678. else
  1679. exprasmlist^.concat(new(pai68k,op_const_reg(A_CMP,S_W,
  1680. setparts[i].stop+1,R_D0)));
  1681. { exprasmlist^.concat(new(pai68k,op_const_ref(A_CMP,S_B,
  1682. setparts[i].stop+1,newreference(p^.left^.location.reference))));}
  1683. end; { end case }
  1684. {If higher, element is in set.}
  1685. emitl(A_BCS,l);
  1686. end
  1687. else
  1688. begin
  1689. exprasmlist^.concat(new(pai68k,op_const_reg(A_OR,S_B,$01,R_CCR)));
  1690. emitl(A_JMP,l);
  1691. end;
  1692. end;
  1693. {Emit the jump over label.}
  1694. exprasmlist^.concat(new(pai_label,init(l2)));
  1695. end
  1696. else
  1697. begin
  1698. {Emit code to check if left is an element.}
  1699. case p^.left^.location.loc of
  1700. LOC_REGISTER,
  1701. LOC_CREGISTER :
  1702. exprasmlist^.concat(new(pai68k,op_const_reg(A_CMP,S_W,
  1703. setparts[i].stop,p^.left^.location.register)));
  1704. else
  1705. { exprasmlist^.concat(new(pai68k,op_const_ref(A_CMP,S_B,
  1706. setparts[i].stop,newreference(p^.left^.location.reference))));}
  1707. exprasmlist^.concat(new(pai68k,op_const_reg(A_CMP,S_W,
  1708. setparts[i].stop,R_D0)));
  1709. end;
  1710. {Result should be in carry flag when ranges are used.}
  1711. if ranges then
  1712. exprasmlist^.concat(new(pai68k, op_const_reg(A_OR,S_B,$01,R_CCR)));
  1713. {If found, jump to end.}
  1714. emitl(A_BEQ,l);
  1715. end;
  1716. if ranges then
  1717. { clear carry flag }
  1718. exprasmlist^.concat(new(pai68k,op_const_reg(A_AND,S_B,$FE,R_CCR)));
  1719. {To compensate for not doing a second pass.}
  1720. stringdispose(p^.right^.location.reference.symbol);
  1721. {Now place the end label.}
  1722. exprasmlist^.concat(new(pai_label,init(l)));
  1723. end
  1724. else
  1725. begin
  1726. { calculate both operators }
  1727. { the complex one first }
  1728. firstcomplex(p);
  1729. secondpass(p^.left);
  1730. set_location(p^.location,p^.left^.location);
  1731. { are too few registers free? }
  1732. pushed:=maybe_push(p^.right^.registers32,p);
  1733. secondpass(p^.right);
  1734. if pushed then restore(p);
  1735. { of course not commutative }
  1736. if p^.swaped then
  1737. swaptree(p);
  1738. { SET_IN_BYTE is an inline assembler procedure instead }
  1739. { of a normal procedure, which is *MUCH* faster }
  1740. { Parameters are passed by registers, and FLAGS are set }
  1741. { according to the result. }
  1742. { a0 = address of set }
  1743. { d0.b = value to compare with }
  1744. { CARRY SET IF FOUND ON EXIT }
  1745. loadsetelement(p^.left);
  1746. exprasmlist^.concat(new(pai68k,op_ref_reg(A_LEA,S_L,
  1747. newreference(p^.right^.location.reference),R_A0)));;
  1748. { emitpushreferenceaddr(p^.right^.location.reference);}
  1749. del_reference(p^.right^.location.reference);
  1750. emitcall('SET_IN_BYTE',true);
  1751. { ungetiftemp(p^.right^.location.reference); }
  1752. p^.location.loc:=LOC_FLAGS;
  1753. p^.location.resflags:=F_C;
  1754. end;
  1755. end;
  1756. end;
  1757. end;
  1758. procedure secondexpr(var p : ptree);
  1759. begin
  1760. secondpass(p^.left);
  1761. end;
  1762. procedure secondblockn(var p : ptree);
  1763. var
  1764. hp : ptree;
  1765. begin
  1766. hp:=p^.left;
  1767. while assigned(hp) do
  1768. begin
  1769. { assignments could be distance optimized }
  1770. if assigned(hp^.right) then
  1771. begin
  1772. cleartempgen;
  1773. secondpass(hp^.right);
  1774. end;
  1775. hp:=hp^.left;
  1776. end;
  1777. end;
  1778. procedure second_while_repeatn(var p : ptree);
  1779. var
  1780. l1,l2,l3,oldclabel,oldblabel : plabel;
  1781. otlabel,oflabel : plabel;
  1782. begin
  1783. getlabel(l1);
  1784. getlabel(l2);
  1785. { arrange continue and breaklabels: }
  1786. oldclabel:=aktcontinuelabel;
  1787. oldblabel:=aktbreaklabel;
  1788. if p^.treetype=repeatn then
  1789. begin
  1790. emitl(A_LABEL,l1);
  1791. aktcontinuelabel:=l1;
  1792. aktbreaklabel:=l2;
  1793. cleartempgen;
  1794. if assigned(p^.right) then
  1795. secondpass(p^.right);
  1796. otlabel:=truelabel;
  1797. oflabel:=falselabel;
  1798. truelabel:=l2;
  1799. falselabel:=l1;
  1800. cleartempgen;
  1801. secondpass(p^.left);
  1802. maketojumpbool(p^.left);
  1803. emitl(A_LABEL,l2);
  1804. truelabel:=otlabel;
  1805. falselabel:=oflabel;
  1806. end
  1807. else { //// NOT a small set //// }
  1808. begin
  1809. { handling code at the end as it is much more efficient }
  1810. emitl(A_JMP,l2);
  1811. emitl(A_LABEL,l1);
  1812. cleartempgen;
  1813. getlabel(l3);
  1814. aktcontinuelabel:=l2;
  1815. aktbreaklabel:=l3;
  1816. if assigned(p^.right) then
  1817. secondpass(p^.right);
  1818. emitl(A_LABEL,l2);
  1819. otlabel:=truelabel;
  1820. oflabel:=falselabel;
  1821. truelabel:=l1;
  1822. falselabel:=l3;
  1823. cleartempgen;
  1824. secondpass(p^.left);
  1825. maketojumpbool(p^.left);
  1826. emitl(A_LABEL,l3);
  1827. truelabel:=otlabel;
  1828. falselabel:=oflabel;
  1829. end;
  1830. aktcontinuelabel:=oldclabel;
  1831. aktbreaklabel:=oldblabel;
  1832. end;
  1833. procedure secondifn(var p : ptree);
  1834. var
  1835. hl,otlabel,oflabel : plabel;
  1836. begin
  1837. otlabel:=truelabel;
  1838. oflabel:=falselabel;
  1839. getlabel(truelabel);
  1840. getlabel(falselabel);
  1841. cleartempgen;
  1842. secondpass(p^.left);
  1843. maketojumpbool(p^.left);
  1844. if assigned(p^.right) then
  1845. begin
  1846. emitl(A_LABEL,truelabel);
  1847. cleartempgen;
  1848. secondpass(p^.right);
  1849. end;
  1850. if assigned(p^.t1) then
  1851. begin
  1852. if assigned(p^.right) then
  1853. begin
  1854. getlabel(hl);
  1855. emitl(A_JMP,hl);
  1856. end;
  1857. emitl(A_LABEL,falselabel);
  1858. cleartempgen;
  1859. secondpass(p^.t1);
  1860. if assigned(p^.right) then
  1861. emitl(A_LABEL,hl);
  1862. end
  1863. else
  1864. emitl(A_LABEL,falselabel);
  1865. if not(assigned(p^.right)) then
  1866. emitl(A_LABEL,truelabel);
  1867. truelabel:=otlabel;
  1868. falselabel:=oflabel;
  1869. end;
  1870. procedure secondbreakn(var p : ptree);
  1871. begin
  1872. if aktbreaklabel<>nil then
  1873. emitl(A_JMP,aktbreaklabel)
  1874. else
  1875. Message(cg_e_break_not_allowed);
  1876. end;
  1877. end.
  1878. {
  1879. $Log$
  1880. Revision 1.8 1998-08-31 12:26:23 peter
  1881. * m68k and palmos updates from surebugfixes
  1882. Revision 1.7 1998/08/10 14:43:17 peter
  1883. * string type st_ fixed
  1884. Revision 1.6 1998/07/10 10:51:00 peter
  1885. * m68k updates
  1886. Revision 1.5 1998/06/08 13:13:37 pierre
  1887. + temporary variables now in temp_gen.pas unit
  1888. because it is processor independent
  1889. * mppc68k.bat modified to undefine i386 and support_mmx
  1890. (which are defaults for i386)
  1891. Revision 1.4 1998/06/04 23:51:35 peter
  1892. * m68k compiles
  1893. + .def file creation moved to gendef.pas so it could also be used
  1894. for win32
  1895. Revision 1.3 1998/04/29 10:33:45 pierre
  1896. + added some code for ansistring (not complete nor working yet)
  1897. * corrected operator overloading
  1898. * corrected nasm output
  1899. + started inline procedures
  1900. + added starstarn : use ** for exponentiation (^ gave problems)
  1901. + started UseTokenInfo cond to get accurate positions
  1902. }