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