ra68kmot.pas 64 KB

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