ra68kmot.pas 64 KB

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