ra68kmot.pas 66 KB

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