cg68k2.pas 91 KB

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