ra68kmot.pas 63 KB

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