ra68kmot.pas 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  1. {
  2. Copyright (c) 1998-2000 by Carl Eric Codere
  3. This unit does the parsing process for the motorola inline assembler
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ra68kmot;
  18. {$i fpcdefs.inc}
  19. {**********************************************************************}
  20. { WARNING }
  21. {**********************************************************************}
  22. { Any modification in the order or removal of terms in the tables }
  23. { in m68k.pas and asmo68k.pas will BREAK the code in this unit, }
  24. { unless the appropriate changes are made to this unit. Addition }
  25. { of terms though, will not change the code herein. }
  26. {**********************************************************************}
  27. {---------------------------------------------------------------------------}
  28. { LEFT TO DO }
  29. {---------------------------------------------------------------------------}
  30. { o Add support for sized indexing such as in d0.l }
  31. { presently only (an,dn) is supported for indexing -- }
  32. { size defaults to LONG. }
  33. { o Add support for MC68020 opcodes. }
  34. { o Add support for MC68020 adressing modes. }
  35. { o Add operand checking with m68k opcode table in ConcatOpCode }
  36. { o Add Floating point support }
  37. {---------------------------------------------------------------------------}
  38. interface
  39. uses
  40. cutils,
  41. globtype,cclasses,cpubase,
  42. symconst,
  43. aasmbase,
  44. rabase,rasm,ra68k,rautils;
  45. type
  46. tasmtoken = (
  47. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_HEXNUM,AS_OCTALNUM,
  48. AS_BINNUM,AS_COMMA,AS_LBRACKET,AS_RBRACKET,AS_LPAREN,
  49. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,AS_INTNUM,
  50. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,AS_APPT,AS_REALNUM,
  51. AS_ALIGN,
  52. {------------------ Assembler directives --------------------}
  53. AS_DB,AS_DW,AS_DD,AS_XDEF,AS_END,
  54. {------------------ Assembler Operators --------------------}
  55. AS_MOD,AS_SHL,AS_SHR,AS_NOT,AS_AND,AS_OR,AS_XOR);
  56. tasmkeyword = string[10];
  57. tm68kmotreader = class(tasmreader)
  58. actasmtoken : tasmtoken;
  59. prevasmtoken : tasmtoken;
  60. procedure SetupTables;
  61. function Assemble: tlinkedlist;override;
  62. function is_asmopcode(const s: string) : boolean;
  63. Function is_asmdirective(const s: string):boolean;
  64. function is_register(const s:string):boolean;
  65. procedure GetToken;
  66. function consume(t : tasmtoken):boolean;
  67. function findopcode(s: string; var opsize: topsize): tasmop;
  68. Function BuildExpression(allow_symbol : boolean; asmsym : pshortstring) : longint;
  69. Procedure BuildConstant(maxvalue: longint);
  70. Procedure BuildRealConstant(typ : tfloattype);
  71. Procedure BuildScaling(const oper:tm68koperand);
  72. Function BuildRefExpression: longint;
  73. procedure BuildReference(const oper:tm68koperand);
  74. Procedure BuildOperand(const oper:tm68koperand);
  75. Procedure BuildStringConstant(asciiz: boolean);
  76. Procedure BuildOpCode(instr:Tm68kinstruction);
  77. end;
  78. Implementation
  79. uses
  80. { global }
  81. globals,verbose,
  82. systems,
  83. { aasm }
  84. cpuinfo,aasmtai,aasmdata,aasmcpu,
  85. cgbase,cgutils,
  86. { symtable }
  87. symbase,symtype,symsym,symdef,symtable,
  88. { pass 1 }
  89. nbas,
  90. { parser }
  91. scanner,ag68kgas,
  92. itcpugas
  93. ;
  94. const
  95. firstdirective = AS_DB;
  96. lastdirective = AS_END;
  97. firstoperator = AS_MOD;
  98. lastoperator = AS_XOR;
  99. _count_asmdirectives = longint(lastdirective)-longint(firstdirective);
  100. _count_asmoperators = longint(lastoperator)-longint(firstoperator);
  101. _asmdirectives : array[0.._count_asmdirectives] of tasmkeyword =
  102. ('DC.B','DC.W','DC.L','XDEF','END');
  103. { problems with shl,shr,not,and,or and xor, they are }
  104. { context sensitive. }
  105. _asmoperators : array[0.._count_asmoperators] of tasmkeyword = (
  106. 'MOD','SHL','SHR','NOT','AND','OR','XOR');
  107. token2str : array[tasmtoken] of tasmkeyword=(
  108. 'NONE','LABEL','LLABEL','STRING','HEXNUM','OCTALNUM',
  109. 'BINNUM',',','[',']','(',
  110. ')',':','.','+','-','*','INTNUM',
  111. 'SEPARATOR','ID','REGISTER','OPCODE','/','APPT','REALNUM',
  112. 'ALIGN',
  113. {------------------ Assembler directives --------------------}
  114. 'DB','DW','DD','XDEF','END',
  115. {------------------ Assembler Operators --------------------}
  116. 'MOD','SHL','SHR','NOT','AND','OR','XOR');
  117. const
  118. firsttoken : boolean = TRUE;
  119. operandnum : byte = 0;
  120. procedure tm68kmotreader.SetupTables;
  121. { creates uppercased symbol tables for speed access }
  122. var
  123. i : tasmop;
  124. Begin
  125. { opcodes }
  126. iasmops:=TFPHashList.create;
  127. for i:=firstop to lastop do
  128. iasmops.Add(upper(gas_op2str[i]),Pointer(PtrInt(i)));
  129. end;
  130. {---------------------------------------------------------------------}
  131. { Routines for the tokenizing }
  132. {---------------------------------------------------------------------}
  133. function tm68kmotreader.is_asmopcode(const s: string):boolean;
  134. var
  135. hs : string;
  136. j : byte;
  137. begin
  138. is_asmopcode:=false;
  139. { first of all we remove the suffix }
  140. j:=pos('.',s);
  141. if j>0 then
  142. hs:=copy(s,1,j-1)
  143. else
  144. hs:=s;
  145. { Search opcodes }
  146. actopcode:=tasmop(PtrInt(iasmops.Find(hs)));
  147. if actopcode<>A_NONE then
  148. begin
  149. actasmtoken:=AS_OPCODE;
  150. result:=TRUE;
  151. exit;
  152. end;
  153. end;
  154. Function tm68kmotreader.is_asmdirective(const s: string):boolean;
  155. var
  156. i:byte;
  157. begin
  158. result:=false;
  159. for i:=0 to _count_asmdirectives do
  160. begin
  161. if s=_asmdirectives[i] then
  162. begin
  163. actasmtoken := tasmtoken(longint(firstdirective)+i);
  164. result:=true;
  165. exit;
  166. end;
  167. end;
  168. end;
  169. function tm68kmotreader.is_register(const s:string):boolean;
  170. begin
  171. is_register:=false;
  172. // FIX ME!!! Ugly, needs a proper fix (KB)
  173. actasmregister:=gas_regnum_search('%'+lower(s));
  174. if actasmregister<>NR_NO then
  175. begin
  176. is_register:=true;
  177. actasmtoken:=AS_REGISTER;
  178. end;
  179. end;
  180. Procedure tm68kmotreader.GetToken;
  181. {*********************************************************************}
  182. { FUNCTION GetToken: tinteltoken; }
  183. { Description: This routine returns intel assembler tokens and }
  184. { does some minor syntax error checking. }
  185. {*********************************************************************}
  186. var
  187. token: tasmtoken;
  188. forcelabel: boolean;
  189. s : string;
  190. begin
  191. forcelabel := FALSE;
  192. actasmpattern :='';
  193. {* INIT TOKEN TO NOTHING *}
  194. token := AS_NONE;
  195. { while space and tab , continue scan... }
  196. while c in [' ',#9] do
  197. c:=current_scanner.asmgetchar;
  198. if not (c in [#10,#13,'{',';']) then
  199. current_scanner.gettokenpos;
  200. { Possiblities for first token in a statement: }
  201. { Local Label, Label, Directive, Prefix or Opcode.... }
  202. if firsttoken and not (c in [#10,#13,'{',';']) then
  203. begin
  204. firsttoken := FALSE;
  205. if c = '@' then
  206. begin
  207. token := AS_LLABEL; { this is a local label }
  208. { Let us point to the next character }
  209. c := current_scanner.asmgetchar;
  210. end;
  211. while c in ['A'..'Z','a'..'z','0'..'9','_','@','.'] do
  212. begin
  213. { if there is an at_sign, then this must absolutely be a label }
  214. if c = '@' then forcelabel:=TRUE;
  215. actasmpattern := actasmpattern + c;
  216. c := current_scanner.asmgetchar;
  217. end;
  218. uppervar(actasmpattern);
  219. if c = ':' then
  220. begin
  221. case token of
  222. AS_NONE: token := AS_LABEL;
  223. AS_LLABEL: ; { do nothing }
  224. end; { end case }
  225. { let us point to the next character }
  226. c := current_scanner.asmgetchar;
  227. actasmtoken := token;
  228. exit;
  229. end;
  230. { Are we trying to create an identifier with }
  231. { an at-sign...? }
  232. if forcelabel then
  233. Message(asmr_e_none_label_contain_at);
  234. If is_asmopcode(actasmpattern) then
  235. exit;
  236. if is_asmdirective(actasmpattern) then
  237. exit
  238. else
  239. begin
  240. actasmtoken := AS_NONE;
  241. Message1(asmr_e_invalid_or_missing_opcode,actasmpattern);
  242. end;
  243. end
  244. else { else firsttoken }
  245. { Here we must handle all possible cases }
  246. begin
  247. case c of
  248. '@': { possiblities : - local label reference , such as in jmp @local1 }
  249. { - @Result, @Code or @Data special variables. }
  250. begin
  251. actasmpattern := c;
  252. c:= current_scanner.asmgetchar;
  253. while c in ['A'..'Z','a'..'z','0'..'9','_','@','.'] do
  254. begin
  255. actasmpattern := actasmpattern + c;
  256. c := current_scanner.asmgetchar;
  257. end;
  258. uppervar(actasmpattern);
  259. actasmtoken := AS_ID;
  260. exit;
  261. end;
  262. { identifier, register, opcode, prefix or directive }
  263. 'A'..'Z','a'..'z','_': begin
  264. actasmpattern := c;
  265. c:= current_scanner.asmgetchar;
  266. while c in ['A'..'Z','a'..'z','0'..'9','_','.'] do
  267. begin
  268. actasmpattern := actasmpattern + c;
  269. c := current_scanner.asmgetchar;
  270. end;
  271. uppervar(actasmpattern);
  272. { this isn't the first token, so it can't be an
  273. opcode }
  274. {If is_asmopcode(actasmpattern) then
  275. exit;}
  276. if is_register(actasmpattern) then
  277. exit;
  278. if is_asmdirective(actasmpattern) then
  279. exit;
  280. { this is surely an identifier }
  281. actasmtoken := AS_ID;
  282. exit;
  283. end;
  284. { override operator... not supported }
  285. '&': begin
  286. c:=current_scanner.asmgetchar;
  287. actasmtoken := AS_AND;
  288. end;
  289. { string or character }
  290. '''' :
  291. begin
  292. actasmpattern:='';
  293. while true do
  294. begin
  295. if c = '''' then
  296. begin
  297. c:=current_scanner.asmgetchar;
  298. if c=#10 then
  299. begin
  300. Message(scan_f_string_exceeds_line);
  301. break;
  302. end;
  303. repeat
  304. if c=''''then
  305. begin
  306. c:=current_scanner.asmgetchar;
  307. if c='''' then
  308. begin
  309. actasmpattern:=actasmpattern+'''';
  310. c:=current_scanner.asmgetchar;
  311. if c=#10 then
  312. begin
  313. Message(scan_f_string_exceeds_line);
  314. break;
  315. end;
  316. end
  317. else break;
  318. end
  319. else
  320. begin
  321. actasmpattern:=actasmpattern+c;
  322. c:=current_scanner.asmgetchar;
  323. if c=#10 then
  324. begin
  325. Message(scan_f_string_exceeds_line);
  326. break
  327. end;
  328. end;
  329. until false; { end repeat }
  330. end
  331. else break; { end if }
  332. end; { end while }
  333. token:=AS_STRING;
  334. actasmtoken := token;
  335. exit;
  336. end;
  337. '$' : begin
  338. c:=current_scanner.asmgetchar;
  339. while c in ['0'..'9','A'..'F','a'..'f'] do
  340. begin
  341. actasmpattern := actasmpattern + c;
  342. c := current_scanner.asmgetchar;
  343. end;
  344. actasmtoken := AS_HEXNUM;
  345. exit;
  346. end;
  347. ',' : begin
  348. actasmtoken := AS_COMMA;
  349. c:=current_scanner.asmgetchar;
  350. exit;
  351. end;
  352. '(' : begin
  353. actasmtoken := AS_LPAREN;
  354. c:=current_scanner.asmgetchar;
  355. exit;
  356. end;
  357. ')' : begin
  358. actasmtoken := AS_RPAREN;
  359. c:=current_scanner.asmgetchar;
  360. exit;
  361. end;
  362. ':' : begin
  363. actasmtoken := AS_COLON;
  364. c:=current_scanner.asmgetchar;
  365. exit;
  366. end;
  367. { '.' : begin
  368. actasmtoken := AS_DOT;
  369. c:=current_scanner.asmgetchar;
  370. exit;
  371. end; }
  372. '+' : begin
  373. actasmtoken := AS_PLUS;
  374. c:=current_scanner.asmgetchar;
  375. exit;
  376. end;
  377. '-' : begin
  378. actasmtoken := AS_MINUS;
  379. c:=current_scanner.asmgetchar;
  380. exit;
  381. end;
  382. '*' : begin
  383. actasmtoken := AS_STAR;
  384. c:=current_scanner.asmgetchar;
  385. exit;
  386. end;
  387. '/' : begin
  388. actasmtoken := AS_SLASH;
  389. c:=current_scanner.asmgetchar;
  390. exit;
  391. end;
  392. '<' : begin
  393. c := current_scanner.asmgetchar;
  394. { invalid characters }
  395. if c <> '<' then
  396. Message(asmr_e_invalid_char_smaller);
  397. { still assume << }
  398. actasmtoken := AS_SHL;
  399. c := current_scanner.asmgetchar;
  400. exit;
  401. end;
  402. '>' : begin
  403. c := current_scanner.asmgetchar;
  404. { invalid characters }
  405. if c <> '>' then
  406. Message(asmr_e_invalid_char_greater);
  407. { still assume << }
  408. actasmtoken := AS_SHR;
  409. c := current_scanner.asmgetchar;
  410. exit;
  411. end;
  412. '|' : begin
  413. actasmtoken := AS_OR;
  414. c := current_scanner.asmgetchar;
  415. exit;
  416. end;
  417. '^' : begin
  418. actasmtoken := AS_XOR;
  419. c := current_scanner.asmgetchar;
  420. exit;
  421. end;
  422. '#' : begin
  423. actasmtoken:=AS_APPT;
  424. c:=current_scanner.asmgetchar;
  425. exit;
  426. end;
  427. '%' : begin
  428. c:=current_scanner.asmgetchar;
  429. while c in ['0','1'] do
  430. begin
  431. actasmpattern := actasmpattern + c;
  432. c := current_scanner.asmgetchar;
  433. end;
  434. actasmtoken := AS_BINNUM;
  435. exit;
  436. end;
  437. { integer number }
  438. '0'..'9': begin
  439. actasmpattern := c;
  440. c := current_scanner.asmgetchar;
  441. while c in ['0'..'9'] do
  442. begin
  443. actasmpattern := actasmpattern + c;
  444. c:= current_scanner.asmgetchar;
  445. end;
  446. actasmtoken := AS_INTNUM;
  447. exit;
  448. end;
  449. ';' : begin
  450. repeat
  451. c:=current_scanner.asmgetchar;
  452. until c=#10;
  453. firsttoken := TRUE;
  454. actasmtoken:=AS_SEPARATOR;
  455. end;
  456. '{',#13,#10 : begin
  457. c:=current_scanner.asmgetchar;
  458. firsttoken := TRUE;
  459. actasmtoken:=AS_SEPARATOR;
  460. end;
  461. else
  462. begin
  463. s:=c;
  464. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  465. end;
  466. end; { end case }
  467. end; { end else if }
  468. end;
  469. {---------------------------------------------------------------------}
  470. { Routines for the parsing }
  471. {---------------------------------------------------------------------}
  472. function tm68kmotreader.consume(t : tasmtoken):boolean;
  473. var
  474. p: pointer;
  475. begin
  476. Consume:=true;
  477. if t<>actasmtoken then
  478. begin
  479. p:=nil;
  480. dword(p^):=0;
  481. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  482. Consume:=false;
  483. end;
  484. repeat
  485. gettoken;
  486. until actasmtoken<>AS_NONE;
  487. end;
  488. function tm68kmotreader.findopcode(s: string; var opsize: topsize): tasmop;
  489. {*********************************************************************}
  490. { FUNCTION findopcode(s: string): tasmop; }
  491. { Description: Determines if the s string is a valid opcode }
  492. { if so returns correct tasmop token. }
  493. {*********************************************************************}
  494. var
  495. j: byte;
  496. op_size: string;
  497. begin
  498. findopcode := A_NONE;
  499. j:=pos('.',s);
  500. if j<>0 then
  501. begin
  502. op_size:=copy(s,j+1,1);
  503. case op_size[1] of
  504. { For the motorola only opsize size is used to }
  505. { determine the size of the operands. }
  506. 'B': opsize := S_B;
  507. 'W': opsize := S_W;
  508. 'L': opsize := S_L;
  509. 'S': opsize := S_FS;
  510. 'D': opsize := S_FD;
  511. 'X': opsize := S_FX;
  512. else
  513. Message1(asmr_e_unknown_opcode,s);
  514. end;
  515. { delete everything starting from dot }
  516. delete(s,j,length(s));
  517. end;
  518. result:=actopcode;
  519. end;
  520. Function tm68kmotreader.BuildExpression(allow_symbol : boolean; asmsym : pshortstring) : longint;
  521. {*********************************************************************}
  522. { FUNCTION BuildExpression: longint }
  523. { Description: This routine calculates a constant expression to }
  524. { a given value. The return value is the value calculated from }
  525. { the expression. }
  526. { The following tokens (not strings) are recognized: }
  527. { (,),SHL,SHR,/,*,NOT,OR,XOR,AND,MOD,+/-,numbers,ID to constants. }
  528. {*********************************************************************}
  529. { ENTRY: On entry the token should be any valid expression token. }
  530. { EXIT: On Exit the token points to either COMMA or SEPARATOR }
  531. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  532. { invalid tokens. }
  533. {*********************************************************************}
  534. var expr: string;
  535. hs, tempstr: string;
  536. sym : tsym;
  537. srsymtable : TSymtable;
  538. hl : tasmlabel;
  539. l : longint;
  540. errorflag: boolean;
  541. begin
  542. errorflag := FALSE;
  543. expr := '';
  544. tempstr := '';
  545. if allow_symbol then
  546. asmsym^:='';
  547. Repeat
  548. Case actasmtoken of
  549. AS_LPAREN: begin
  550. Consume(AS_LPAREN);
  551. expr := expr + '(';
  552. end;
  553. AS_RPAREN: begin
  554. Consume(AS_RPAREN);
  555. expr := expr + ')';
  556. end;
  557. AS_SHL: begin
  558. Consume(AS_SHL);
  559. expr := expr + '<';
  560. end;
  561. AS_SHR: begin
  562. Consume(AS_SHR);
  563. expr := expr + '>';
  564. end;
  565. AS_SLASH: begin
  566. Consume(AS_SLASH);
  567. expr := expr + '/';
  568. end;
  569. AS_MOD: begin
  570. Consume(AS_MOD);
  571. expr := expr + '%';
  572. end;
  573. AS_STAR: begin
  574. Consume(AS_STAR);
  575. expr := expr + '*';
  576. end;
  577. AS_PLUS: begin
  578. Consume(AS_PLUS);
  579. expr := expr + '+';
  580. end;
  581. AS_MINUS: begin
  582. Consume(AS_MINUS);
  583. expr := expr + '-';
  584. end;
  585. AS_AND: begin
  586. Consume(AS_AND);
  587. expr := expr + '&';
  588. end;
  589. AS_NOT: begin
  590. Consume(AS_NOT);
  591. expr := expr + '~';
  592. end;
  593. AS_XOR: begin
  594. Consume(AS_XOR);
  595. expr := expr + '^';
  596. end;
  597. AS_OR: begin
  598. Consume(AS_OR);
  599. expr := expr + '|';
  600. end;
  601. AS_ID: begin
  602. if SearchIConstant(actasmpattern,l) then
  603. begin
  604. str(l, tempstr);
  605. expr := expr + tempstr;
  606. Consume(AS_ID);
  607. End else
  608. if not allow_symbol then
  609. begin
  610. Message(asmr_e_syn_constant);
  611. l := 0;
  612. End else
  613. begin
  614. hs:='';
  615. if (expr[Length(expr)]='+') then
  616. Delete(expr,Length(expr),1)
  617. else if expr<>'' then
  618. begin
  619. Message(asmr_e_invalid_constant_expression);
  620. break;
  621. End;
  622. tempstr:=actasmpattern;
  623. consume(AS_ID);
  624. if (length(tempstr)>1) and (tempstr[1]='@') then
  625. begin
  626. CreateLocalLabel(tempstr,hl,false);
  627. hs:=hl.name
  628. end
  629. else if SearchLabel(tempstr,hl,false) then
  630. hs:=hl.name
  631. else
  632. begin
  633. asmsearchsym(tempstr,sym,srsymtable);
  634. if assigned(sym) then
  635. begin
  636. case sym.typ of
  637. paravarsym,
  638. localvarsym :
  639. begin
  640. Message(asmr_e_no_local_or_para_allowed);
  641. hs:=tabstractvarsym(sym).mangledname;
  642. end;
  643. staticvarsym :
  644. hs:=tstaticvarsym(sym).mangledname;
  645. procsym :
  646. begin
  647. if tprocsym(sym).procdeflist.count>1 then
  648. Message(asmr_w_calling_overload_func);
  649. hs:=tprocdef(tprocsym(sym).procdeflist[0]).mangledname;
  650. end;
  651. typesym :
  652. begin
  653. if not(ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  654. Message(asmr_e_wrong_sym_type);
  655. end;
  656. else
  657. Message(asmr_e_wrong_sym_type);
  658. end;
  659. end
  660. else
  661. Message1(sym_e_unknown_id,tempstr);
  662. end;
  663. { symbol found? }
  664. if hs<>'' then
  665. begin
  666. if asmsym^='' then
  667. asmsym^:=hs
  668. else
  669. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  670. end;
  671. end;
  672. end;
  673. AS_INTNUM: begin
  674. expr := expr + actasmpattern;
  675. Consume(AS_INTNUM);
  676. end;
  677. AS_BINNUM: begin
  678. tempstr := tostr(ParseVal(actasmpattern,2));
  679. if tempstr = '' then
  680. Message(asmr_e_error_converting_binary);
  681. expr:=expr+tempstr;
  682. Consume(AS_BINNUM);
  683. end;
  684. AS_HEXNUM: begin
  685. tempstr := tostr(ParseVal(actasmpattern,16));
  686. if tempstr = '' then
  687. Message(asmr_e_error_converting_hexadecimal);
  688. expr:=expr+tempstr;
  689. Consume(AS_HEXNUM);
  690. end;
  691. AS_OCTALNUM: begin
  692. tempstr := tostr(ParseVal(actasmpattern,8));
  693. if tempstr = '' then
  694. Message(asmr_e_error_converting_octal);
  695. expr:=expr+tempstr;
  696. Consume(AS_OCTALNUM);
  697. end;
  698. { go to next term }
  699. AS_COMMA: begin
  700. if not ErrorFlag then
  701. BuildExpression := CalculateExpression(expr)
  702. else
  703. BuildExpression := 0;
  704. Exit;
  705. end;
  706. { go to next symbol }
  707. AS_SEPARATOR: begin
  708. if not ErrorFlag then
  709. BuildExpression := CalculateExpression(expr)
  710. else
  711. BuildExpression := 0;
  712. Exit;
  713. end;
  714. else
  715. begin
  716. { only write error once. }
  717. if not errorflag then
  718. Message(asmr_e_invalid_constant_expression);
  719. { consume tokens until we find COMMA or SEPARATOR }
  720. Consume(actasmtoken);
  721. errorflag := TRUE;
  722. End;
  723. end;
  724. Until false;
  725. end;
  726. Procedure tm68kmotreader.BuildRealConstant(typ : tfloattype);
  727. {*********************************************************************}
  728. { PROCEDURE BuilRealConst }
  729. { Description: This routine calculates a constant expression to }
  730. { a given value. The return value is the value calculated from }
  731. { the expression. }
  732. { The following tokens (not strings) are recognized: }
  733. { +/-,numbers and real numbers }
  734. {*********************************************************************}
  735. { ENTRY: On entry the token should be any valid expression token. }
  736. { EXIT: On Exit the token points to either COMMA or SEPARATOR }
  737. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  738. { invalid tokens. }
  739. {*********************************************************************}
  740. var expr: string;
  741. r : extended;
  742. code : word;
  743. negativ : boolean;
  744. errorflag: boolean;
  745. begin
  746. errorflag := FALSE;
  747. Repeat
  748. negativ:=false;
  749. expr := '';
  750. if actasmtoken=AS_PLUS then Consume(AS_PLUS)
  751. else if actasmtoken=AS_MINUS then
  752. begin
  753. negativ:=true;
  754. consume(AS_MINUS);
  755. end;
  756. Case actasmtoken of
  757. AS_INTNUM: begin
  758. expr := actasmpattern;
  759. Consume(AS_INTNUM);
  760. end;
  761. AS_REALNUM: begin
  762. expr := actasmpattern;
  763. { in ATT syntax you have 0d in front of the real }
  764. { should this be forced ? yes i think so, as to }
  765. { conform to gas as much as possible. }
  766. if (expr[1]='0') and (upper(expr[2])='D') then
  767. expr:=copy(expr,3,255);
  768. Consume(AS_REALNUM);
  769. end;
  770. AS_BINNUM: begin
  771. { checking for real constants with this should use }
  772. { real DECODING otherwise the compiler will crash! }
  773. Message(asmr_e_invalid_float_expr);
  774. expr:='0.0';
  775. Consume(AS_BINNUM);
  776. end;
  777. AS_HEXNUM: begin
  778. { checking for real constants with this should use }
  779. { real DECODING otherwise the compiler will crash! }
  780. Message(asmr_e_invalid_float_expr);
  781. expr:='0.0';
  782. Consume(AS_HEXNUM);
  783. end;
  784. AS_OCTALNUM: begin
  785. { checking for real constants with this should use }
  786. { real DECODING otherwise the compiler will crash! }
  787. { xxxToDec using reals could be a solution, but the }
  788. { problem is that these will crash the m68k compiler }
  789. { when compiling -- because of lack of good fpu }
  790. { support. }
  791. Message(asmr_e_invalid_float_expr);
  792. expr:='0.0';
  793. Consume(AS_OCTALNUM);
  794. end;
  795. else
  796. begin
  797. { only write error once. }
  798. if not errorflag then
  799. Message(asmr_e_invalid_float_expr);
  800. { consume tokens until we find COMMA or SEPARATOR }
  801. Consume(actasmtoken);
  802. errorflag := TRUE;
  803. End;
  804. end;
  805. { go to next term }
  806. if (actasmtoken=AS_COMMA) or (actasmtoken=AS_SEPARATOR) then
  807. begin
  808. if negativ then expr:='-'+expr;
  809. val(expr,r,code);
  810. if code<>0 then
  811. begin
  812. r:=0;
  813. Message(asmr_e_invalid_float_expr);
  814. ConcatRealConstant(curlist,r,typ);
  815. End
  816. else
  817. begin
  818. ConcatRealConstant(curlist,r,typ);
  819. End;
  820. end
  821. else
  822. Message(asmr_e_invalid_float_expr);
  823. Until actasmtoken=AS_SEPARATOR;
  824. end;
  825. Procedure tm68kmotreader.BuildConstant(maxvalue: longint);
  826. {*********************************************************************}
  827. { PROCEDURE BuildConstant }
  828. { Description: This routine takes care of parsing a DB,DD,or DW }
  829. { line and adding those to the assembler node. Expressions, range- }
  830. { checking are fullly taken care of. }
  831. { maxvalue: $ff -> indicates that this is a DB node. }
  832. { $ffff -> indicates that this is a DW node. }
  833. { $ffffffff -> indicates that this is a DD node. }
  834. {*********************************************************************}
  835. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  836. {*********************************************************************}
  837. var
  838. expr: string;
  839. value : longint;
  840. begin
  841. Repeat
  842. Case actasmtoken of
  843. AS_STRING: begin
  844. if maxvalue <> $ff then
  845. Message(asmr_e_string_not_allowed_as_const);
  846. expr := actasmpattern;
  847. if length(expr) > 1 then
  848. Message(asmr_e_string_not_allowed_as_const);
  849. Consume(AS_STRING);
  850. Case actasmtoken of
  851. AS_COMMA: Consume(AS_COMMA);
  852. AS_SEPARATOR: ;
  853. else
  854. Message(asmr_e_invalid_string_expression);
  855. end; { end case }
  856. ConcatString(curlist,expr);
  857. end;
  858. AS_INTNUM,AS_BINNUM,
  859. AS_OCTALNUM,AS_HEXNUM:
  860. begin
  861. value:=BuildExpression(false,nil);
  862. ConcatConstant(curlist,value,maxvalue);
  863. end;
  864. AS_ID:
  865. begin
  866. value:=BuildExpression(false,nil);
  867. if value > maxvalue then
  868. begin
  869. Message(asmr_e_constant_out_of_bounds);
  870. { assuming a value of maxvalue }
  871. value := maxvalue;
  872. end;
  873. ConcatConstant(curlist,value,maxvalue);
  874. end;
  875. { These terms can start an assembler expression }
  876. AS_PLUS,AS_MINUS,AS_LPAREN,AS_NOT: begin
  877. value := BuildExpression(false,nil);
  878. ConcatConstant(curlist,value,maxvalue);
  879. end;
  880. AS_COMMA: begin
  881. Consume(AS_COMMA);
  882. END;
  883. AS_SEPARATOR: ;
  884. else
  885. begin
  886. Message(asmr_e_syntax_error);
  887. end;
  888. end; { end case }
  889. Until actasmtoken = AS_SEPARATOR;
  890. end;
  891. Procedure TM68kMotreader.BuildScaling(const oper:tm68koperand);
  892. {*********************************************************************}
  893. { Takes care of parsing expression starting from the scaling value }
  894. { up to and including possible field specifiers. }
  895. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR }
  896. { or AS_COMMA. On entry should point to the AS_STAR token. }
  897. {*********************************************************************}
  898. var str:string;
  899. l: longint;
  900. code: integer;
  901. begin
  902. Consume(AS_STAR);
  903. if (oper.opr.ref.scalefactor <> 0)
  904. and (oper.opr.ref.scalefactor <> 1) then
  905. Message(asmr_e_wrong_base_index);
  906. case actasmtoken of
  907. AS_INTNUM: str := actasmpattern;
  908. AS_HEXNUM: str := Tostr(ParseVal(actasmpattern,16));
  909. AS_BINNUM: str := Tostr(ParseVal(actasmpattern,2));
  910. AS_OCTALNUM: str := Tostr(ParseVal(actasmpattern,8));
  911. else
  912. Message(asmr_e_syntax_error);
  913. end;
  914. val(str, l, code);
  915. if code <> 0 then
  916. Message(asmr_e_wrong_scale_factor);
  917. if ((l = 2) or (l = 4) or (l = 8) or (l = 1)) and (code = 0) then
  918. begin
  919. oper.opr.ref.scalefactor := l;
  920. end
  921. else
  922. begin
  923. Message(asmr_e_wrong_scale_factor);
  924. oper.opr.ref.scalefactor := 0;
  925. end;
  926. if oper.opr.ref.index = NR_NO then
  927. begin
  928. Message(asmr_e_wrong_base_index);
  929. oper.opr.ref.scalefactor := 0;
  930. end;
  931. { Consume the scaling number }
  932. Consume(actasmtoken);
  933. if actasmtoken = AS_RPAREN then
  934. Consume(AS_RPAREN)
  935. else
  936. Message(asmr_e_wrong_scale_factor);
  937. { // .Field.Field ... or separator/comma // }
  938. if actasmtoken in [AS_COMMA,AS_SEPARATOR] then
  939. begin
  940. end
  941. else
  942. Message(asmr_e_syntax_error);
  943. end;
  944. Function TM68kMotreader.BuildRefExpression: longint;
  945. {*********************************************************************}
  946. { FUNCTION BuildRefExpression: longint }
  947. { Description: This routine calculates a constant expression to }
  948. { a given value. The return value is the value calculated from }
  949. { the expression. }
  950. { The following tokens (not strings) are recognized: }
  951. { SHL,SHR,/,*,NOT,OR,XOR,AND,MOD,+/-,numbers,ID to constants. }
  952. {*********************************************************************}
  953. { ENTRY: On entry the token should be any valid expression token. }
  954. { EXIT: On Exit the token points to the LPAREN token. }
  955. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  956. { invalid tokens. }
  957. {*********************************************************************}
  958. var tempstr: string;
  959. expr: string;
  960. l : longint;
  961. errorflag : boolean;
  962. begin
  963. errorflag := FALSE;
  964. tempstr := '';
  965. expr := '';
  966. Repeat
  967. Case actasmtoken of
  968. AS_RPAREN: begin
  969. Message(asmr_e_syntax_error);
  970. Consume(AS_RPAREN);
  971. end;
  972. AS_SHL: begin
  973. Consume(AS_SHL);
  974. expr := expr + '<';
  975. end;
  976. AS_SHR: begin
  977. Consume(AS_SHR);
  978. expr := expr + '>';
  979. end;
  980. AS_SLASH: begin
  981. Consume(AS_SLASH);
  982. expr := expr + '/';
  983. end;
  984. AS_MOD: begin
  985. Consume(AS_MOD);
  986. expr := expr + '%';
  987. end;
  988. AS_STAR: begin
  989. Consume(AS_STAR);
  990. expr := expr + '*';
  991. end;
  992. AS_PLUS: begin
  993. Consume(AS_PLUS);
  994. expr := expr + '+';
  995. end;
  996. AS_MINUS: begin
  997. Consume(AS_MINUS);
  998. expr := expr + '-';
  999. end;
  1000. AS_AND: begin
  1001. Consume(AS_AND);
  1002. expr := expr + '&';
  1003. end;
  1004. AS_NOT: begin
  1005. Consume(AS_NOT);
  1006. expr := expr + '~';
  1007. end;
  1008. AS_XOR: begin
  1009. Consume(AS_XOR);
  1010. expr := expr + '^';
  1011. end;
  1012. AS_OR: begin
  1013. Consume(AS_OR);
  1014. expr := expr + '|';
  1015. end;
  1016. { End of reference }
  1017. AS_LPAREN: begin
  1018. if not ErrorFlag then
  1019. BuildRefExpression := CalculateExpression(expr)
  1020. else
  1021. BuildRefExpression := 0;
  1022. { no longer in an expression }
  1023. exit;
  1024. end;
  1025. AS_ID:
  1026. begin
  1027. if NOT SearchIConstant(actasmpattern,l) then
  1028. begin
  1029. Message(asmr_e_syn_constant);
  1030. l := 0;
  1031. end;
  1032. str(l, tempstr);
  1033. expr := expr + tempstr;
  1034. Consume(AS_ID);
  1035. end;
  1036. AS_INTNUM: begin
  1037. expr := expr + actasmpattern;
  1038. Consume(AS_INTNUM);
  1039. end;
  1040. AS_BINNUM: begin
  1041. tempstr := Tostr(ParseVal(actasmpattern,2));
  1042. if tempstr = '' then
  1043. Message(asmr_e_error_converting_binary);
  1044. expr:=expr+tempstr;
  1045. Consume(AS_BINNUM);
  1046. end;
  1047. AS_HEXNUM: begin
  1048. tempstr := Tostr(ParseVal(actasmpattern,16));
  1049. if tempstr = '' then
  1050. Message(asmr_e_error_converting_hexadecimal);
  1051. expr:=expr+tempstr;
  1052. Consume(AS_HEXNUM);
  1053. end;
  1054. AS_OCTALNUM: begin
  1055. tempstr := Tostr(ParseVal(actasmpattern,8));
  1056. if tempstr = '' then
  1057. Message(asmr_e_error_converting_octal);
  1058. expr:=expr+tempstr;
  1059. Consume(AS_OCTALNUM);
  1060. end;
  1061. else
  1062. begin
  1063. { write error only once. }
  1064. if not errorflag then
  1065. Message(asmr_e_invalid_constant_expression);
  1066. BuildRefExpression := 0;
  1067. if actasmtoken in [AS_COMMA,AS_SEPARATOR] then exit;
  1068. { consume tokens until we find COMMA or SEPARATOR }
  1069. Consume(actasmtoken);
  1070. errorflag := TRUE;
  1071. end;
  1072. end;
  1073. Until false;
  1074. end;
  1075. {*********************************************************************}
  1076. { PROCEDURE BuildBracketExpression }
  1077. { Description: This routine builds up an expression after a LPAREN }
  1078. { token is encountered. }
  1079. { On entry actasmtoken should be equal to AS_LPAREN }
  1080. {*********************************************************************}
  1081. { EXIT CONDITION: On exit the routine should point to either the }
  1082. { AS_COMMA or AS_SEPARATOR token. }
  1083. {*********************************************************************}
  1084. procedure TM68kMotreader.BuildReference(const oper:tm68koperand);
  1085. var
  1086. l:longint;
  1087. code: integer;
  1088. str: string;
  1089. begin
  1090. Consume(AS_LPAREN);
  1091. case actasmtoken of
  1092. { // (reg ... // }
  1093. AS_REGISTER:
  1094. begin
  1095. oper.opr.ref.base := actasmregister;
  1096. Consume(AS_REGISTER);
  1097. { can either be a register or a right parenthesis }
  1098. { // (reg) // }
  1099. { // (reg)+ // }
  1100. if actasmtoken=AS_RPAREN then
  1101. begin
  1102. Consume(AS_RPAREN);
  1103. if actasmtoken = AS_PLUS then
  1104. begin
  1105. if (oper.opr.ref.direction <> dir_none) then
  1106. Message(asmr_e_no_inc_and_dec_together)
  1107. else
  1108. oper.opr.ref.direction := dir_inc;
  1109. Consume(AS_PLUS);
  1110. end;
  1111. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR]) then
  1112. begin
  1113. Message(asmr_e_invalid_reference_syntax);
  1114. { error recovery ... }
  1115. while actasmtoken <> AS_SEPARATOR do
  1116. Consume(actasmtoken);
  1117. end;
  1118. exit;
  1119. end;
  1120. { // (reg,reg .. // }
  1121. Consume(AS_COMMA);
  1122. if actasmtoken = AS_REGISTER then
  1123. begin
  1124. oper.opr.ref.index :=
  1125. actasmregister;
  1126. Consume(AS_REGISTER);
  1127. { check for scaling ... }
  1128. case actasmtoken of
  1129. AS_RPAREN:
  1130. begin
  1131. Consume(AS_RPAREN);
  1132. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR]) then
  1133. begin
  1134. { error recovery ... }
  1135. Message(asmr_e_invalid_reference_syntax);
  1136. while actasmtoken <> AS_SEPARATOR do
  1137. Consume(actasmtoken);
  1138. end;
  1139. exit;
  1140. end;
  1141. AS_STAR:
  1142. begin
  1143. BuildScaling(oper);
  1144. end;
  1145. else
  1146. begin
  1147. Message(asmr_e_invalid_reference_syntax);
  1148. while (actasmtoken <> AS_SEPARATOR) do
  1149. Consume(actasmtoken);
  1150. end;
  1151. end; { end case }
  1152. end
  1153. else
  1154. begin
  1155. Message(asmr_e_invalid_reference_syntax);
  1156. while (actasmtoken <> AS_SEPARATOR) do
  1157. Consume(actasmtoken);
  1158. end;
  1159. end;
  1160. AS_HEXNUM,AS_OCTALNUM, { direct address }
  1161. AS_BINNUM,AS_INTNUM:
  1162. begin
  1163. case actasmtoken of
  1164. AS_INTNUM: str := actasmpattern;
  1165. AS_HEXNUM: str := Tostr(ParseVal(actasmpattern,16));
  1166. AS_BINNUM: str := Tostr(ParseVal(actasmpattern,2));
  1167. AS_OCTALNUM: str := Tostr(ParseVal(actasmpattern,8));
  1168. else
  1169. Message(asmr_e_syntax_error);
  1170. end;
  1171. Consume(actasmtoken);
  1172. val(str, l, code);
  1173. if code <> 0 then
  1174. Message(asmr_e_invalid_reference_syntax)
  1175. else
  1176. oper.opr.ref.offset := l;
  1177. Consume(AS_RPAREN);
  1178. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR]) then
  1179. begin
  1180. { error recovery ... }
  1181. Message(asmr_e_invalid_reference_syntax);
  1182. while actasmtoken <> AS_SEPARATOR do
  1183. Consume(actasmtoken);
  1184. end;
  1185. exit;
  1186. end;
  1187. else
  1188. begin
  1189. Message(asmr_e_invalid_reference_syntax);
  1190. while (actasmtoken <> AS_SEPARATOR) do
  1191. Consume(actasmtoken);
  1192. end;
  1193. end;
  1194. end;
  1195. Procedure TM68kMotreader.BuildOperand(const oper:tm68koperand);
  1196. {*********************************************************************}
  1197. { EXIT CONDITION: On exit the routine should point to either the }
  1198. { AS_COMMA or AS_SEPARATOR token. }
  1199. {*********************************************************************}
  1200. var
  1201. expr: string;
  1202. tempstr: string;
  1203. lab: tasmlabel;
  1204. l : longint;
  1205. i: Tsuperregister;
  1206. r:Tregister;
  1207. hl: tasmlabel;
  1208. reg_one, reg_two: tregister;
  1209. regset: tcpuregisterset;
  1210. p: pointer;
  1211. begin
  1212. regset := [];
  1213. tempstr := '';
  1214. case actasmtoken of
  1215. { // Memory reference // }
  1216. AS_LPAREN:
  1217. begin
  1218. Oper.InitRef;
  1219. BuildReference(oper);
  1220. end;
  1221. { // Constant expression // }
  1222. AS_APPT: begin
  1223. Consume(AS_APPT);
  1224. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1225. Message(asmr_e_invalid_operand_type);
  1226. { identifiers are handled by BuildExpression }
  1227. oper.opr.typ := OPR_CONSTANT;
  1228. oper.opr.val :=BuildExpression(true,@tempstr);
  1229. if tempstr<>'' then
  1230. begin
  1231. l:=oper.opr.val;
  1232. oper.opr.typ := OPR_SYMBOL;
  1233. oper.opr.symofs := l;
  1234. oper.opr.symbol := current_asmdata.RefAsmSymbol(tempstr);
  1235. end;
  1236. end;
  1237. { // Constant memory offset . // }
  1238. { // This must absolutely be followed by ( // }
  1239. AS_HEXNUM,AS_INTNUM,
  1240. AS_BINNUM,AS_OCTALNUM,AS_PLUS:
  1241. begin
  1242. Oper.InitRef;
  1243. oper.opr.ref.offset:=BuildRefExpression;
  1244. BuildReference(oper);
  1245. end;
  1246. { // A constant expression, or a Variable ref. // }
  1247. AS_ID: begin
  1248. Oper.InitRef;
  1249. if actasmpattern[1] = '@' then
  1250. { // Label or Special symbol reference // }
  1251. begin
  1252. if actasmpattern = '@RESULT' then
  1253. oper.SetUpResult
  1254. else
  1255. if actasmpattern = 'SELF' then
  1256. oper.SetUpSelf
  1257. else
  1258. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  1259. Message(asmr_w_CODE_and_DATA_not_supported)
  1260. else
  1261. begin
  1262. delete(actasmpattern,1,1);
  1263. if actasmpattern = '' then
  1264. Message(asmr_e_null_label_ref_not_allowed);
  1265. CreateLocalLabel(actasmpattern,lab,false);
  1266. oper.opr.typ := OPR_SYMBOL;
  1267. oper.opr.symbol := lab;
  1268. oper.opr.symofs := 0;
  1269. // labeled := TRUE;
  1270. end;
  1271. Consume(AS_ID);
  1272. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1273. Message(asmr_e_syntax_error);
  1274. end
  1275. { probably a variable or normal expression }
  1276. { or a procedure (such as in CALL ID) }
  1277. else
  1278. begin
  1279. { is it a constant ? }
  1280. if SearchIConstant(actasmpattern,l) then
  1281. begin
  1282. Oper.InitRef;
  1283. oper.opr.ref.offset:=BuildRefExpression;
  1284. BuildReference(oper);
  1285. end
  1286. else { is it a label variable ? }
  1287. { // ID[ , ID.Field.Field or simple ID // }
  1288. { check if this is a label, if so then }
  1289. { emit it as a label. }
  1290. if SearchLabel(actasmpattern,hl,false) then
  1291. begin
  1292. oper.opr.typ := OPR_SYMBOL;
  1293. oper.opr.symbol := hl;
  1294. oper.opr.symofs := 0;
  1295. // labeled := TRUE;
  1296. Consume(AS_ID);
  1297. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1298. Message(asmr_e_syntax_error);
  1299. end
  1300. else begin
  1301. expr:=actasmpattern;
  1302. Consume(AS_ID);
  1303. { typecasting? }
  1304. if SearchType(expr,l) then
  1305. begin
  1306. oper.hastype:=true;
  1307. oper.typesize:=l;
  1308. case actasmtoken of
  1309. AS_LPAREN :
  1310. begin
  1311. { Support Type([Reference]) }
  1312. Consume(AS_LPAREN);
  1313. BuildOperand(oper{,true});
  1314. Consume(AS_RPAREN);
  1315. end;
  1316. AS_LBRACKET :
  1317. begin
  1318. { Support Var.Type[Index] }
  1319. { Convert @label.Byte[1] to reference }
  1320. if oper.opr.typ=OPR_SYMBOL then
  1321. oper.initref;
  1322. end;
  1323. end;
  1324. end
  1325. else
  1326. begin
  1327. if not oper.SetupVar(expr,false) then
  1328. begin
  1329. { not a variable, check special variables.. }
  1330. if expr = 'SELF' then
  1331. oper.SetupSelf
  1332. else begin
  1333. writeln('unknown id: ',expr);
  1334. Message1(sym_e_unknown_id,expr);
  1335. end;
  1336. expr:='';
  1337. end;
  1338. end;
  1339. // Message1(sym_e_unknown_id,actasmpattern);
  1340. end;
  1341. case actasmtoken of
  1342. AS_LPAREN: { indexing }
  1343. BuildReference(oper);
  1344. AS_SEPARATOR,AS_COMMA: begin
  1345. end;
  1346. else
  1347. Message(asmr_e_syntax_error);
  1348. end;
  1349. end;
  1350. end;
  1351. { // Pre-decrement mode reference or constant mem offset. // }
  1352. AS_MINUS: begin
  1353. Consume(AS_MINUS);
  1354. if actasmtoken = AS_LPAREN then
  1355. begin
  1356. Oper.InitRef;
  1357. { indicate pre-decrement mode }
  1358. oper.opr.ref.direction := dir_dec;
  1359. BuildReference(oper);
  1360. end
  1361. else
  1362. if actasmtoken in [AS_OCTALNUM,AS_HEXNUM,AS_BINNUM,AS_INTNUM] then
  1363. begin
  1364. Oper.InitRef;
  1365. oper.opr.ref.offset:=BuildRefExpression;
  1366. { negate because was preceded by a negative sign! }
  1367. oper.opr.ref.offset:=-oper.opr.ref.offset;
  1368. BuildReference(oper);
  1369. end
  1370. else
  1371. begin
  1372. Message(asmr_e_syntax_error);
  1373. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1374. Consume(actasmtoken);
  1375. end;
  1376. end;
  1377. { // Register, a variable reference or a constant reference // }
  1378. AS_REGISTER: begin
  1379. // writeln('register! ',actasmpattern);
  1380. { save the type of register used. }
  1381. tempstr := actasmpattern;
  1382. Consume(AS_REGISTER);
  1383. { // Simple register // }
  1384. if (actasmtoken = AS_SEPARATOR) or (actasmtoken = AS_COMMA) then
  1385. begin
  1386. // writeln('simple reg');
  1387. if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
  1388. Message(asmr_e_invalid_operand_type);
  1389. oper.opr.typ := OPR_REGISTER;
  1390. oper.opr.reg := actasmregister;
  1391. end
  1392. else
  1393. { HERE WE MUST HANDLE THE SPECIAL CASE OF MOVEM AND FMOVEM }
  1394. { // Individual register listing // }
  1395. if (actasmtoken = AS_SLASH) then
  1396. begin
  1397. r:=actasmregister;
  1398. if getregtype(r)<>R_INTREGISTER then
  1399. internalerror(200302191);
  1400. include(regset,getsupreg(r));
  1401. Consume(AS_SLASH);
  1402. if actasmtoken = AS_REGISTER then
  1403. begin
  1404. While not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1405. begin
  1406. case actasmtoken of
  1407. AS_REGISTER: begin
  1408. if getregtype(r)<>R_INTREGISTER then
  1409. internalerror(200302191);
  1410. include(regset,getsupreg(r));
  1411. Consume(AS_REGISTER);
  1412. end;
  1413. AS_SLASH: Consume(AS_SLASH);
  1414. AS_SEPARATOR,AS_COMMA: break;
  1415. else
  1416. begin
  1417. Message(asmr_e_invalid_reg_list_in_movem);
  1418. Consume(actasmtoken);
  1419. end;
  1420. end; { end case }
  1421. end; { end while }
  1422. oper.opr.typ:= OPR_regset;
  1423. oper.opr.regset := regset;
  1424. end
  1425. else
  1426. { error recovery ... }
  1427. begin
  1428. Message(asmr_e_invalid_reg_list_in_movem);
  1429. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1430. Consume(actasmtoken);
  1431. end;
  1432. end
  1433. else
  1434. { // Range register listing // }
  1435. if (actasmtoken = AS_MINUS) then
  1436. begin
  1437. Consume(AS_MINUS);
  1438. reg_one:=actasmregister;
  1439. if actasmtoken <> AS_REGISTER then
  1440. begin
  1441. Message(asmr_e_invalid_reg_list_in_movem);
  1442. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1443. Consume(actasmtoken);
  1444. end
  1445. else
  1446. begin
  1447. { determine the register range ... }
  1448. reg_two:=actasmregister;
  1449. if getregtype(reg_two)<>R_INTREGISTER then
  1450. internalerror(200302191);
  1451. if getsupreg(reg_one) > getsupreg(reg_two) then
  1452. for i:=getsupreg(reg_two) to getsupreg(reg_one) do
  1453. include(regset,i)
  1454. else
  1455. for i:=getsupreg(reg_one) to getsupreg(reg_two) do
  1456. include(regset,i);
  1457. Consume(AS_REGISTER);
  1458. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1459. begin
  1460. Message(asmr_e_invalid_reg_list_in_movem);
  1461. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1462. Consume(actasmtoken);
  1463. end;
  1464. { set up instruction }
  1465. oper.opr.typ:= OPR_regset;
  1466. oper.opr.regset := regset;
  1467. end;
  1468. end
  1469. else
  1470. { DIVSL/DIVS/MULS/MULU with long for MC68020 only }
  1471. if (actasmtoken = AS_COLON) then
  1472. begin
  1473. if (current_settings.cputype = cpu_MC68020) or (cs_compilesystem in current_settings.moduleswitches) then
  1474. begin
  1475. Consume(AS_COLON);
  1476. if (actasmtoken = AS_REGISTER) then
  1477. begin
  1478. { set up old field, since register is valid }
  1479. oper.opr.typ := OPR_REGISTER;
  1480. oper.opr.reg := actasmregister;
  1481. Inc(operandnum);
  1482. oper.opr.typ := OPR_REGISTER;
  1483. oper.opr.reg := actasmregister;
  1484. Consume(AS_REGISTER);
  1485. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1486. begin
  1487. Message(asmr_e_invalid_reg_list_for_opcode);
  1488. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1489. Consume(actasmtoken);
  1490. end;
  1491. end;
  1492. end
  1493. else
  1494. begin
  1495. Message1(asmr_e_higher_cpu_mode_required,'68020');
  1496. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1497. begin
  1498. Message(asmr_e_invalid_reg_list_for_opcode);
  1499. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1500. Consume(actasmtoken);
  1501. end;
  1502. end;
  1503. end
  1504. else
  1505. Message(asmr_e_invalid_register);
  1506. end;
  1507. AS_SEPARATOR, AS_COMMA: ;
  1508. else
  1509. begin
  1510. Message(asmr_e_invalid_opcode_and_operand);
  1511. Consume(actasmtoken);
  1512. end;
  1513. end; { end case }
  1514. end;
  1515. Procedure tm68kmotreader.BuildStringConstant(asciiz: boolean);
  1516. {*********************************************************************}
  1517. { PROCEDURE BuildStringConstant }
  1518. { Description: Takes care of a ASCII, or ASCIIZ directive. }
  1519. { asciiz: boolean -> if true then string will be null terminated. }
  1520. {*********************************************************************}
  1521. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  1522. { On ENTRY: Token should point to AS_STRING }
  1523. {*********************************************************************}
  1524. var
  1525. expr: string;
  1526. errorflag : boolean;
  1527. begin
  1528. errorflag := FALSE;
  1529. Repeat
  1530. Case actasmtoken of
  1531. AS_STRING: begin
  1532. expr:=actasmpattern;
  1533. if asciiz then
  1534. expr:=expr+#0;
  1535. ConcatString(curlist,expr);
  1536. Consume(AS_STRING);
  1537. end;
  1538. AS_COMMA: begin
  1539. Consume(AS_COMMA);
  1540. END;
  1541. AS_SEPARATOR: ;
  1542. else
  1543. begin
  1544. Consume(actasmtoken);
  1545. if not errorflag then
  1546. Message(asmr_e_invalid_string_expression);
  1547. errorflag := TRUE;
  1548. end;
  1549. end; { end case }
  1550. Until actasmtoken = AS_SEPARATOR;
  1551. end;
  1552. Procedure TM68kmotReader.BuildOpCode(instr:Tm68kinstruction);
  1553. {*********************************************************************}
  1554. { PROCEDURE BuildOpcode; }
  1555. { Description: Parses the intel opcode and operands, and writes it }
  1556. { in the TInstruction object. }
  1557. {*********************************************************************}
  1558. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  1559. { On ENTRY: Token should point to AS_OPCODE }
  1560. {*********************************************************************}
  1561. var
  1562. operandnum : longint;
  1563. begin
  1564. { // opcode // }
  1565. { allow for newline as in gas styled syntax }
  1566. { under DOS you get two AS_SEPARATOR !! }
  1567. while actasmtoken=AS_SEPARATOR do
  1568. Consume(AS_SEPARATOR);
  1569. if (actasmtoken <> AS_OPCODE) then
  1570. begin
  1571. Message(asmr_e_invalid_or_missing_opcode);
  1572. { error recovery }
  1573. While not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1574. Consume(actasmtoken);
  1575. exit;
  1576. end
  1577. else
  1578. begin
  1579. Instr.opcode := findopcode(actasmpattern,instr.opsize);
  1580. Consume(AS_OPCODE);
  1581. { // Zero operand opcode ? // }
  1582. if actasmtoken = AS_SEPARATOR then
  1583. exit
  1584. else
  1585. operandnum := 1;
  1586. end;
  1587. While actasmtoken <> AS_SEPARATOR do
  1588. begin
  1589. case actasmtoken of
  1590. { // Operand delimiter // }
  1591. AS_COMMA: begin
  1592. if operandnum > Max_Operands then
  1593. Message(asmr_e_too_many_operands)
  1594. else
  1595. Inc(operandnum);
  1596. Consume(AS_COMMA);
  1597. end;
  1598. { // End of asm operands for this opcode // }
  1599. AS_SEPARATOR: ;
  1600. else
  1601. BuildOperand(Instr.Operands[operandnum] as tm68koperand);
  1602. end; { end case }
  1603. end; { end while }
  1604. instr.Ops:=operandnum;
  1605. end;
  1606. function tm68kmotreader.Assemble: tlinkedlist;
  1607. var
  1608. hl: tasmlabel;
  1609. instr : TM68kInstruction;
  1610. begin
  1611. //Message(asmr_d_start_reading);
  1612. firsttoken := TRUE;
  1613. operandnum := 0;
  1614. { sets up all opcode and register tables in uppercase }
  1615. if not _asmsorted then
  1616. begin
  1617. SetupTables;
  1618. _asmsorted := TRUE;
  1619. end;
  1620. curlist:=TAsmList.Create;
  1621. { setup label linked list }
  1622. LocalLabelList:=TLocalLabelList.Create;
  1623. c:=current_scanner.asmgetchar;
  1624. gettoken;
  1625. while actasmtoken<>AS_END do
  1626. begin
  1627. case actasmtoken of
  1628. AS_LLABEL:
  1629. begin
  1630. if CreateLocalLabel(actasmpattern,hl,true) then
  1631. ConcatLabel(curlist,hl);
  1632. Consume(AS_LLABEL);
  1633. end;
  1634. AS_LABEL:
  1635. begin
  1636. { when looking for Pascal labels, these must }
  1637. { be in uppercase. }
  1638. if SearchLabel(upper(actasmpattern),hl,true) then
  1639. ConcatLabel(curlist,hl)
  1640. else
  1641. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1642. Consume(AS_LABEL);
  1643. end;
  1644. AS_DW:
  1645. begin
  1646. Consume(AS_DW);
  1647. BuildConstant($ffff);
  1648. end;
  1649. AS_DB:
  1650. begin
  1651. Consume(AS_DB);
  1652. BuildConstant($ff);
  1653. end;
  1654. AS_DD:
  1655. begin
  1656. Consume(AS_DD);
  1657. BuildConstant(longint($ffffffff));
  1658. end;
  1659. AS_XDEF:
  1660. begin
  1661. Consume(AS_XDEF);
  1662. if actasmtoken=AS_ID then
  1663. ConcatPublic(curlist,actasmpattern);
  1664. Consume(AS_ID);
  1665. if actasmtoken<>AS_SEPARATOR then
  1666. Consume(AS_SEPARATOR);
  1667. end;
  1668. AS_ALIGN:
  1669. begin
  1670. Message(asmr_w_align_not_supported);
  1671. while actasmtoken <> AS_SEPARATOR do
  1672. Consume(actasmtoken);
  1673. end;
  1674. AS_OPCODE:
  1675. begin
  1676. instr:=TM68kInstruction.Create(tm68koperand);
  1677. BuildOpcode(instr);
  1678. // instr.AddReferenceSizes;
  1679. // instr.SetInstructionOpsize;
  1680. // instr.CheckOperandSizes;
  1681. if instr.labeled then
  1682. instr.ConcatLabeledInstr(curlist)
  1683. else begin
  1684. instr.ConcatInstruction(curlist);
  1685. end;
  1686. instr.Free;
  1687. {
  1688. instr.init;
  1689. BuildOpcode;
  1690. instr.ops := operandnum;
  1691. if instr.labeled then
  1692. ConcatLabeledInstr(instr)
  1693. else
  1694. ConcatOpCode(instr);
  1695. instr.done;}
  1696. end;
  1697. AS_SEPARATOR:
  1698. begin
  1699. Consume(AS_SEPARATOR);
  1700. { let us go back to the first operand }
  1701. operandnum := 0;
  1702. end;
  1703. AS_END:
  1704. { end assembly block }
  1705. ;
  1706. else
  1707. begin
  1708. Message(asmr_e_syntax_error);
  1709. { error recovery }
  1710. Consume(actasmtoken);
  1711. end;
  1712. end; { end case }
  1713. end; { end while }
  1714. { Check LocalLabelList }
  1715. LocalLabelList.CheckEmitted;
  1716. LocalLabelList.Free;
  1717. assemble:=curlist;
  1718. //Message(asmr_d_finish_reading);
  1719. end;
  1720. {*****************************************************************************
  1721. Initialize
  1722. *****************************************************************************}
  1723. const
  1724. asmmode_m68k_mot_info : tasmmodeinfo =
  1725. (
  1726. id : asmmode_m68k_mot;
  1727. idtxt : 'MOTOROLA';
  1728. casmreader : tm68kmotreader;
  1729. );
  1730. asmmode_m68k_standard_info : tasmmodeinfo =
  1731. (
  1732. id : asmmode_standard;
  1733. idtxt : 'STANDARD';
  1734. casmreader : tm68kmotreader;
  1735. );
  1736. begin
  1737. RegisterAsmMode(asmmode_m68k_mot_info);
  1738. RegisterAsmMode(asmmode_m68k_standard_info);
  1739. end.