ra68kmot.pas 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794
  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. Consume(actasmtoken);
  916. end;
  917. end; { end case }
  918. until actasmtoken = AS_SEPARATOR;
  919. end;
  920. Procedure TM68kMotreader.BuildScaling(const oper:tm68koperand);
  921. {*********************************************************************}
  922. { Takes care of parsing expression starting from the scaling value }
  923. { up to and including possible field specifiers. }
  924. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR }
  925. { or AS_COMMA. On entry should point to the AS_STAR token. }
  926. {*********************************************************************}
  927. var str:string;
  928. l: longint;
  929. code: integer;
  930. begin
  931. str:='';
  932. Consume(AS_STAR);
  933. if (oper.opr.ref.scalefactor <> 0)
  934. and (oper.opr.ref.scalefactor <> 1) then
  935. Message(asmr_e_wrong_base_index);
  936. case actasmtoken of
  937. AS_INTNUM: str := actasmpattern;
  938. AS_HEXNUM: str := Tostr(ParseVal(actasmpattern,16));
  939. AS_BINNUM: str := Tostr(ParseVal(actasmpattern,2));
  940. AS_OCTALNUM: str := Tostr(ParseVal(actasmpattern,8));
  941. else
  942. Message(asmr_e_syntax_error);
  943. end;
  944. val(str, l, code);
  945. if code <> 0 then
  946. Message(asmr_e_wrong_scale_factor);
  947. if ((l = 2) or (l = 4) or (l = 8) or (l = 1)) and (code = 0) then
  948. begin
  949. oper.opr.ref.scalefactor := l;
  950. end
  951. else
  952. begin
  953. Message(asmr_e_wrong_scale_factor);
  954. oper.opr.ref.scalefactor := 0;
  955. end;
  956. if oper.opr.ref.index = NR_NO then
  957. begin
  958. Message(asmr_e_wrong_base_index);
  959. oper.opr.ref.scalefactor := 0;
  960. end;
  961. { Consume the scaling number }
  962. Consume(actasmtoken);
  963. if actasmtoken = AS_RPAREN then
  964. Consume(AS_RPAREN)
  965. else
  966. Message(asmr_e_wrong_scale_factor);
  967. { // .Field.Field ... or separator/comma // }
  968. if actasmtoken in [AS_COMMA,AS_SEPARATOR] then
  969. begin
  970. end
  971. else
  972. Message(asmr_e_syntax_error);
  973. end;
  974. Function TM68kMotreader.BuildRefExpression: tcgint;
  975. {*********************************************************************}
  976. { FUNCTION BuildRefExpression: tcgint }
  977. { Description: This routine calculates a constant expression to }
  978. { a given value. The return value is the value calculated from }
  979. { the expression. }
  980. { The following tokens (not strings) are recognized: }
  981. { SHL,SHR,/,*,NOT,OR,XOR,AND,MOD,+/-,numbers,ID to constants. }
  982. {*********************************************************************}
  983. { ENTRY: On entry the token should be any valid expression token. }
  984. { EXIT: On Exit the token points to the LPAREN token. }
  985. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  986. { invalid tokens. }
  987. {*********************************************************************}
  988. var
  989. tempstr: string;
  990. expr: string;
  991. l : tcgint;
  992. errorflag : boolean;
  993. begin
  994. BuildRefExpression := 0;
  995. errorflag := FALSE;
  996. expr := '';
  997. repeat
  998. tempstr := '';
  999. case actasmtoken of
  1000. AS_RPAREN:
  1001. Message(asmr_e_syntax_error);
  1002. AS_SHL:
  1003. tempstr := '<';
  1004. AS_SHR:
  1005. tempstr := '>';
  1006. AS_SLASH:
  1007. tempstr := '/';
  1008. AS_MOD:
  1009. tempstr := '%';
  1010. AS_STAR:
  1011. tempstr := '*';
  1012. AS_PLUS:
  1013. tempstr := '+';
  1014. AS_MINUS:
  1015. tempstr := '-';
  1016. AS_AND:
  1017. tempstr := '&';
  1018. AS_NOT:
  1019. tempstr := '~';
  1020. AS_XOR:
  1021. tempstr := '^';
  1022. AS_OR:
  1023. tempstr := '|';
  1024. { End of reference }
  1025. AS_LPAREN:
  1026. begin
  1027. if not ErrorFlag then
  1028. BuildRefExpression := CalculateExpression(expr);
  1029. { no longer in an expression }
  1030. exit;
  1031. end;
  1032. AS_ID:
  1033. begin
  1034. if not SearchIConstant(actasmpattern,l) then
  1035. begin
  1036. Message(asmr_e_syn_constant);
  1037. l := 0;
  1038. end;
  1039. str(l, tempstr);
  1040. end;
  1041. AS_INTNUM:
  1042. tempstr := actasmpattern;
  1043. AS_BINNUM:
  1044. begin
  1045. tempstr := Tostr(ParseVal(actasmpattern,2));
  1046. if tempstr = '' then
  1047. Message(asmr_e_error_converting_binary);
  1048. end;
  1049. AS_HEXNUM:
  1050. begin
  1051. tempstr := Tostr(ParseVal(actasmpattern,16));
  1052. if tempstr = '' then
  1053. Message(asmr_e_error_converting_hexadecimal);
  1054. end;
  1055. AS_OCTALNUM:
  1056. begin
  1057. tempstr := Tostr(ParseVal(actasmpattern,8));
  1058. if tempstr = '' then
  1059. Message(asmr_e_error_converting_octal);
  1060. end;
  1061. else
  1062. begin
  1063. if actasmtoken in [AS_COMMA,AS_SEPARATOR] then
  1064. begin
  1065. { no longer in an expression }
  1066. if not ErrorFlag then
  1067. BuildRefExpression := CalculateExpression(expr);
  1068. exit;
  1069. end;
  1070. { write error only once. }
  1071. if not errorflag then
  1072. Message(asmr_e_invalid_constant_expression);
  1073. { consume tokens until we find COMMA or SEPARATOR }
  1074. errorflag := true;
  1075. end;
  1076. end;
  1077. if tempstr <> '' then
  1078. expr := expr + tempstr;
  1079. Consume(actasmtoken);
  1080. until false;
  1081. end;
  1082. {*********************************************************************}
  1083. { PROCEDURE BuildBracketExpression }
  1084. { Description: This routine builds up an expression after a LPAREN }
  1085. { token is encountered. }
  1086. { On entry actasmtoken should be equal to AS_LPAREN }
  1087. {*********************************************************************}
  1088. { EXIT CONDITION: On exit the routine should point to either the }
  1089. { AS_COMMA or AS_SEPARATOR token. }
  1090. {*********************************************************************}
  1091. procedure TM68kMotreader.BuildReference(const oper:tm68koperand);
  1092. var
  1093. l:tcgint;
  1094. code: integer;
  1095. str: string;
  1096. begin
  1097. str:='';
  1098. Consume(AS_LPAREN);
  1099. case actasmtoken of
  1100. { // (reg ... // }
  1101. AS_REGISTER:
  1102. begin
  1103. oper.opr.ref.base := actasmregister;
  1104. Consume(AS_REGISTER);
  1105. { can either be a register or a right parenthesis }
  1106. { // (reg) // }
  1107. { // (reg)+ // }
  1108. if actasmtoken=AS_RPAREN then
  1109. begin
  1110. Consume(AS_RPAREN);
  1111. if actasmtoken = AS_PLUS then
  1112. begin
  1113. if (oper.opr.ref.direction <> dir_none) then
  1114. Message(asmr_e_no_inc_and_dec_together)
  1115. else
  1116. oper.opr.ref.direction := dir_inc;
  1117. Consume(AS_PLUS);
  1118. end;
  1119. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR]) then
  1120. begin
  1121. Message(asmr_e_invalid_reference_syntax);
  1122. { error recovery ... }
  1123. while actasmtoken <> AS_SEPARATOR do
  1124. Consume(actasmtoken);
  1125. end;
  1126. exit;
  1127. end;
  1128. { // (reg,reg .. // }
  1129. Consume(AS_COMMA);
  1130. if actasmtoken = AS_REGISTER then
  1131. begin
  1132. oper.opr.ref.index :=
  1133. actasmregister;
  1134. Consume(AS_REGISTER);
  1135. { check for scaling ... }
  1136. case actasmtoken of
  1137. AS_RPAREN:
  1138. begin
  1139. Consume(AS_RPAREN);
  1140. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR]) then
  1141. begin
  1142. { error recovery ... }
  1143. Message(asmr_e_invalid_reference_syntax);
  1144. while actasmtoken <> AS_SEPARATOR do
  1145. Consume(actasmtoken);
  1146. end;
  1147. exit;
  1148. end;
  1149. AS_STAR:
  1150. begin
  1151. BuildScaling(oper);
  1152. end;
  1153. else
  1154. begin
  1155. Message(asmr_e_invalid_reference_syntax);
  1156. while (actasmtoken <> AS_SEPARATOR) do
  1157. Consume(actasmtoken);
  1158. end;
  1159. end; { end case }
  1160. end
  1161. else
  1162. begin
  1163. Message(asmr_e_invalid_reference_syntax);
  1164. while (actasmtoken <> AS_SEPARATOR) do
  1165. Consume(actasmtoken);
  1166. end;
  1167. end;
  1168. AS_HEXNUM,AS_OCTALNUM, { direct address }
  1169. AS_BINNUM,AS_INTNUM:
  1170. begin
  1171. case actasmtoken of
  1172. AS_INTNUM: str := actasmpattern;
  1173. AS_HEXNUM: str := Tostr(ParseVal(actasmpattern,16));
  1174. AS_BINNUM: str := Tostr(ParseVal(actasmpattern,2));
  1175. AS_OCTALNUM: str := Tostr(ParseVal(actasmpattern,8));
  1176. else
  1177. Message(asmr_e_syntax_error);
  1178. end;
  1179. Consume(actasmtoken);
  1180. val(str, l, code);
  1181. if code <> 0 then
  1182. Message(asmr_e_invalid_reference_syntax)
  1183. else
  1184. oper.opr.ref.offset := l;
  1185. Consume(AS_RPAREN);
  1186. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR]) then
  1187. begin
  1188. { error recovery ... }
  1189. Message(asmr_e_invalid_reference_syntax);
  1190. while actasmtoken <> AS_SEPARATOR do
  1191. Consume(actasmtoken);
  1192. end;
  1193. exit;
  1194. end;
  1195. else
  1196. begin
  1197. Message(asmr_e_invalid_reference_syntax);
  1198. while (actasmtoken <> AS_SEPARATOR) do
  1199. Consume(actasmtoken);
  1200. end;
  1201. end;
  1202. end;
  1203. procedure tm68kmotreader.BuildRegList(const oper:tm68koperand);
  1204. {*********************************************************************}
  1205. { EXIT CONDITION: On exit the routine should point to either the }
  1206. { AS_COMMA or AS_SEPARATOR token. }
  1207. {*********************************************************************}
  1208. var
  1209. i: Tsuperregister;
  1210. reg_one, reg_two: tregister;
  1211. rs_one, rs_two: tsuperregister;
  1212. addrregset,dataregset,fpuregset: tcpuregisterset;
  1213. errorflag, first: boolean;
  1214. begin
  1215. dataregset := [];
  1216. addrregset := [];
  1217. fpuregset := [];
  1218. { 1., try to consume a register list
  1219. 2., if successful, add the register list
  1220. 3., if not possible, then we have a standalone register, add
  1221. 4., repeat until we dont have a slash any more }
  1222. errorflag:=false;
  1223. first:=true;
  1224. repeat
  1225. reg_one:=actasmregister;
  1226. rs_one:=getsupreg(reg_one);
  1227. if not (first or try_to_consume(AS_REGISTER)) then
  1228. begin
  1229. errorflag:=true;
  1230. break;
  1231. end;
  1232. first:=false;
  1233. { try to consume a register list }
  1234. if try_to_consume(AS_MINUS) then
  1235. begin
  1236. reg_two:=actasmregister;
  1237. rs_two:=getsupreg(reg_two);
  1238. if (not try_to_consume(AS_REGISTER)) or
  1239. (rs_one >= rs_two) or
  1240. (getregtype(reg_one) <> getregtype(reg_two)) then
  1241. begin
  1242. errorflag:=true;
  1243. break;
  1244. end;
  1245. end
  1246. else
  1247. begin
  1248. { nope, we have a single element "list" }
  1249. reg_two:=reg_one;
  1250. rs_two:=getsupreg(reg_two);
  1251. end;
  1252. case getregtype(reg_one) of
  1253. R_INTREGISTER:
  1254. for i:=getsupreg(reg_one) to getsupreg(reg_two) do
  1255. include(dataregset,i);
  1256. R_ADDRESSREGISTER:
  1257. for i:=getsupreg(reg_one) to getsupreg(reg_two) do
  1258. include(addrregset,i);
  1259. R_FPUREGISTER:
  1260. for i:=getsupreg(reg_one) to getsupreg(reg_two) do
  1261. include(fpuregset,i);
  1262. else
  1263. internalerror(201611041);
  1264. end;
  1265. until not try_to_consume(AS_SLASH);
  1266. errorflag:=errorflag or
  1267. (((dataregset <> []) or (addrregset <> [])) and (fpuregset <> [])) or
  1268. (not (actasmtoken in [AS_SEPARATOR,AS_COMMA]));
  1269. if errorflag then
  1270. begin
  1271. Message(asmr_e_invalid_reg_list_in_movem_or_fmovem);
  1272. consume_all_until([AS_SEPARATOR,AS_COMMA]);
  1273. end
  1274. else
  1275. begin
  1276. oper.opr.typ:= OPR_REGSET;
  1277. oper.opr.regsetdata := dataregset;
  1278. oper.opr.regsetaddr := addrregset;
  1279. oper.opr.regsetfpu := fpuregset;
  1280. end;
  1281. end;
  1282. procedure tm68kmotreader.BuildRegPair(const oper:tm68koperand);
  1283. {*********************************************************************}
  1284. { EXIT CONDITION: On exit the routine should point to either the }
  1285. { AS_COMMA or AS_SEPARATOR token. }
  1286. {*********************************************************************}
  1287. begin
  1288. oper.opr.typ := OPR_REGPAIR;
  1289. oper.opr.reghi := actasmregister;
  1290. Consume(AS_COLON);
  1291. if not try_to_consume(AS_REGISTER) then
  1292. begin
  1293. Message(asmr_e_invalid_reg_list_for_opcode);
  1294. consume_all_until([AS_SEPARATOR,AS_COMMA]);
  1295. end
  1296. else
  1297. oper.opr.reglo:=actasmregister;
  1298. end;
  1299. Procedure TM68kMotreader.BuildOperand(const oper:tm68koperand);
  1300. {*********************************************************************}
  1301. { EXIT CONDITION: On exit the routine should point to either the }
  1302. { AS_COMMA or AS_SEPARATOR token. }
  1303. {*********************************************************************}
  1304. var
  1305. expr: string;
  1306. tempstr: string;
  1307. lab: tasmlabel;
  1308. l : tcgint;
  1309. hl: tasmlabel;
  1310. p: pointer;
  1311. begin
  1312. case actasmtoken of
  1313. { // Memory reference // }
  1314. AS_LPAREN:
  1315. begin
  1316. Oper.InitRef;
  1317. BuildReference(oper);
  1318. end;
  1319. { // Constant expression // }
  1320. AS_APPT: begin
  1321. Consume(AS_APPT);
  1322. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1323. Message(asmr_e_invalid_operand_type);
  1324. { identifiers are handled by BuildExpression }
  1325. oper.opr.typ := OPR_CONSTANT;
  1326. l:=BuildExpression(true,@tempstr);
  1327. oper.opr.val :=aint(l);
  1328. if tempstr<>'' then
  1329. begin
  1330. l:=oper.opr.val;
  1331. oper.opr.typ := OPR_SYMBOL;
  1332. oper.opr.symofs := l;
  1333. oper.opr.symbol := current_asmdata.RefAsmSymbol(tempstr,AT_FUNCTION);
  1334. end;
  1335. end;
  1336. { // Constant memory offset . // }
  1337. AS_HEXNUM,AS_INTNUM,
  1338. AS_BINNUM,AS_OCTALNUM,AS_PLUS:
  1339. begin
  1340. Oper.InitRef;
  1341. oper.opr.ref.offset:=BuildRefExpression;
  1342. if actasmtoken = AS_LPAREN then
  1343. BuildReference(oper);
  1344. end;
  1345. { // A constant expression, or a Variable ref. // }
  1346. AS_ID: begin
  1347. if actasmpattern[1] = '@' then
  1348. { // Label or Special symbol reference // }
  1349. begin
  1350. if actasmpattern = '@RESULT' then
  1351. oper.SetUpResult
  1352. else
  1353. if actasmpattern = 'SELF' then
  1354. oper.SetUpSelf
  1355. else
  1356. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  1357. Message(asmr_w_CODE_and_DATA_not_supported)
  1358. else
  1359. begin
  1360. delete(actasmpattern,1,1);
  1361. if actasmpattern = '' then
  1362. Message(asmr_e_null_label_ref_not_allowed);
  1363. CreateLocalLabel(actasmpattern,lab,false);
  1364. oper.opr.typ := OPR_SYMBOL;
  1365. oper.opr.symbol := lab;
  1366. oper.opr.symofs := 0;
  1367. end;
  1368. Consume(AS_ID);
  1369. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1370. Message(asmr_e_syntax_error);
  1371. end
  1372. { probably a variable or normal expression }
  1373. { or a procedure (such as in CALL ID) }
  1374. else
  1375. begin
  1376. { is it a constant ? }
  1377. if SearchIConstant(actasmpattern,l) then
  1378. begin
  1379. Oper.InitRef;
  1380. oper.opr.ref.offset:=BuildRefExpression;
  1381. if actasmtoken = AS_LPAREN then
  1382. BuildReference(oper);
  1383. end
  1384. else { is it a label variable ? }
  1385. { // ID[ , ID.Field.Field or simple ID // }
  1386. { check if this is a label, if so then }
  1387. { emit it as a label. }
  1388. if SearchLabel(actasmpattern,hl,false) then
  1389. begin
  1390. oper.opr.typ := OPR_SYMBOL;
  1391. oper.opr.symbol := hl;
  1392. oper.opr.symofs := 0;
  1393. Consume(AS_ID);
  1394. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1395. Message(asmr_e_syntax_error);
  1396. end
  1397. else begin
  1398. expr:=actasmpattern;
  1399. Consume(AS_ID);
  1400. { typecasting? }
  1401. if SearchType(expr,l) then
  1402. begin
  1403. oper.hastype:=true;
  1404. oper.typesize:=l;
  1405. case actasmtoken of
  1406. AS_LPAREN :
  1407. begin
  1408. { Support Type([Reference]) }
  1409. Consume(AS_LPAREN);
  1410. BuildOperand(oper{,true});
  1411. Consume(AS_RPAREN);
  1412. end;
  1413. AS_LBRACKET :
  1414. begin
  1415. { Support Var.Type[Index] }
  1416. { Convert @label.Byte[1] to reference }
  1417. if oper.opr.typ=OPR_SYMBOL then
  1418. oper.initref;
  1419. end;
  1420. else
  1421. ;
  1422. end;
  1423. end
  1424. else
  1425. begin
  1426. if actasmtoken = AS_LPAREN then
  1427. oper.initref;
  1428. if not oper.SetupVar(expr,false) then
  1429. begin
  1430. { not a variable, check special variables.. }
  1431. if expr = 'SELF' then
  1432. oper.SetupSelf
  1433. else begin
  1434. Message1(sym_e_unknown_id,expr);
  1435. end;
  1436. expr:='';
  1437. end;
  1438. end;
  1439. // Message1(sym_e_unknown_id,actasmpattern);
  1440. end;
  1441. case actasmtoken of
  1442. AS_LPAREN: { indexing }
  1443. BuildReference(oper);
  1444. AS_SEPARATOR,AS_COMMA: begin
  1445. end;
  1446. else
  1447. Message(asmr_e_syntax_error);
  1448. end;
  1449. end;
  1450. end;
  1451. { // Pre-decrement mode reference or constant mem offset. // }
  1452. AS_MINUS: begin
  1453. Consume(AS_MINUS);
  1454. if actasmtoken = AS_LPAREN then
  1455. begin
  1456. Oper.InitRef;
  1457. { indicate pre-decrement mode }
  1458. oper.opr.ref.direction := dir_dec;
  1459. BuildReference(oper);
  1460. end
  1461. else
  1462. if actasmtoken in [AS_OCTALNUM,AS_HEXNUM,AS_BINNUM,AS_INTNUM] then
  1463. begin
  1464. Oper.InitRef;
  1465. oper.opr.ref.offset:=BuildRefExpression;
  1466. { negate because was preceded by a negative sign! }
  1467. oper.opr.ref.offset:=-oper.opr.ref.offset;
  1468. BuildReference(oper);
  1469. end
  1470. else
  1471. begin
  1472. Message(asmr_e_syntax_error);
  1473. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1474. Consume(actasmtoken);
  1475. end;
  1476. end;
  1477. { // Register, a variable reference or a constant reference // }
  1478. AS_REGISTER: begin
  1479. Consume(AS_REGISTER);
  1480. case actasmtoken of
  1481. AS_SEPARATOR, AS_COMMA:
  1482. begin
  1483. { // Simple register // }
  1484. if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
  1485. Message(asmr_e_invalid_operand_type);
  1486. oper.opr.typ := OPR_REGISTER;
  1487. oper.opr.reg := actasmregister;
  1488. end;
  1489. AS_SLASH, AS_MINUS:
  1490. { // Register listing // }
  1491. BuildRegList(oper);
  1492. AS_COLON:
  1493. { // Register pair // }
  1494. BuildRegPair(oper);
  1495. else
  1496. Message(asmr_e_invalid_register);
  1497. end;
  1498. end;
  1499. AS_SEPARATOR, AS_COMMA: ;
  1500. else
  1501. begin
  1502. Message(asmr_e_invalid_opcode_and_operand);
  1503. Consume(actasmtoken);
  1504. end;
  1505. end; { end case }
  1506. end;
  1507. Procedure TM68kmotReader.BuildOpCode(instr:Tm68kinstruction);
  1508. {*********************************************************************}
  1509. { PROCEDURE BuildOpcode; }
  1510. { Description: Parses the intel opcode and operands, and writes it }
  1511. { in the TInstruction object. }
  1512. {*********************************************************************}
  1513. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  1514. { On ENTRY: Token should point to AS_OPCODE }
  1515. {*********************************************************************}
  1516. var
  1517. operandnum : longint;
  1518. begin
  1519. { // opcode // }
  1520. { allow for newline as in gas styled syntax }
  1521. { under DOS you get two AS_SEPARATOR !! }
  1522. while actasmtoken=AS_SEPARATOR do
  1523. Consume(AS_SEPARATOR);
  1524. if (actasmtoken <> AS_OPCODE) then
  1525. begin
  1526. Message(asmr_e_invalid_or_missing_opcode);
  1527. { error recovery }
  1528. While not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1529. Consume(actasmtoken);
  1530. exit;
  1531. end
  1532. else
  1533. begin
  1534. Instr.opcode := findopcode(actasmpattern,instr.opsize);
  1535. Consume(AS_OPCODE);
  1536. { // Zero operand opcode ? // }
  1537. if actasmtoken = AS_SEPARATOR then
  1538. exit
  1539. else
  1540. operandnum := 1;
  1541. end;
  1542. While actasmtoken <> AS_SEPARATOR do
  1543. begin
  1544. case actasmtoken of
  1545. { // Operand delimiter // }
  1546. AS_COMMA: begin
  1547. if operandnum > Max_Operands then
  1548. Message(asmr_e_too_many_operands)
  1549. else
  1550. Inc(operandnum);
  1551. Consume(AS_COMMA);
  1552. end;
  1553. { // End of asm operands for this opcode // }
  1554. AS_SEPARATOR: ;
  1555. else
  1556. BuildOperand(Instr.Operands[operandnum] as tm68koperand);
  1557. end; { end case }
  1558. end; { end while }
  1559. instr.Ops:=operandnum;
  1560. end;
  1561. function tm68kmotreader.Assemble: tlinkedlist;
  1562. var
  1563. hl: tasmlabel;
  1564. instr : TM68kInstruction;
  1565. begin
  1566. //Message(asmr_d_start_reading);
  1567. firsttoken := TRUE;
  1568. operandnum := 0;
  1569. { sets up all opcode and register tables in uppercase }
  1570. if not _asmsorted then
  1571. begin
  1572. SetupTables;
  1573. _asmsorted := TRUE;
  1574. end;
  1575. curlist:=TAsmList.Create;
  1576. gettoken;
  1577. while actasmtoken<>AS_END do
  1578. begin
  1579. case actasmtoken of
  1580. AS_LLABEL:
  1581. begin
  1582. if CreateLocalLabel(actasmpattern,hl,true) then
  1583. ConcatLabel(curlist,hl);
  1584. Consume(AS_LLABEL);
  1585. end;
  1586. AS_LABEL:
  1587. begin
  1588. { when looking for Pascal labels, these must }
  1589. { be in uppercase. }
  1590. if SearchLabel(upper(actasmpattern),hl,true) then
  1591. ConcatLabel(curlist,hl)
  1592. else
  1593. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1594. Consume(AS_LABEL);
  1595. end;
  1596. AS_DW:
  1597. begin
  1598. Consume(AS_DW);
  1599. BuildConstant(sizeof(word));
  1600. end;
  1601. AS_DB:
  1602. begin
  1603. Consume(AS_DB);
  1604. BuildConstant(sizeof(byte));
  1605. end;
  1606. AS_DD:
  1607. begin
  1608. Consume(AS_DD);
  1609. BuildConstant(sizeof(dword));
  1610. end;
  1611. AS_XDEF:
  1612. begin
  1613. Consume(AS_XDEF);
  1614. if actasmtoken=AS_ID then
  1615. ConcatPublic(curlist,actasmpattern);
  1616. Consume(AS_ID);
  1617. if actasmtoken<>AS_SEPARATOR then
  1618. Consume(AS_SEPARATOR);
  1619. end;
  1620. AS_ALIGN:
  1621. begin
  1622. Message(asmr_w_align_not_supported);
  1623. while actasmtoken <> AS_SEPARATOR do
  1624. Consume(actasmtoken);
  1625. end;
  1626. AS_OPCODE:
  1627. begin
  1628. instr:=TM68kInstruction.Create(tm68koperand);
  1629. BuildOpcode(instr);
  1630. // instr.AddReferenceSizes;
  1631. // instr.SetInstructionOpsize;
  1632. // instr.CheckOperandSizes;
  1633. if instr.labeled then
  1634. instr.ConcatLabeledInstr(curlist)
  1635. else begin
  1636. instr.ConcatInstruction(curlist);
  1637. end;
  1638. instr.Free;
  1639. end;
  1640. AS_SEPARATOR:
  1641. begin
  1642. Consume(AS_SEPARATOR);
  1643. { let us go back to the first operand }
  1644. operandnum := 0;
  1645. end;
  1646. AS_END:
  1647. { end assembly block }
  1648. ;
  1649. else
  1650. begin
  1651. Message(asmr_e_syntax_error);
  1652. { error recovery }
  1653. Consume(actasmtoken);
  1654. end;
  1655. end; { end case }
  1656. end; { end while }
  1657. { Check LocalLabelList }
  1658. checklocallabels;
  1659. assemble:=curlist;
  1660. //Message(asmr_d_finish_reading);
  1661. end;
  1662. {*****************************************************************************
  1663. Initialize
  1664. *****************************************************************************}
  1665. const
  1666. asmmode_m68k_mot_info : tasmmodeinfo =
  1667. (
  1668. id : asmmode_m68k_mot;
  1669. idtxt : 'MOTOROLA';
  1670. casmreader : tm68kmotreader;
  1671. );
  1672. asmmode_m68k_standard_info : tasmmodeinfo =
  1673. (
  1674. id : asmmode_standard;
  1675. idtxt : 'STANDARD';
  1676. casmreader : tm68kmotreader;
  1677. );
  1678. begin
  1679. RegisterAsmMode(asmmode_m68k_mot_info);
  1680. RegisterAsmMode(asmmode_m68k_standard_info);
  1681. end.