ra68kmot.pas 68 KB

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