ra68kmot.pas 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Carl Eric Codere
  4. This unit does the parsing process for the motorola inline assembler
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ra68kmot;
  19. {$i fpcdefs.inc}
  20. {**********************************************************************}
  21. { WARNING }
  22. {**********************************************************************}
  23. { Any modification in the order or removal of terms in the tables }
  24. { in m68k.pas and asmo68k.pas will BREAK the code in this unit, }
  25. { unless the appropriate changes are made to this unit. Addition }
  26. { of terms though, will not change the code herein. }
  27. {**********************************************************************}
  28. {---------------------------------------------------------------------------}
  29. { LEFT TO DO }
  30. {---------------------------------------------------------------------------}
  31. { o Add support for sized indexing such as in d0.l }
  32. { presently only (an,dn) is supported for indexing -- }
  33. { size defaults to LONG. }
  34. { o Add support for MC68020 opcodes. }
  35. { o Add support for MC68020 adressing modes. }
  36. { o Add operand checking with m68k opcode table in ConcatOpCode }
  37. { o Add Floating point support }
  38. {---------------------------------------------------------------------------}
  39. interface
  40. uses
  41. rasm;
  42. type
  43. tasmtoken = (
  44. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_HEXNUM,AS_OCTALNUM,
  45. AS_BINNUM,AS_COMMA,AS_LBRACKET,AS_RBRACKET,AS_LPAREN,
  46. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,AS_INTNUM,
  47. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,AS_APPT,AS_REALNUM,
  48. AS_ALIGN,
  49. {------------------ Assembler directives --------------------}
  50. AS_DB,AS_DW,AS_DD,AS_XDEF,AS_END,
  51. {------------------ Assembler Operators --------------------}
  52. AS_MOD,AS_SHL,AS_SHR,AS_NOT,AS_AND,AS_OR,AS_XOR);
  53. tm68kmotreader = class(tasmreader)
  54. actasmtoken: tasmtoken;
  55. actasmpattern: string;
  56. destructor destroy;override;
  57. end;
  58. Implementation
  59. uses
  60. { common }
  61. cutils,cclasses,
  62. { global }
  63. globtype,globals,verbose,
  64. systems,
  65. { aasm }
  66. cpuinfo,aasmbase,aasmtai,aasmcpu,
  67. { symtable }
  68. symconst,symbase,symtype,symsym,symtable,
  69. { pass 1 }
  70. nbas,
  71. { parser }
  72. scanner,agcpugas,
  73. rautils
  74. ;
  75. const
  76. { this variable is TRUE if the lookup tables have already been setup }
  77. { for fast access. On the first call to assemble the tables are setup }
  78. { and stay set up. }
  79. _asmsorted: boolean = FALSE;
  80. firstasmreg = R_D0;
  81. lastasmreg = R_FPSR;
  82. type
  83. tiasmops = array[firstop..lastop] of string[7];
  84. piasmops = ^tiasmops;
  85. tasmkeyword = string[6];
  86. var
  87. { sorted tables of opcodes }
  88. iasmops: piasmops;
  89. { uppercased tables of registers }
  90. iasmregs: array[firstasmreg..lastasmreg] of string[6];
  91. const
  92. regname_count=17;
  93. regname_count_bsstart=16;
  94. regname2regnum:array[0..regname_count-1] of regname2regnumrec=(
  95. (name:'A0'; number:NR_A0),
  96. (name:'A1'; number:NR_A1),
  97. (name:'A2'; number:NR_A2),
  98. (name:'A3'; number:NR_A3),
  99. (name:'A4'; number:NR_A4),
  100. (name:'A5'; number:NR_A5),
  101. (name:'A6'; number:NR_A6),
  102. (name:'A7'; number:NR_A7),
  103. (name:'D0'; number:NR_D0),
  104. (name:'D1'; number:NR_D1),
  105. (name:'D2'; number:NR_D2),
  106. (name:'D3'; number:NR_D3),
  107. (name:'D4'; number:NR_D4),
  108. (name:'D5'; number:NR_D5),
  109. (name:'D6'; number:NR_D6),
  110. (name:'D7'; number:NR_D7),
  111. (name:'SP'; number:NR_A7));
  112. const
  113. firstdirective = AS_DB;
  114. lastdirective = AS_END;
  115. firstoperator = AS_MOD;
  116. lastoperator = AS_XOR;
  117. _count_asmdirectives = longint(lastdirective)-longint(firstdirective);
  118. _count_asmoperators = longint(lastoperator)-longint(firstoperator);
  119. _asmdirectives : array[0.._count_asmdirectives] of tasmkeyword =
  120. ('DC.B','DC.W','DC.L','XDEF','END');
  121. { problems with shl,shr,not,and,or and xor, they are }
  122. { context sensitive. }
  123. _asmoperators : array[0.._count_asmoperators] of tasmkeyword = (
  124. 'MOD','SHL','SHR','NOT','AND','OR','XOR');
  125. const
  126. firsttoken : boolean = TRUE;
  127. operandnum : byte = 0;
  128. Procedure SetupTables;
  129. { creates uppercased symbol tables for speed access }
  130. var
  131. i: tasmop;
  132. j: tregister;
  133. begin
  134. {Message(asmr_d_creating_lookup_tables);}
  135. { opcodes }
  136. new(iasmops);
  137. for i:=firstop to lastop do
  138. iasmops^[i] := upper(gas_op2str[i]);
  139. { opcodes }
  140. for j.enum:=firstasmreg to lastasmreg do
  141. iasmregs[j.enum] := upper(std_reg2str[j.enum]);
  142. end;
  143. {---------------------------------------------------------------------}
  144. { Routines for the tokenizing }
  145. {---------------------------------------------------------------------}
  146. function regnum_search(const s:string):Tnewregister;
  147. {Searches the register number that belongs to the register in s.
  148. s must be in uppercase!.}
  149. var i,p:byte;
  150. begin
  151. {Binary search.}
  152. p:=0;
  153. i:=regname_count_bsstart;
  154. while i<>0 do
  155. begin
  156. if (p+i<regname_count) and (upper(regname2regnum[p+i].name)<=s) then
  157. p:=p+i;
  158. i:=i shr 1;
  159. end;
  160. if upper(regname2regnum[p].name)=s then
  161. regnum_search:=regname2regnum[p].number
  162. else
  163. regnum_search:=NR_NO;
  164. end;
  165. function is_asmopcode(s: string):Boolean;
  166. {*********************************************************************}
  167. { FUNCTION is_asmopcode(s: string):Boolean }
  168. { Description: Determines if the s string is a valid opcode }
  169. { if so returns TRUE otherwise returns FALSE. }
  170. { Remark: Suffixes are also checked, as long as they are valid. }
  171. {*********************************************************************}
  172. var
  173. i: tasmop;
  174. j: byte;
  175. begin
  176. is_asmopcode := FALSE;
  177. { first of all we remove the suffix }
  178. j:=pos('.',s);
  179. if j<>0 then
  180. delete(s,j,2);
  181. for i:=firstop to lastop do
  182. begin
  183. if s = iasmops^[i] then
  184. begin
  185. is_asmopcode:=TRUE;
  186. exit;
  187. end;
  188. end;
  189. end;
  190. Procedure is_asmdirective(const s: string; var token: tasmtoken);
  191. {*********************************************************************}
  192. { FUNCTION is_asmdirective(s: string; var token: tinteltoken):Boolean }
  193. { Description: Determines if the s string is a valid directive }
  194. { (an operator can occur in operand fields, while a directive cannot) }
  195. { if so returns the directive token, otherwise does not change token.}
  196. {*********************************************************************}
  197. var
  198. i:byte;
  199. begin
  200. for i:=0 to _count_asmdirectives do
  201. begin
  202. if s=_asmdirectives[i] then
  203. begin
  204. token := tasmtoken(longint(firstdirective)+i);
  205. exit;
  206. end;
  207. end;
  208. end;
  209. Procedure is_register(const s: string; var token: tasmtoken);
  210. {*********************************************************************}
  211. { PROCEDURE is_register(s: string; var token: tinteltoken); }
  212. { Description: Determines if the s string is a valid register, if }
  213. { so return token equal to A_REGISTER, otherwise does not change token}
  214. {*********************************************************************}
  215. var
  216. i: tregister;
  217. begin
  218. if regnum_search(s)=NR_NO then
  219. begin
  220. for i.enum:=firstasmreg to lastasmreg do
  221. begin
  222. if s=iasmregs[i.enum] then
  223. begin
  224. token := AS_REGISTER;
  225. exit;
  226. end;
  227. end;
  228. { take care of other name for sp }
  229. if s = 'A7' then
  230. begin
  231. token:=AS_REGISTER;
  232. exit;
  233. end;
  234. end
  235. else
  236. token:=AS_REGISTER;
  237. end;
  238. Function GetToken: tasmtoken;
  239. {*********************************************************************}
  240. { FUNCTION GetToken: tinteltoken; }
  241. { Description: This routine returns intel assembler tokens and }
  242. { does some minor syntax error checking. }
  243. {*********************************************************************}
  244. var
  245. token: tasmtoken;
  246. forcelabel: boolean;
  247. begin
  248. forcelabel := FALSE;
  249. actasmpattern :='';
  250. {* INIT TOKEN TO NOTHING *}
  251. token := AS_NONE;
  252. { while space and tab , continue scan... }
  253. while c in [' ',#9] do
  254. c:=current_scanner.asmgetchar;
  255. if not (c in [newline,#13,'{',';']) then
  256. current_scanner.gettokenpos;
  257. { Possiblities for first token in a statement: }
  258. { Local Label, Label, Directive, Prefix or Opcode.... }
  259. if firsttoken and not (c in [newline,#13,'{',';']) then
  260. begin
  261. firsttoken := FALSE;
  262. if c = '@' then
  263. begin
  264. token := AS_LLABEL; { this is a local label }
  265. { Let us point to the next character }
  266. c := current_scanner.asmgetchar;
  267. end;
  268. while c in ['A'..'Z','a'..'z','0'..'9','_','@','.'] do
  269. begin
  270. { if there is an at_sign, then this must absolutely be a label }
  271. if c = '@' then forcelabel:=TRUE;
  272. actasmpattern := actasmpattern + c;
  273. c := current_scanner.asmgetchar;
  274. end;
  275. uppervar(actasmpattern);
  276. if c = ':' then
  277. begin
  278. case token of
  279. AS_NONE: token := AS_LABEL;
  280. AS_LLABEL: ; { do nothing }
  281. end; { end case }
  282. { let us point to the next character }
  283. c := current_scanner.asmgetchar;
  284. gettoken := token;
  285. exit;
  286. end;
  287. { Are we trying to create an identifier with }
  288. { an at-sign...? }
  289. if forcelabel then
  290. Message(asmr_e_none_label_contain_at);
  291. If is_asmopcode(actasmpattern) then
  292. begin
  293. gettoken := AS_OPCODE;
  294. exit;
  295. end;
  296. is_asmdirective(actasmpattern, token);
  297. if (token <> AS_NONE) then
  298. begin
  299. gettoken := token;
  300. exit
  301. end
  302. else
  303. begin
  304. gettoken := AS_NONE;
  305. Message1(asmr_e_invalid_or_missing_opcode,actasmpattern);
  306. end;
  307. end
  308. else { else firsttoken }
  309. { Here we must handle all possible cases }
  310. begin
  311. case c of
  312. '@': { possiblities : - local label reference , such as in jmp @local1 }
  313. { - @Result, @Code or @Data special variables. }
  314. begin
  315. actasmpattern := c;
  316. c:= current_scanner.asmgetchar;
  317. while c in ['A'..'Z','a'..'z','0'..'9','_','@','.'] do
  318. begin
  319. actasmpattern := actasmpattern + c;
  320. c := current_scanner.asmgetchar;
  321. end;
  322. uppervar(actasmpattern);
  323. gettoken := AS_ID;
  324. exit;
  325. end;
  326. { identifier, register, opcode, prefix or directive }
  327. 'A'..'Z','a'..'z','_': begin
  328. actasmpattern := c;
  329. c:= current_scanner.asmgetchar;
  330. while c in ['A'..'Z','a'..'z','0'..'9','_','.'] do
  331. begin
  332. actasmpattern := actasmpattern + c;
  333. c := current_scanner.asmgetchar;
  334. end;
  335. uppervar(actasmpattern);
  336. If is_asmopcode(actasmpattern) then
  337. begin
  338. gettoken := AS_OPCODE;
  339. exit;
  340. end;
  341. is_register(actasmpattern, token);
  342. {is_asmoperator(actasmpattern,token);}
  343. is_asmdirective(actasmpattern,token);
  344. { if found }
  345. if (token <> AS_NONE) then
  346. begin
  347. gettoken := token;
  348. exit;
  349. end
  350. { this is surely an identifier }
  351. else
  352. token := AS_ID;
  353. gettoken := token;
  354. exit;
  355. end;
  356. { override operator... not supported }
  357. '&': begin
  358. c:=current_scanner.asmgetchar;
  359. gettoken := AS_AND;
  360. end;
  361. { string or character }
  362. '''' :
  363. begin
  364. actasmpattern:='';
  365. while true do
  366. begin
  367. if c = '''' then
  368. begin
  369. c:=current_scanner.asmgetchar;
  370. if c=newline then
  371. begin
  372. Message(scan_f_string_exceeds_line);
  373. break;
  374. end;
  375. repeat
  376. if c=''''then
  377. begin
  378. c:=current_scanner.asmgetchar;
  379. if c='''' then
  380. begin
  381. actasmpattern:=actasmpattern+'''';
  382. c:=current_scanner.asmgetchar;
  383. if c=newline then
  384. begin
  385. Message(scan_f_string_exceeds_line);
  386. break;
  387. end;
  388. end
  389. else break;
  390. end
  391. else
  392. begin
  393. actasmpattern:=actasmpattern+c;
  394. c:=current_scanner.asmgetchar;
  395. if c=newline then
  396. begin
  397. Message(scan_f_string_exceeds_line);
  398. break
  399. end;
  400. end;
  401. until false; { end repeat }
  402. end
  403. else break; { end if }
  404. end; { end while }
  405. token:=AS_STRING;
  406. gettoken := token;
  407. exit;
  408. end;
  409. '$' : begin
  410. c:=current_scanner.asmgetchar;
  411. while c in ['0'..'9','A'..'F','a'..'f'] do
  412. begin
  413. actasmpattern := actasmpattern + c;
  414. c := current_scanner.asmgetchar;
  415. end;
  416. gettoken := AS_HEXNUM;
  417. exit;
  418. end;
  419. ',' : begin
  420. gettoken := AS_COMMA;
  421. c:=current_scanner.asmgetchar;
  422. exit;
  423. end;
  424. '(' : begin
  425. gettoken := AS_LPAREN;
  426. c:=current_scanner.asmgetchar;
  427. exit;
  428. end;
  429. ')' : begin
  430. gettoken := AS_RPAREN;
  431. c:=current_scanner.asmgetchar;
  432. exit;
  433. end;
  434. ':' : begin
  435. gettoken := AS_COLON;
  436. c:=current_scanner.asmgetchar;
  437. exit;
  438. end;
  439. { '.' : begin
  440. gettoken := AS_DOT;
  441. c:=current_scanner.asmgetchar;
  442. exit;
  443. end; }
  444. '+' : begin
  445. gettoken := AS_PLUS;
  446. c:=current_scanner.asmgetchar;
  447. exit;
  448. end;
  449. '-' : begin
  450. gettoken := AS_MINUS;
  451. c:=current_scanner.asmgetchar;
  452. exit;
  453. end;
  454. '*' : begin
  455. gettoken := AS_STAR;
  456. c:=current_scanner.asmgetchar;
  457. exit;
  458. end;
  459. '/' : begin
  460. gettoken := AS_SLASH;
  461. c:=current_scanner.asmgetchar;
  462. exit;
  463. end;
  464. '<' : begin
  465. c := current_scanner.asmgetchar;
  466. { invalid characters }
  467. if c <> '<' then
  468. Message(asmr_e_invalid_char_smaller);
  469. { still assume << }
  470. gettoken := AS_SHL;
  471. c := current_scanner.asmgetchar;
  472. exit;
  473. end;
  474. '>' : begin
  475. c := current_scanner.asmgetchar;
  476. { invalid characters }
  477. if c <> '>' then
  478. Message(asmr_e_invalid_char_greater);
  479. { still assume << }
  480. gettoken := AS_SHR;
  481. c := current_scanner.asmgetchar;
  482. exit;
  483. end;
  484. '|' : begin
  485. gettoken := AS_OR;
  486. c := current_scanner.asmgetchar;
  487. exit;
  488. end;
  489. '^' : begin
  490. gettoken := AS_XOR;
  491. c := current_scanner.asmgetchar;
  492. exit;
  493. end;
  494. '#' : begin
  495. gettoken:=AS_APPT;
  496. c:=current_scanner.asmgetchar;
  497. exit;
  498. end;
  499. '%' : begin
  500. c:=current_scanner.asmgetchar;
  501. while c in ['0','1'] do
  502. begin
  503. actasmpattern := actasmpattern + c;
  504. c := current_scanner.asmgetchar;
  505. end;
  506. gettoken := AS_BINNUM;
  507. exit;
  508. end;
  509. { integer number }
  510. '0'..'9': begin
  511. actasmpattern := c;
  512. c := current_scanner.asmgetchar;
  513. while c in ['0'..'9'] do
  514. begin
  515. actasmpattern := actasmpattern + c;
  516. c:= current_scanner.asmgetchar;
  517. end;
  518. gettoken := AS_INTNUM;
  519. exit;
  520. end;
  521. ';' : begin
  522. repeat
  523. c:=current_scanner.asmgetchar;
  524. until c=newline;
  525. firsttoken := TRUE;
  526. gettoken:=AS_SEPARATOR;
  527. end;
  528. '{',#13,newline : begin
  529. c:=current_scanner.asmgetchar;
  530. firsttoken := TRUE;
  531. gettoken:=AS_SEPARATOR;
  532. end;
  533. else
  534. begin
  535. Message(scan_f_illegal_char);
  536. end;
  537. end; { end case }
  538. end; { end else if }
  539. end;
  540. {---------------------------------------------------------------------}
  541. { Routines for the parsing }
  542. {---------------------------------------------------------------------}
  543. procedure consume(t : tasmtoken);
  544. begin
  545. if t<>actasmtoken then
  546. Message(asmr_e_syntax_error);
  547. actasmtoken:=gettoken;
  548. { if the token must be ignored, then }
  549. { get another token to parse. }
  550. if actasmtoken = AS_NONE then
  551. actasmtoken := gettoken;
  552. end;
  553. function findregister(const s : string): tregister;
  554. {*********************************************************************}
  555. { FUNCTION findregister(s: string):tasmop; }
  556. { Description: Determines if the s string is a valid register, }
  557. { if so returns correct tregister token, or R_NO if not found. }
  558. {*********************************************************************}
  559. var
  560. i: tregister;
  561. begin
  562. i.enum:=R_INTREGISTER;
  563. i.number:=regnum_search(s);
  564. if i.number=NR_NO then
  565. begin
  566. findregister.enum := R_NO;
  567. for i.enum:=firstasmreg to lastasmreg do
  568. if s = iasmregs[i.enum] then
  569. begin
  570. findregister := i;
  571. exit;
  572. end;
  573. if s = 'A7' then
  574. begin
  575. findregister.enum := R_SP;
  576. exit;
  577. end;
  578. end
  579. else
  580. findregister:=i;
  581. end;
  582. function findopcode(s: string; var opsize: topsize): tasmop;
  583. {*********************************************************************}
  584. { FUNCTION findopcode(s: string): tasmop; }
  585. { Description: Determines if the s string is a valid opcode }
  586. { if so returns correct tasmop token. }
  587. {*********************************************************************}
  588. var
  589. i: tasmop;
  590. j: byte;
  591. op_size: string;
  592. begin
  593. findopcode := A_NONE;
  594. j:=pos('.',s);
  595. if j<>0 then
  596. begin
  597. op_size:=copy(s,j+1,1);
  598. case op_size[1] of
  599. { For the motorola only opsize size is used to }
  600. { determine the size of the operands. }
  601. 'B': opsize := S_B;
  602. 'W': opsize := S_W;
  603. 'L': opsize := S_L;
  604. 'S': opsize := S_FS;
  605. 'D': opsize := S_FD;
  606. 'X': opsize := S_FX;
  607. else
  608. Message1(asmr_e_unknown_opcode,s);
  609. end;
  610. { delete everything starting from dot }
  611. delete(s,j,length(s));
  612. end;
  613. for i:=firstop to lastop do
  614. if s = iasmops^[i] then
  615. begin
  616. findopcode:=i;
  617. exit;
  618. end;
  619. end;
  620. Function BuildExpression(allow_symbol : boolean; asmsym : pstring) : longint;
  621. {*********************************************************************}
  622. { FUNCTION BuildExpression: longint }
  623. { Description: This routine calculates a constant expression to }
  624. { a given value. The return value is the value calculated from }
  625. { the expression. }
  626. { The following tokens (not strings) are recognized: }
  627. { (,),SHL,SHR,/,*,NOT,OR,XOR,AND,MOD,+/-,numbers,ID to constants. }
  628. {*********************************************************************}
  629. { ENTRY: On entry the token should be any valid expression token. }
  630. { EXIT: On Exit the token points to either COMMA or SEPARATOR }
  631. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  632. { invalid tokens. }
  633. {*********************************************************************}
  634. var expr: string;
  635. hs, tempstr: string;
  636. sym : tsym;
  637. srsymtable : tsymtable;
  638. hl : tasmlabel;
  639. l : longint;
  640. errorflag: boolean;
  641. begin
  642. errorflag := FALSE;
  643. expr := '';
  644. tempstr := '';
  645. if allow_symbol then
  646. asmsym^:='';
  647. Repeat
  648. Case actasmtoken of
  649. AS_LPAREN: begin
  650. Consume(AS_LPAREN);
  651. expr := expr + '(';
  652. end;
  653. AS_RPAREN: begin
  654. Consume(AS_RPAREN);
  655. expr := expr + ')';
  656. end;
  657. AS_SHL: begin
  658. Consume(AS_SHL);
  659. expr := expr + '<';
  660. end;
  661. AS_SHR: begin
  662. Consume(AS_SHR);
  663. expr := expr + '>';
  664. end;
  665. AS_SLASH: begin
  666. Consume(AS_SLASH);
  667. expr := expr + '/';
  668. end;
  669. AS_MOD: begin
  670. Consume(AS_MOD);
  671. expr := expr + '%';
  672. end;
  673. AS_STAR: begin
  674. Consume(AS_STAR);
  675. expr := expr + '*';
  676. end;
  677. AS_PLUS: begin
  678. Consume(AS_PLUS);
  679. expr := expr + '+';
  680. end;
  681. AS_MINUS: begin
  682. Consume(AS_MINUS);
  683. expr := expr + '-';
  684. end;
  685. AS_AND: begin
  686. Consume(AS_AND);
  687. expr := expr + '&';
  688. end;
  689. AS_NOT: begin
  690. Consume(AS_NOT);
  691. expr := expr + '~';
  692. end;
  693. AS_XOR: begin
  694. Consume(AS_XOR);
  695. expr := expr + '^';
  696. end;
  697. AS_OR: begin
  698. Consume(AS_OR);
  699. expr := expr + '|';
  700. end;
  701. AS_ID: begin
  702. if SearchIConstant(actasmpattern,l) then
  703. begin
  704. str(l, tempstr);
  705. expr := expr + tempstr;
  706. Consume(AS_ID);
  707. End else
  708. if not allow_symbol then
  709. begin
  710. Message(asmr_e_syn_constant);
  711. l := 0;
  712. End else
  713. begin
  714. hs:='';
  715. if (expr[Length(expr)]='+') then
  716. Delete(expr,Length(expr),1)
  717. else if expr<>'' then
  718. begin
  719. Message(asmr_e_invalid_constant_expression);
  720. break;
  721. End;
  722. tempstr:=actasmpattern;
  723. consume(AS_ID);
  724. if (length(tempstr)>1) and (tempstr[1]='@') then
  725. begin
  726. CreateLocalLabel(tempstr,hl,false);
  727. hs:=hl.name
  728. end
  729. else if SearchLabel(tempstr,hl,false) then
  730. hs:=hl.name
  731. else
  732. begin
  733. searchsym(tempstr,sym,srsymtable);
  734. if assigned(sym) then
  735. begin
  736. case sym.typ of
  737. varsym :
  738. begin
  739. if sym.owner.symtabletype in [localsymtable,parasymtable] then
  740. Message(asmr_e_no_local_or_para_allowed);
  741. hs:=tvarsym(sym).mangledname;
  742. end;
  743. typedconstsym :
  744. hs:=ttypedconstsym(sym).mangledname;
  745. procsym :
  746. begin
  747. if tprocsym(sym).procdef_count>1 then
  748. Message(asmr_w_calling_overload_func);
  749. hs:=tprocsym(sym).first_procdef.mangledname;
  750. end;
  751. typesym :
  752. begin
  753. if not(ttypesym(sym).restype.def.deftype in [recorddef,objectdef]) then
  754. Message(asmr_e_wrong_sym_type);
  755. end;
  756. else
  757. Message(asmr_e_wrong_sym_type);
  758. end;
  759. end
  760. else
  761. Message1(sym_e_unknown_id,tempstr);
  762. end;
  763. { symbol found? }
  764. if hs<>'' then
  765. begin
  766. if asmsym^='' then
  767. asmsym^:=hs
  768. else
  769. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  770. end;
  771. end;
  772. end;
  773. AS_INTNUM: begin
  774. expr := expr + actasmpattern;
  775. Consume(AS_INTNUM);
  776. end;
  777. AS_BINNUM: begin
  778. tempstr := tostr(ValBinary(actasmpattern));
  779. if tempstr = '' then
  780. Message(asmr_e_error_converting_binary);
  781. expr:=expr+tempstr;
  782. Consume(AS_BINNUM);
  783. end;
  784. AS_HEXNUM: begin
  785. tempstr := tostr(ValHexadecimal(actasmpattern));
  786. if tempstr = '' then
  787. Message(asmr_e_error_converting_hexadecimal);
  788. expr:=expr+tempstr;
  789. Consume(AS_HEXNUM);
  790. end;
  791. AS_OCTALNUM: begin
  792. tempstr := tostr(ValOctal(actasmpattern));
  793. if tempstr = '' then
  794. Message(asmr_e_error_converting_octal);
  795. expr:=expr+tempstr;
  796. Consume(AS_OCTALNUM);
  797. end;
  798. { go to next term }
  799. AS_COMMA: begin
  800. if not ErrorFlag then
  801. BuildExpression := CalculateExpression(expr)
  802. else
  803. BuildExpression := 0;
  804. Exit;
  805. end;
  806. { go to next symbol }
  807. AS_SEPARATOR: begin
  808. if not ErrorFlag then
  809. BuildExpression := CalculateExpression(expr)
  810. else
  811. BuildExpression := 0;
  812. Exit;
  813. end;
  814. else
  815. begin
  816. { only write error once. }
  817. if not errorflag then
  818. Message(asmr_e_invalid_constant_expression);
  819. { consume tokens until we find COMMA or SEPARATOR }
  820. Consume(actasmtoken);
  821. errorflag := TRUE;
  822. End;
  823. end;
  824. Until false;
  825. end;
  826. Procedure BuildRealConstant(typ : tfloattype);
  827. {*********************************************************************}
  828. { PROCEDURE BuilRealConst }
  829. { Description: This routine calculates a constant expression to }
  830. { a given value. The return value is the value calculated from }
  831. { the expression. }
  832. { The following tokens (not strings) are recognized: }
  833. { +/-,numbers and real numbers }
  834. {*********************************************************************}
  835. { ENTRY: On entry the token should be any valid expression token. }
  836. { EXIT: On Exit the token points to either COMMA or SEPARATOR }
  837. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  838. { invalid tokens. }
  839. {*********************************************************************}
  840. var expr: string;
  841. r : extended;
  842. code : word;
  843. negativ : boolean;
  844. errorflag: boolean;
  845. begin
  846. errorflag := FALSE;
  847. Repeat
  848. negativ:=false;
  849. expr := '';
  850. if actasmtoken=AS_PLUS then Consume(AS_PLUS)
  851. else if actasmtoken=AS_MINUS then
  852. begin
  853. negativ:=true;
  854. consume(AS_MINUS);
  855. end;
  856. Case actasmtoken of
  857. AS_INTNUM: begin
  858. expr := actasmpattern;
  859. Consume(AS_INTNUM);
  860. end;
  861. AS_REALNUM: begin
  862. expr := actasmpattern;
  863. { in ATT syntax you have 0d in front of the real }
  864. { should this be forced ? yes i think so, as to }
  865. { conform to gas as much as possible. }
  866. if (expr[1]='0') and (upper(expr[2])='D') then
  867. expr:=copy(expr,3,255);
  868. Consume(AS_REALNUM);
  869. end;
  870. AS_BINNUM: begin
  871. { checking for real constants with this should use }
  872. { real DECODING otherwise the compiler will crash! }
  873. Message(asmr_e_invalid_float_expr);
  874. expr:='0.0';
  875. Consume(AS_BINNUM);
  876. end;
  877. AS_HEXNUM: begin
  878. { checking for real constants with this should use }
  879. { real DECODING otherwise the compiler will crash! }
  880. Message(asmr_e_invalid_float_expr);
  881. expr:='0.0';
  882. Consume(AS_HEXNUM);
  883. end;
  884. AS_OCTALNUM: begin
  885. { checking for real constants with this should use }
  886. { real DECODING otherwise the compiler will crash! }
  887. { xxxToDec using reals could be a solution, but the }
  888. { problem is that these will crash the m68k compiler }
  889. { when compiling -- because of lack of good fpu }
  890. { support. }
  891. Message(asmr_e_invalid_float_expr);
  892. expr:='0.0';
  893. Consume(AS_OCTALNUM);
  894. end;
  895. else
  896. begin
  897. { only write error once. }
  898. if not errorflag then
  899. Message(asmr_e_invalid_float_expr);
  900. { consume tokens until we find COMMA or SEPARATOR }
  901. Consume(actasmtoken);
  902. errorflag := TRUE;
  903. End;
  904. end;
  905. { go to next term }
  906. if (actasmtoken=AS_COMMA) or (actasmtoken=AS_SEPARATOR) then
  907. begin
  908. if negativ then expr:='-'+expr;
  909. val(expr,r,code);
  910. if code<>0 then
  911. begin
  912. r:=0;
  913. Message(asmr_e_invalid_float_expr);
  914. ConcatRealConstant(curlist,r,typ);
  915. End
  916. else
  917. begin
  918. ConcatRealConstant(curlist,r,typ);
  919. End;
  920. end
  921. else
  922. Message(asmr_e_invalid_float_expr);
  923. Until actasmtoken=AS_SEPARATOR;
  924. end;
  925. Procedure BuildConstant(maxvalue: longint);
  926. {*********************************************************************}
  927. { PROCEDURE BuildConstant }
  928. { Description: This routine takes care of parsing a DB,DD,or DW }
  929. { line and adding those to the assembler node. Expressions, range- }
  930. { checking are fullly taken care of. }
  931. { maxvalue: $ff -> indicates that this is a DB node. }
  932. { $ffff -> indicates that this is a DW node. }
  933. { $ffffffff -> indicates that this is a DD node. }
  934. {*********************************************************************}
  935. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  936. {*********************************************************************}
  937. var
  938. strlength: byte;
  939. expr: string;
  940. tempstr: string;
  941. value : longint;
  942. begin
  943. Repeat
  944. Case actasmtoken of
  945. AS_STRING: begin
  946. if maxvalue = $ff then
  947. strlength := 1
  948. else
  949. Message(asmr_e_string_not_allowed_as_const);
  950. expr := actasmpattern;
  951. if length(expr) > 1 then
  952. Message(asmr_e_string_not_allowed_as_const);
  953. Consume(AS_STRING);
  954. Case actasmtoken of
  955. AS_COMMA: Consume(AS_COMMA);
  956. AS_SEPARATOR: ;
  957. else
  958. Message(asmr_e_invalid_string_expression);
  959. end; { end case }
  960. ConcatString(curlist,expr);
  961. end;
  962. AS_INTNUM,AS_BINNUM,
  963. AS_OCTALNUM,AS_HEXNUM:
  964. begin
  965. value:=BuildExpression(false,nil);
  966. ConcatConstant(curlist,value,maxvalue);
  967. end;
  968. AS_ID:
  969. begin
  970. value:=BuildExpression(false,nil);
  971. if value > maxvalue then
  972. begin
  973. Message(asmr_e_constant_out_of_bounds);
  974. { assuming a value of maxvalue }
  975. value := maxvalue;
  976. end;
  977. ConcatConstant(curlist,value,maxvalue);
  978. end;
  979. { These terms can start an assembler expression }
  980. AS_PLUS,AS_MINUS,AS_LPAREN,AS_NOT: begin
  981. value := BuildExpression(false,nil);
  982. ConcatConstant(curlist,value,maxvalue);
  983. end;
  984. AS_COMMA: begin
  985. Consume(AS_COMMA);
  986. END;
  987. AS_SEPARATOR: ;
  988. else
  989. begin
  990. Message(asmr_e_syntax_error);
  991. end;
  992. end; { end case }
  993. Until actasmtoken = AS_SEPARATOR;
  994. end;
  995. {****************************************************************************
  996. Tm68kOperand
  997. ****************************************************************************}
  998. type
  999. TM68kOperand=class(TOperand)
  1000. Procedure BuildOperand;override;
  1001. private
  1002. labeled : boolean;
  1003. Procedure BuildReference;
  1004. Function BuildRefExpression: longint;
  1005. Procedure BuildScaling;
  1006. end;
  1007. Procedure TM68kOperand.BuildScaling;
  1008. {*********************************************************************}
  1009. { Takes care of parsing expression starting from the scaling value }
  1010. { up to and including possible field specifiers. }
  1011. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR }
  1012. { or AS_COMMA. On entry should point to the AS_STAR token. }
  1013. {*********************************************************************}
  1014. var str:string;
  1015. l: longint;
  1016. code: integer;
  1017. begin
  1018. Consume(AS_STAR);
  1019. if (opr.ref.scalefactor <> 0)
  1020. and (opr.ref.scalefactor <> 1) then
  1021. Message(asmr_e_wrong_base_index);
  1022. case actasmtoken of
  1023. AS_INTNUM: str := actasmpattern;
  1024. AS_HEXNUM: str := Tostr(ValHexadecimal(actasmpattern));
  1025. AS_BINNUM: str := Tostr(ValBinary(actasmpattern));
  1026. AS_OCTALNUM: str := Tostr(ValOctal(actasmpattern));
  1027. else
  1028. Message(asmr_e_syntax_error);
  1029. end;
  1030. val(str, l, code);
  1031. if code <> 0 then
  1032. Message(asmr_e_wrong_scale_factor);
  1033. if ((l = 2) or (l = 4) or (l = 8) or (l = 1)) and (code = 0) then
  1034. begin
  1035. opr.ref.scalefactor := l;
  1036. end
  1037. else
  1038. begin
  1039. Message(asmr_e_wrong_scale_factor);
  1040. opr.ref.scalefactor := 0;
  1041. end;
  1042. if opr.ref.index.enum = R_NO then
  1043. begin
  1044. Message(asmr_e_wrong_base_index);
  1045. opr.ref.scalefactor := 0;
  1046. end;
  1047. { Consume the scaling number }
  1048. Consume(actasmtoken);
  1049. if actasmtoken = AS_RPAREN then
  1050. Consume(AS_RPAREN)
  1051. else
  1052. Message(asmr_e_wrong_scale_factor);
  1053. { // .Field.Field ... or separator/comma // }
  1054. if actasmtoken in [AS_COMMA,AS_SEPARATOR] then
  1055. begin
  1056. end
  1057. else
  1058. Message(asmr_e_syntax_error);
  1059. end;
  1060. Function TM68kOperand.BuildRefExpression: longint;
  1061. {*********************************************************************}
  1062. { FUNCTION BuildRefExpression: longint }
  1063. { Description: This routine calculates a constant expression to }
  1064. { a given value. The return value is the value calculated from }
  1065. { the expression. }
  1066. { The following tokens (not strings) are recognized: }
  1067. { SHL,SHR,/,*,NOT,OR,XOR,AND,MOD,+/-,numbers,ID to constants. }
  1068. {*********************************************************************}
  1069. { ENTRY: On entry the token should be any valid expression token. }
  1070. { EXIT: On Exit the token points to the LPAREN token. }
  1071. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  1072. { invalid tokens. }
  1073. {*********************************************************************}
  1074. var tempstr: string;
  1075. expr: string;
  1076. l : longint;
  1077. errorflag : boolean;
  1078. begin
  1079. errorflag := FALSE;
  1080. tempstr := '';
  1081. expr := '';
  1082. Repeat
  1083. Case actasmtoken of
  1084. AS_RPAREN: begin
  1085. Message(asmr_e_syntax_error);
  1086. Consume(AS_RPAREN);
  1087. end;
  1088. AS_SHL: begin
  1089. Consume(AS_SHL);
  1090. expr := expr + '<';
  1091. end;
  1092. AS_SHR: begin
  1093. Consume(AS_SHR);
  1094. expr := expr + '>';
  1095. end;
  1096. AS_SLASH: begin
  1097. Consume(AS_SLASH);
  1098. expr := expr + '/';
  1099. end;
  1100. AS_MOD: begin
  1101. Consume(AS_MOD);
  1102. expr := expr + '%';
  1103. end;
  1104. AS_STAR: begin
  1105. Consume(AS_STAR);
  1106. expr := expr + '*';
  1107. end;
  1108. AS_PLUS: begin
  1109. Consume(AS_PLUS);
  1110. expr := expr + '+';
  1111. end;
  1112. AS_MINUS: begin
  1113. Consume(AS_MINUS);
  1114. expr := expr + '-';
  1115. end;
  1116. AS_AND: begin
  1117. Consume(AS_AND);
  1118. expr := expr + '&';
  1119. end;
  1120. AS_NOT: begin
  1121. Consume(AS_NOT);
  1122. expr := expr + '~';
  1123. end;
  1124. AS_XOR: begin
  1125. Consume(AS_XOR);
  1126. expr := expr + '^';
  1127. end;
  1128. AS_OR: begin
  1129. Consume(AS_OR);
  1130. expr := expr + '|';
  1131. end;
  1132. { End of reference }
  1133. AS_LPAREN: begin
  1134. if not ErrorFlag then
  1135. BuildRefExpression := CalculateExpression(expr)
  1136. else
  1137. BuildRefExpression := 0;
  1138. { no longer in an expression }
  1139. exit;
  1140. end;
  1141. AS_ID:
  1142. begin
  1143. if NOT SearchIConstant(actasmpattern,l) then
  1144. begin
  1145. Message(asmr_e_syn_constant);
  1146. l := 0;
  1147. end;
  1148. str(l, tempstr);
  1149. expr := expr + tempstr;
  1150. Consume(AS_ID);
  1151. end;
  1152. AS_INTNUM: begin
  1153. expr := expr + actasmpattern;
  1154. Consume(AS_INTNUM);
  1155. end;
  1156. AS_BINNUM: begin
  1157. tempstr := Tostr(ValBinary(actasmpattern));
  1158. if tempstr = '' then
  1159. Message(asmr_e_error_converting_binary);
  1160. expr:=expr+tempstr;
  1161. Consume(AS_BINNUM);
  1162. end;
  1163. AS_HEXNUM: begin
  1164. tempstr := Tostr(ValHexadecimal(actasmpattern));
  1165. if tempstr = '' then
  1166. Message(asmr_e_error_converting_hexadecimal);
  1167. expr:=expr+tempstr;
  1168. Consume(AS_HEXNUM);
  1169. end;
  1170. AS_OCTALNUM: begin
  1171. tempstr := Tostr(ValOctal(actasmpattern));
  1172. if tempstr = '' then
  1173. Message(asmr_e_error_converting_octal);
  1174. expr:=expr+tempstr;
  1175. Consume(AS_OCTALNUM);
  1176. end;
  1177. else
  1178. begin
  1179. { write error only once. }
  1180. if not errorflag then
  1181. Message(asmr_e_invalid_constant_expression);
  1182. BuildRefExpression := 0;
  1183. if actasmtoken in [AS_COMMA,AS_SEPARATOR] then exit;
  1184. { consume tokens until we find COMMA or SEPARATOR }
  1185. Consume(actasmtoken);
  1186. errorflag := TRUE;
  1187. end;
  1188. end;
  1189. Until false;
  1190. end;
  1191. {*********************************************************************}
  1192. { PROCEDURE BuildBracketExpression }
  1193. { Description: This routine builds up an expression after a LPAREN }
  1194. { token is encountered. }
  1195. { On entry actasmtoken should be equal to AS_LPAREN }
  1196. {*********************************************************************}
  1197. { EXIT CONDITION: On exit the routine should point to either the }
  1198. { AS_COMMA or AS_SEPARATOR token. }
  1199. {*********************************************************************}
  1200. procedure TM68kOperand.BuildReference;
  1201. var
  1202. l:longint;
  1203. code: integer;
  1204. str: string;
  1205. begin
  1206. Consume(AS_LPAREN);
  1207. case actasmtoken of
  1208. { // (reg ... // }
  1209. AS_REGISTER:
  1210. begin
  1211. opr.ref.base := findregister(actasmpattern);
  1212. Consume(AS_REGISTER);
  1213. { can either be a register or a right parenthesis }
  1214. { // (reg) // }
  1215. { // (reg)+ // }
  1216. if actasmtoken=AS_RPAREN then
  1217. begin
  1218. Consume(AS_RPAREN);
  1219. if actasmtoken = AS_PLUS then
  1220. begin
  1221. if (opr.ref.direction <> dir_none) then
  1222. Message(asmr_e_no_inc_and_dec_together)
  1223. else
  1224. opr.ref.direction := dir_inc;
  1225. Consume(AS_PLUS);
  1226. end;
  1227. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR]) then
  1228. begin
  1229. Message(asmr_e_invalid_reference_syntax);
  1230. { error recovery ... }
  1231. while actasmtoken <> AS_SEPARATOR do
  1232. Consume(actasmtoken);
  1233. end;
  1234. exit;
  1235. end;
  1236. { // (reg,reg .. // }
  1237. Consume(AS_COMMA);
  1238. if actasmtoken = AS_REGISTER then
  1239. begin
  1240. opr.ref.index :=
  1241. findregister(actasmpattern);
  1242. Consume(AS_REGISTER);
  1243. { check for scaling ... }
  1244. case actasmtoken of
  1245. AS_RPAREN:
  1246. begin
  1247. Consume(AS_RPAREN);
  1248. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR]) then
  1249. begin
  1250. { error recovery ... }
  1251. Message(asmr_e_invalid_reference_syntax);
  1252. while actasmtoken <> AS_SEPARATOR do
  1253. Consume(actasmtoken);
  1254. end;
  1255. exit;
  1256. end;
  1257. AS_STAR:
  1258. begin
  1259. BuildScaling;
  1260. end;
  1261. else
  1262. begin
  1263. Message(asmr_e_invalid_reference_syntax);
  1264. while (actasmtoken <> AS_SEPARATOR) do
  1265. Consume(actasmtoken);
  1266. end;
  1267. end; { end case }
  1268. end
  1269. else
  1270. begin
  1271. Message(asmr_e_invalid_reference_syntax);
  1272. while (actasmtoken <> AS_SEPARATOR) do
  1273. Consume(actasmtoken);
  1274. end;
  1275. end;
  1276. AS_HEXNUM,AS_OCTALNUM, { direct address }
  1277. AS_BINNUM,AS_INTNUM:
  1278. begin
  1279. case actasmtoken of
  1280. AS_INTNUM: str := actasmpattern;
  1281. AS_HEXNUM: str := Tostr(ValHexadecimal(actasmpattern));
  1282. AS_BINNUM: str := Tostr(ValBinary(actasmpattern));
  1283. AS_OCTALNUM: str := Tostr(ValOctal(actasmpattern));
  1284. else
  1285. Message(asmr_e_syntax_error);
  1286. end;
  1287. Consume(actasmtoken);
  1288. val(str, l, code);
  1289. if code <> 0 then
  1290. Message(asmr_e_invalid_reference_syntax)
  1291. else
  1292. opr.ref.offset := l;
  1293. Consume(AS_RPAREN);
  1294. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR]) then
  1295. begin
  1296. { error recovery ... }
  1297. Message(asmr_e_invalid_reference_syntax);
  1298. while actasmtoken <> AS_SEPARATOR do
  1299. Consume(actasmtoken);
  1300. end;
  1301. exit;
  1302. end;
  1303. else
  1304. begin
  1305. Message(asmr_e_invalid_reference_syntax);
  1306. while (actasmtoken <> AS_SEPARATOR) do
  1307. Consume(actasmtoken);
  1308. end;
  1309. end;
  1310. end;
  1311. Procedure TM68kOperand.BuildOperand;
  1312. {*********************************************************************}
  1313. { EXIT CONDITION: On exit the routine should point to either the }
  1314. { AS_COMMA or AS_SEPARATOR token. }
  1315. {*********************************************************************}
  1316. var
  1317. tempstr: string;
  1318. expr: string;
  1319. lab: tasmlabel;
  1320. l : longint;
  1321. i: Tsuperregister;
  1322. r:Tregister;
  1323. hl: tasmlabel;
  1324. reg_one, reg_two: tregister;
  1325. reglist: Tsupregset;
  1326. begin
  1327. reglist := [];
  1328. tempstr := '';
  1329. expr := '';
  1330. case actasmtoken of
  1331. { // Memory reference // }
  1332. AS_LPAREN:
  1333. begin
  1334. InitRef;
  1335. BuildReference;
  1336. end;
  1337. { // Constant expression // }
  1338. AS_APPT: begin
  1339. Consume(AS_APPT);
  1340. if not (opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1341. Message(asmr_e_invalid_operand_type);
  1342. { identifiers are handled by BuildExpression }
  1343. opr.typ := OPR_CONSTANT;
  1344. opr.val :=BuildExpression(true,@tempstr);
  1345. if tempstr<>'' then
  1346. begin
  1347. l:=opr.val;
  1348. opr.typ := OPR_SYMBOL;
  1349. opr.symofs := l;
  1350. opr.symbol := objectlibrary.newasmsymbol(tempstr,AB_EXTERNAL,AT_FUNCTION);
  1351. end;
  1352. end;
  1353. { // Constant memory offset . // }
  1354. { // This must absolutely be followed by ( // }
  1355. AS_HEXNUM,AS_INTNUM,
  1356. AS_BINNUM,AS_OCTALNUM,AS_PLUS:
  1357. begin
  1358. InitRef;
  1359. opr.ref.offset:=BuildRefExpression;
  1360. BuildReference;
  1361. end;
  1362. { // A constant expression, or a Variable ref. // }
  1363. AS_ID: begin
  1364. InitRef;
  1365. if actasmpattern[1] = '@' then
  1366. { // Label or Special symbol reference // }
  1367. begin
  1368. if actasmpattern = '@RESULT' then
  1369. SetUpResult
  1370. else
  1371. if actasmpattern = 'SELF' then
  1372. SetUpSelf
  1373. else
  1374. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  1375. Message(asmr_w_CODE_and_DATA_not_supported)
  1376. else
  1377. begin
  1378. delete(actasmpattern,1,1);
  1379. if actasmpattern = '' then
  1380. Message(asmr_e_null_label_ref_not_allowed);
  1381. CreateLocalLabel(actasmpattern,lab,false);
  1382. opr.typ := OPR_SYMBOL;
  1383. opr.symbol := lab;
  1384. opr.symofs := 0;
  1385. labeled := TRUE;
  1386. end;
  1387. Consume(AS_ID);
  1388. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1389. Message(asmr_e_syntax_error);
  1390. end
  1391. { probably a variable or normal expression }
  1392. { or a procedure (such as in CALL ID) }
  1393. else
  1394. begin
  1395. { is it a constant ? }
  1396. if SearchIConstant(actasmpattern,l) then
  1397. begin
  1398. InitRef;
  1399. opr.ref.offset:=BuildRefExpression;
  1400. BuildReference;
  1401. end
  1402. else { is it a label variable ? }
  1403. begin
  1404. { // ID[ , ID.Field.Field or simple ID // }
  1405. { check if this is a label, if so then }
  1406. { emit it as a label. }
  1407. if SearchLabel(actasmpattern,hl,false) then
  1408. begin
  1409. opr.typ := OPR_SYMBOL;
  1410. opr.symbol := hl;
  1411. opr.symofs := 0;
  1412. labeled := TRUE;
  1413. Consume(AS_ID);
  1414. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1415. Message(asmr_e_syntax_error);
  1416. end
  1417. else
  1418. { is it a normal variable ? }
  1419. if (cs_compilesystem in aktmoduleswitches) then
  1420. begin
  1421. if not SetupDirectVar(expr) then
  1422. begin
  1423. { not found, finally ... add it anyways ... }
  1424. Message1(asmr_w_id_supposed_external,expr);
  1425. opr.ref.symbol:=objectlibrary.newasmsymbol(expr,AB_EXTERNAL,AT_FUNCTION);
  1426. end;
  1427. end
  1428. else
  1429. Message1(sym_e_unknown_id,actasmpattern);
  1430. expr := actasmpattern;
  1431. Consume(AS_ID);
  1432. case actasmtoken of
  1433. AS_LPAREN: { indexing }
  1434. BuildReference;
  1435. AS_SEPARATOR,AS_COMMA: ;
  1436. else
  1437. Message(asmr_e_syntax_error);
  1438. end;
  1439. end;
  1440. end;
  1441. end;
  1442. { // Pre-decrement mode reference or constant mem offset. // }
  1443. AS_MINUS: begin
  1444. Consume(AS_MINUS);
  1445. if actasmtoken = AS_LPAREN then
  1446. begin
  1447. InitRef;
  1448. { indicate pre-decrement mode }
  1449. opr.ref.direction := dir_dec;
  1450. BuildReference;
  1451. end
  1452. else
  1453. if actasmtoken in [AS_OCTALNUM,AS_HEXNUM,AS_BINNUM,AS_INTNUM] then
  1454. begin
  1455. InitRef;
  1456. opr.ref.offset:=BuildRefExpression;
  1457. { negate because was preceded by a negative sign! }
  1458. opr.ref.offset:=-opr.ref.offset;
  1459. BuildReference;
  1460. end
  1461. else
  1462. begin
  1463. Message(asmr_e_syntax_error);
  1464. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1465. Consume(actasmtoken);
  1466. end;
  1467. end;
  1468. { // Register, a variable reference or a constant reference // }
  1469. AS_REGISTER: begin
  1470. { save the type of register used. }
  1471. tempstr := actasmpattern;
  1472. Consume(AS_REGISTER);
  1473. { // Simple register // }
  1474. if (actasmtoken = AS_SEPARATOR) or (actasmtoken = AS_COMMA) then
  1475. begin
  1476. if not (opr.typ in [OPR_NONE,OPR_REGISTER]) then
  1477. Message(asmr_e_invalid_operand_type);
  1478. opr.typ := OPR_REGISTER;
  1479. opr.reg := findregister(tempstr);
  1480. end
  1481. else
  1482. { HERE WE MUST HANDLE THE SPECIAL CASE OF MOVEM AND FMOVEM }
  1483. { // Individual register listing // }
  1484. if (actasmtoken = AS_SLASH) then
  1485. begin
  1486. r:=findregister(tempstr);
  1487. if r.enum<>R_INTREGISTER then
  1488. internalerror(200302191);
  1489. reglist := [r.number shr 8];
  1490. Consume(AS_SLASH);
  1491. if actasmtoken = AS_REGISTER then
  1492. begin
  1493. While not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1494. begin
  1495. case actasmtoken of
  1496. AS_REGISTER: begin
  1497. r:=findregister(tempstr);
  1498. if r.enum<>R_INTREGISTER then
  1499. internalerror(200302191);
  1500. reglist := reglist + [r.number shr 8];
  1501. Consume(AS_REGISTER);
  1502. end;
  1503. AS_SLASH: Consume(AS_SLASH);
  1504. AS_SEPARATOR,AS_COMMA: break;
  1505. else
  1506. begin
  1507. Message(asmr_e_invalid_reg_list_in_movem);
  1508. Consume(actasmtoken);
  1509. end;
  1510. end; { end case }
  1511. end; { end while }
  1512. opr.typ:= OPR_REGLIST;
  1513. opr.reglist := reglist;
  1514. end
  1515. else
  1516. { error recovery ... }
  1517. begin
  1518. Message(asmr_e_invalid_reg_list_in_movem);
  1519. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1520. Consume(actasmtoken);
  1521. end;
  1522. end
  1523. else
  1524. { // Range register listing // }
  1525. if (actasmtoken = AS_MINUS) then
  1526. begin
  1527. Consume(AS_MINUS);
  1528. reg_one:=findregister(tempstr);
  1529. if actasmtoken <> AS_REGISTER then
  1530. begin
  1531. Message(asmr_e_invalid_reg_list_in_movem);
  1532. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1533. Consume(actasmtoken);
  1534. end
  1535. else
  1536. begin
  1537. { determine the register range ... }
  1538. reg_two:=findregister(actasmpattern);
  1539. if reg_two.enum<>R_INTREGISTER then
  1540. internalerror(200302191);
  1541. if reg_one.enum > reg_two.enum then
  1542. for i:=reg_two.number shr 8 to reg_one.number shr 8 do
  1543. reglist:=reglist+[i]
  1544. else
  1545. for i:=reg_one.number shr 8 to reg_two.number shr 8 do
  1546. reglist:=reglist+[i];
  1547. Consume(AS_REGISTER);
  1548. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1549. begin
  1550. Message(asmr_e_invalid_reg_list_in_movem);
  1551. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1552. Consume(actasmtoken);
  1553. end;
  1554. { set up instruction }
  1555. opr.typ:= OPR_REGLIST;
  1556. opr.reglist := reglist;
  1557. end;
  1558. end
  1559. else
  1560. { DIVSL/DIVS/MULS/MULU with long for MC68020 only }
  1561. if (actasmtoken = AS_COLON) then
  1562. begin
  1563. if (aktoptprocessor = MC68020) or (cs_compilesystem in aktmoduleswitches) then
  1564. begin
  1565. Consume(AS_COLON);
  1566. if (actasmtoken = AS_REGISTER) then
  1567. begin
  1568. { set up old field, since register is valid }
  1569. opr.typ := OPR_REGISTER;
  1570. opr.reg := findregister(tempstr);
  1571. Inc(operandnum);
  1572. opr.typ := OPR_REGISTER;
  1573. opr.reg := findregister(actasmpattern);
  1574. Consume(AS_REGISTER);
  1575. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1576. begin
  1577. Message(asmr_e_invalid_reg_list_for_opcode);
  1578. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1579. Consume(actasmtoken);
  1580. end;
  1581. end;
  1582. end
  1583. else
  1584. begin
  1585. Message1(asmr_e_higher_cpu_mode_required,'68020');
  1586. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1587. begin
  1588. Message(asmr_e_invalid_reg_list_for_opcode);
  1589. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1590. Consume(actasmtoken);
  1591. end;
  1592. end;
  1593. end
  1594. else
  1595. Message(asmr_e_invalid_register);
  1596. end;
  1597. AS_SEPARATOR, AS_COMMA: ;
  1598. else
  1599. begin
  1600. Message(asmr_e_invalid_opcode_and_operand);
  1601. Consume(actasmtoken);
  1602. end;
  1603. end; { end case }
  1604. end;
  1605. Procedure BuildStringConstant(asciiz: boolean);
  1606. {*********************************************************************}
  1607. { PROCEDURE BuildStringConstant }
  1608. { Description: Takes care of a ASCII, or ASCIIZ directive. }
  1609. { asciiz: boolean -> if true then string will be null terminated. }
  1610. {*********************************************************************}
  1611. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  1612. { On ENTRY: Token should point to AS_STRING }
  1613. {*********************************************************************}
  1614. var
  1615. expr: string;
  1616. errorflag : boolean;
  1617. begin
  1618. errorflag := FALSE;
  1619. Repeat
  1620. Case actasmtoken of
  1621. AS_STRING: begin
  1622. expr:=actasmpattern;
  1623. if asciiz then
  1624. expr:=expr+#0;
  1625. ConcatPasString(curlist,expr);
  1626. Consume(AS_STRING);
  1627. end;
  1628. AS_COMMA: begin
  1629. Consume(AS_COMMA);
  1630. END;
  1631. AS_SEPARATOR: ;
  1632. else
  1633. begin
  1634. Consume(actasmtoken);
  1635. if not errorflag then
  1636. Message(asmr_e_invalid_string_expression);
  1637. errorflag := TRUE;
  1638. end;
  1639. end; { end case }
  1640. Until actasmtoken = AS_SEPARATOR;
  1641. end;
  1642. {*****************************************************************************
  1643. TM68kInstruction
  1644. *****************************************************************************}
  1645. type
  1646. TM68kInstruction=class(TInstruction)
  1647. procedure InitOperands;override;
  1648. procedure BuildOpcode;override;
  1649. procedure ConcatInstruction(p : taasmoutput);override;
  1650. Procedure ConcatLabeledInstr(p : taasmoutput);
  1651. end;
  1652. procedure TM68kInstruction.InitOperands;
  1653. var
  1654. i : longint;
  1655. begin
  1656. for i:=1 to max_operands do
  1657. Operands[i]:=TM68kOperand.Create;
  1658. end;
  1659. Procedure TM68kInstruction.BuildOpCode;
  1660. {*********************************************************************}
  1661. { PROCEDURE BuildOpcode; }
  1662. { Description: Parses the intel opcode and operands, and writes it }
  1663. { in the TInstruction object. }
  1664. {*********************************************************************}
  1665. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  1666. { On ENTRY: Token should point to AS_OPCODE }
  1667. {*********************************************************************}
  1668. var asmtok: tasmop;
  1669. expr: string;
  1670. operandnum : longint;
  1671. begin
  1672. expr := '';
  1673. asmtok := A_NONE; { assmume no prefix }
  1674. { // opcode // }
  1675. { allow for newline as in gas styled syntax }
  1676. { under DOS you get two AS_SEPARATOR !! }
  1677. while actasmtoken=AS_SEPARATOR do
  1678. Consume(AS_SEPARATOR);
  1679. if (actasmtoken <> AS_OPCODE) then
  1680. begin
  1681. Message(asmr_e_invalid_or_missing_opcode);
  1682. { error recovery }
  1683. While not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1684. Consume(actasmtoken);
  1685. exit;
  1686. end
  1687. else
  1688. begin
  1689. opcode := findopcode(actasmpattern,opsize);
  1690. Consume(AS_OPCODE);
  1691. { // Zero operand opcode ? // }
  1692. if actasmtoken = AS_SEPARATOR then
  1693. exit
  1694. else
  1695. operandnum := 1;
  1696. end;
  1697. While actasmtoken <> AS_SEPARATOR do
  1698. begin
  1699. case actasmtoken of
  1700. { // Operand delimiter // }
  1701. AS_COMMA: begin
  1702. if operandnum > Max_Operands then
  1703. Message(asmr_e_too_many_operands)
  1704. else
  1705. Inc(operandnum);
  1706. Consume(AS_COMMA);
  1707. end;
  1708. { // End of asm operands for this opcode // }
  1709. AS_SEPARATOR: ;
  1710. else
  1711. Operands[operandnum].BuildOperand;
  1712. end; { end case }
  1713. end; { end while }
  1714. end;
  1715. procedure TM68kInstruction.ConcatInstruction(p : taasmoutput);
  1716. var
  1717. fits : boolean;
  1718. begin
  1719. fits := FALSE;
  1720. { setup specific opcodetions for first pass }
  1721. { Setup special operands }
  1722. { Convert to general form as to conform to the m68k opcode table }
  1723. if (opcode = A_ADDA) or (opcode = A_ADDI)
  1724. then opcode := A_ADD
  1725. else
  1726. { CMPM excluded because of GAS v1.34 BUG }
  1727. if (opcode = A_CMPA) or
  1728. (opcode = A_CMPI) then
  1729. opcode := A_CMP
  1730. else
  1731. if opcode = A_EORI then
  1732. opcode := A_EOR
  1733. else
  1734. if opcode = A_MOVEA then
  1735. opcode := A_MOVE
  1736. else
  1737. if opcode = A_ORI then
  1738. opcode := A_OR
  1739. else
  1740. if (opcode = A_SUBA) or (opcode = A_SUBI) then
  1741. opcode := A_SUB;
  1742. { Setup operand types }
  1743. (*
  1744. in opcode <> A_MOVEM then
  1745. begin
  1746. while not(fits) do
  1747. begin
  1748. { set the opcodetion cache, if the opcodetion }
  1749. { occurs the first time }
  1750. if (it[i].i=opcode) and (ins_cache[opcode]=-1) then
  1751. ins_cache[opcode]:=i;
  1752. if (it[i].i=opcode) and (instr.ops=it[i].ops) then
  1753. begin
  1754. { first fit }
  1755. case instr.ops of
  1756. 0 : begin
  1757. fits:=true;
  1758. break;
  1759. end;
  1760. 1 :
  1761. begin
  1762. if (optyp1 and it[i].o1)<>0 then
  1763. begin
  1764. fits:=true;
  1765. break;
  1766. end;
  1767. end;
  1768. 2 : if ((optyp1 and it[i].o1)<>0) and
  1769. ((optyp2 and it[i].o2)<>0) then
  1770. begin
  1771. fits:=true;
  1772. break;
  1773. end
  1774. 3 : if ((optyp1 and it[i].o1)<>0) and
  1775. ((optyp2 and it[i].o2)<>0) and
  1776. ((optyp3 and it[i].o3)<>0) then
  1777. begin
  1778. fits:=true;
  1779. break;
  1780. end;
  1781. end; { end case }
  1782. end; { endif }
  1783. if it[i].i=A_NONE then
  1784. begin
  1785. { NO MATCH! }
  1786. Message(asmr_e_invalid_combination_opcode_and_operand);
  1787. exit;
  1788. end;
  1789. inc(i);
  1790. end; { end while }
  1791. *)
  1792. fits:=TRUE;
  1793. { We add the opcode to the opcode linked list }
  1794. if fits then
  1795. begin
  1796. case ops of
  1797. 0:
  1798. if opsize <> S_NO then
  1799. p.concat((taicpu.op_none(opcode,opsize)))
  1800. else
  1801. p.concat((taicpu.op_none(opcode,S_NO)));
  1802. 1: begin
  1803. case operands[1].opr.typ of
  1804. OPR_SYMBOL:
  1805. begin
  1806. p.concat((taicpu.op_sym_ofs(opcode,
  1807. opsize, operands[1].opr.symbol,operands[1].opr.symofs)));
  1808. end;
  1809. OPR_CONSTANT:
  1810. begin
  1811. p.concat((taicpu.op_const(opcode,
  1812. opsize, operands[1].opr.val)));
  1813. end;
  1814. OPR_REGISTER:
  1815. p.concat((taicpu.op_reg(opcode,opsize,operands[1].opr.reg)));
  1816. OPR_REFERENCE:
  1817. if opsize <> S_NO then
  1818. begin
  1819. p.concat((taicpu.op_ref(opcode,
  1820. opsize,operands[1].opr.ref)));
  1821. end
  1822. else
  1823. begin
  1824. { special jmp and call case with }
  1825. { symbolic references. }
  1826. if opcode in [A_BSR,A_JMP,A_JSR,A_BRA,A_PEA] then
  1827. begin
  1828. p.concat((taicpu.op_ref(opcode,
  1829. S_NO,operands[1].opr.ref)));
  1830. end
  1831. else
  1832. Message(asmr_e_invalid_opcode_and_operand);
  1833. end;
  1834. OPR_NONE:
  1835. Message(asmr_e_invalid_opcode_and_operand);
  1836. else
  1837. begin
  1838. Message(asmr_e_invalid_opcode_and_operand);
  1839. end;
  1840. end;
  1841. end;
  1842. 2: begin
  1843. { source }
  1844. case operands[1].opr.typ of
  1845. { reg,reg }
  1846. { reg,ref }
  1847. OPR_REGISTER:
  1848. begin
  1849. case operands[2].opr.typ of
  1850. OPR_REGISTER:
  1851. begin
  1852. p.concat((taicpu.op_reg_reg(opcode,
  1853. opsize,operands[1].opr.reg,operands[2].opr.reg)));
  1854. end;
  1855. OPR_REFERENCE:
  1856. p.concat((taicpu.op_reg_ref(opcode,
  1857. opsize,operands[1].opr.reg,operands[2].opr.ref)));
  1858. else { else case }
  1859. begin
  1860. Message(asmr_e_invalid_opcode_and_operand);
  1861. end;
  1862. end; { end second operand case for OPR_REGISTER }
  1863. end;
  1864. { reglist, ref }
  1865. OPR_REGLIST:
  1866. begin
  1867. case operands[2].opr.typ of
  1868. OPR_REFERENCE :
  1869. p.concat((taicpu.op_reglist_ref(opcode,
  1870. opsize,operands[1].opr.reglist,operands[2].opr.ref)));
  1871. else
  1872. begin
  1873. Message(asmr_e_invalid_opcode_and_operand);
  1874. end;
  1875. end; { end second operand case for OPR_REGLIST }
  1876. end;
  1877. { const,reg }
  1878. { const,const }
  1879. { const,ref }
  1880. OPR_CONSTANT:
  1881. case operands[2].opr.typ of
  1882. { constant, constant does not have a specific size. }
  1883. OPR_CONSTANT:
  1884. p.concat((taicpu.op_const_const(opcode,
  1885. S_NO,operands[1].opr.val,operands[2].opr.val)));
  1886. OPR_REFERENCE:
  1887. begin
  1888. p.concat((taicpu.op_const_ref(opcode,
  1889. opsize,operands[1].opr.val,
  1890. operands[2].opr.ref)))
  1891. end;
  1892. OPR_REGISTER:
  1893. begin
  1894. p.concat((taicpu.op_const_reg(opcode,
  1895. opsize,operands[1].opr.val,
  1896. operands[2].opr.reg)))
  1897. end;
  1898. else
  1899. begin
  1900. Message(asmr_e_invalid_opcode_and_operand);
  1901. end;
  1902. end; { end second operand case for OPR_CONSTANT }
  1903. { ref,reg }
  1904. { ref,ref }
  1905. OPR_REFERENCE:
  1906. case operands[2].opr.typ of
  1907. OPR_REGISTER:
  1908. begin
  1909. p.concat((taicpu.op_ref_reg(opcode,
  1910. opsize,operands[1].opr.ref,
  1911. operands[2].opr.reg)));
  1912. end;
  1913. OPR_REGLIST:
  1914. begin
  1915. p.concat((taicpu.op_ref_reglist(opcode,
  1916. opsize,operands[1].opr.ref,
  1917. operands[2].opr.reglist)));
  1918. end;
  1919. OPR_REFERENCE: { special opcodes }
  1920. p.concat((taicpu.op_ref_ref(opcode,
  1921. opsize,operands[1].opr.ref,
  1922. operands[2].opr.ref)));
  1923. else
  1924. begin
  1925. Message(asmr_e_invalid_opcode_and_operand);
  1926. end;
  1927. end; { end second operand case for OPR_REFERENCE }
  1928. OPR_SYMBOL: case operands[2].opr.typ of
  1929. OPR_REFERENCE:
  1930. begin
  1931. p.concat((taicpu.op_sym_ofs_ref(opcode,
  1932. opsize,operands[1].opr.symbol,operands[1].opr.symofs,
  1933. operands[2].opr.ref)))
  1934. end;
  1935. OPR_REGISTER:
  1936. begin
  1937. p.concat((taicpu.op_sym_ofs_reg(opcode,
  1938. opsize,operands[1].opr.symbol,operands[1].opr.symofs,
  1939. operands[2].opr.reg)))
  1940. end;
  1941. else
  1942. begin
  1943. Message(asmr_e_invalid_opcode_and_operand);
  1944. end;
  1945. end; { end second operand case for OPR_SYMBOL }
  1946. else
  1947. begin
  1948. Message(asmr_e_invalid_opcode_and_operand);
  1949. end;
  1950. end; { end first operand case }
  1951. end;
  1952. 3: begin
  1953. if (opcode = A_DIVSL) or (opcode = A_DIVUL) or (opcode = A_MULU)
  1954. or (opcode = A_MULS) or (opcode = A_DIVS) or (opcode = A_DIVU) then
  1955. begin
  1956. if (operands[1].opr.typ <> OPR_REGISTER)
  1957. or (operands[2].opr.typ <> OPR_REGISTER)
  1958. or (operands[3].opr.typ <> OPR_REGISTER) then
  1959. begin
  1960. Message(asmr_e_invalid_opcode_and_operand);
  1961. end
  1962. else
  1963. begin
  1964. p.concat((taicpu. op_reg_reg_reg(opcode,opsize,
  1965. operands[1].opr.reg,operands[2].opr.reg,operands[3].opr.reg)));
  1966. end;
  1967. end
  1968. else
  1969. Message(asmr_e_invalid_opcode_and_operand);
  1970. end;
  1971. end; { end case }
  1972. end;
  1973. end;
  1974. procedure TM68kInstruction.ConcatLabeledInstr(p : taasmoutput);
  1975. begin
  1976. if ((opcode >= A_BCC) and (opcode <= A_BVS)) or
  1977. (opcode = A_BRA) or (opcode = A_BSR) or
  1978. (opcode = A_JMP) or (opcode = A_JSR) or
  1979. ((opcode >= A_FBEQ) and (opcode <= A_FBNGLE)) then
  1980. begin
  1981. if ops > 2 then
  1982. Message(asmr_e_invalid_opcode_and_operand)
  1983. else if operands[1].opr.typ <> OPR_SYMBOL then
  1984. Message(asmr_e_invalid_opcode_and_operand)
  1985. else if (operands[1].opr.typ = OPR_SYMBOL) and
  1986. (ops = 1) then
  1987. if assigned(operands[1].opr.symbol) and
  1988. (operands[1].opr.symofs=0) then
  1989. p.concat(taicpu.op_sym(opcode,S_NO,
  1990. operands[1].opr.symbol))
  1991. else
  1992. Message(asmr_e_invalid_opcode_and_operand);
  1993. end
  1994. else if ((opcode >= A_DBCC) and (opcode <= A_DBF))
  1995. or ((opcode >= A_FDBEQ) and (opcode <= A_FDBNGLE)) then
  1996. begin
  1997. if (ops<>2) or
  1998. (operands[1].opr.typ <> OPR_REGISTER) or
  1999. (operands[2].opr.typ <> OPR_SYMBOL) or
  2000. (operands[2].opr.symofs <> 0) then
  2001. Message(asmr_e_invalid_opcode_and_operand)
  2002. else
  2003. p.concat(taicpu.op_reg_sym(opcode,opsize,operands[1].opr.reg,
  2004. operands[2].opr.symbol));
  2005. end
  2006. else
  2007. Message(asmr_e_invalid_opcode_and_operand);
  2008. end;
  2009. function ti386intreader.Assemble: tlinkedlist;
  2010. var
  2011. hl: tasmlabel;
  2012. labelptr,nextlabel : tasmlabel;
  2013. commname : string;
  2014. instr : TM68kInstruction;
  2015. begin
  2016. Message(asmr_d_start_reading);
  2017. firsttoken := TRUE;
  2018. operandnum := 0;
  2019. { sets up all opcode and register tables in uppercase }
  2020. if not _asmsorted then
  2021. begin
  2022. SetupTables;
  2023. _asmsorted := TRUE;
  2024. end;
  2025. curlist:=TAAsmoutput.Create;
  2026. { setup label linked list }
  2027. LocalLabelList:=TLocalLabelList.Create;
  2028. c:=current_scanner.asmgetchar;
  2029. actasmtoken:=gettoken;
  2030. while actasmtoken<>AS_END do
  2031. begin
  2032. case actasmtoken of
  2033. AS_LLABEL:
  2034. begin
  2035. if CreateLocalLabel(actasmpattern,hl,true) then
  2036. ConcatLabel(curlist,hl);
  2037. Consume(AS_LLABEL);
  2038. end;
  2039. AS_LABEL:
  2040. begin
  2041. { when looking for Pascal labels, these must }
  2042. { be in uppercase. }
  2043. if SearchLabel(upper(actasmpattern),hl,true) then
  2044. ConcatLabel(curlist,hl)
  2045. else
  2046. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  2047. Consume(AS_LABEL);
  2048. end;
  2049. AS_DW:
  2050. begin
  2051. Consume(AS_DW);
  2052. BuildConstant($ffff);
  2053. end;
  2054. AS_DB:
  2055. begin
  2056. Consume(AS_DB);
  2057. BuildConstant($ff);
  2058. end;
  2059. AS_DD:
  2060. begin
  2061. Consume(AS_DD);
  2062. BuildConstant($ffffffff);
  2063. end;
  2064. AS_XDEF:
  2065. begin
  2066. Consume(AS_XDEF);
  2067. if actasmtoken=AS_ID then
  2068. ConcatPublic(curlist,actasmpattern);
  2069. Consume(AS_ID);
  2070. if actasmtoken<>AS_SEPARATOR then
  2071. Consume(AS_SEPARATOR);
  2072. end;
  2073. AS_ALIGN:
  2074. begin
  2075. Message(asmr_w_align_not_supported);
  2076. while actasmtoken <> AS_SEPARATOR do
  2077. Consume(actasmtoken);
  2078. end;
  2079. AS_OPCODE:
  2080. begin
  2081. instr:=TM68kInstruction.Create;
  2082. instr.BuildOpcode;
  2083. { instr.AddReferenceSizes;}
  2084. { instr.SetInstructionOpsize;}
  2085. { instr.CheckOperandSizes;}
  2086. if instr.labeled then
  2087. instr.ConcatLabeledInstr(curlist)
  2088. else
  2089. instr.ConcatInstruction(curlist);
  2090. instr.Free;
  2091. {
  2092. instr.init;
  2093. BuildOpcode;
  2094. instr.ops := operandnum;
  2095. if instr.labeled then
  2096. ConcatLabeledInstr(instr)
  2097. else
  2098. ConcatOpCode(instr);
  2099. instr.done;}
  2100. end;
  2101. AS_SEPARATOR:
  2102. begin
  2103. Consume(AS_SEPARATOR);
  2104. { let us go back to the first operand }
  2105. operandnum := 0;
  2106. end;
  2107. AS_END:
  2108. { end assembly block }
  2109. ;
  2110. else
  2111. begin
  2112. Message(asmr_e_syntax_error);
  2113. { error recovery }
  2114. Consume(actasmtoken);
  2115. end;
  2116. end; { end case }
  2117. end; { end while }
  2118. { Check LocalLabelList }
  2119. LocalLabelList.CheckEmitted;
  2120. LocalLabelList.Free;
  2121. assemble:=curlist;
  2122. Message(asmr_d_finish_reading);
  2123. end;
  2124. {*****************************************************************************
  2125. Initialize
  2126. *****************************************************************************}
  2127. const
  2128. asmmode_m68k_mot_info : tasmmodeinfo =
  2129. (
  2130. id : asmmode_m68k_mot;
  2131. idtxt : 'MOTOROLA';
  2132. casmreader : tm68kmotreader;
  2133. );
  2134. begin
  2135. RegisterAsmMode(asmmode_i386_intel_info);
  2136. end.
  2137. {
  2138. $Log$
  2139. Revision 1.5 2004-06-20 08:55:31 florian
  2140. * logs truncated
  2141. Revision 1.4 2004/05/20 21:54:33 florian
  2142. + <pointer> - <pointer> result is divided by the pointer element size now
  2143. this is delphi compatible as well as resulting in the expected result for p1+(p2-p1)
  2144. Revision 1.3 2004/05/06 20:30:51 florian
  2145. * m68k compiler compilation fixed
  2146. Revision 1.14 2004/03/02 00:36:33 olle
  2147. * big transformation of Tai_[const_]Symbol.Create[data]name*
  2148. }