ra68kmot.pas 68 KB

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