2
0

raz80asm.pas 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533
  1. {
  2. Copyright (c) 1998-2008 by Carl Eric Codere and Peter Vreman
  3. Does the parsing for the Z80 styled 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 raz80asm;
  18. {$i fpcdefs.inc}
  19. Interface
  20. uses
  21. cclasses,
  22. globtype,
  23. rasm,raz80,
  24. aasmbase,cpubase;
  25. type
  26. tasmtoken = (
  27. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_INTNUM,
  28. AS_REALNUM,AS_COMMA,AS_LPAREN,
  29. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,
  30. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_CONDITION,AS_SLASH,AS_DOLLAR,
  31. AS_HASH,AS_LSBRACKET,AS_RSBRACKET,AS_LBRACKET,AS_RBRACKET,
  32. AS_EQUAL,
  33. {------------------ Assembler directives --------------------}
  34. AS_DEFB,AS_DEFW,AS_AREA,AS_END,
  35. {------------------ Assembler Operators --------------------}
  36. AS_TYPE,AS_OFFSET,AS_SIZEOF,AS_VMTOFFSET,AS_MOD,AS_SHL,AS_SHR,AS_NOT,AS_AND,AS_OR,AS_XOR,AS_NOR,AS_AT,
  37. AS_RELTYPE, // common token for relocation types
  38. {------------------ Target-specific directive ---------------}
  39. AS_TARGET_DIRECTIVE
  40. );
  41. tasmkeyword = string[10];
  42. const
  43. { These tokens should be modified accordingly to the modifications }
  44. { in the different enumerations. }
  45. firstdirective = AS_DEFB;
  46. lastdirective = AS_END;
  47. token2str : array[tasmtoken] of tasmkeyword=(
  48. '','Label','LLabel','string','integer',
  49. 'float',',','(',
  50. ')',':','.','+','-','*',
  51. ';','identifier','register','opcode','condition','/','$',
  52. '#','{','}','[',']',
  53. '=',
  54. 'defb','defw','area','END',
  55. 'TYPE','OFFSET','SIZEOF','VMTOFFSET','%','<<','>>','!','&','|','^','~','@','reltype',
  56. 'directive');
  57. type
  58. { input flags for BuildConstSymbolExpression }
  59. tconstsymbolexpressioninputflag = (
  60. cseif_needofs,
  61. cseif_isref,
  62. cseif_startingminus,
  63. { allows using full reference-like syntax for constsymbol expressions,
  64. for example:
  65. Rec.Str[5] -> Rec.Str+5 }
  66. cseif_referencelike
  67. );
  68. tconstsymbolexpressioninputflags = set of tconstsymbolexpressioninputflag;
  69. { output flags for BuildConstSymbolExpression }
  70. tconstsymbolexpressionoutputflag = (
  71. cseof_isseg,
  72. cseof_is_farproc_entry,
  73. cseof_hasofs
  74. );
  75. tconstsymbolexpressionoutputflags = set of tconstsymbolexpressionoutputflag;
  76. { tz80reader }
  77. tz80reader = class(tasmreader)
  78. actasmcond : TAsmCond;
  79. actasmpattern_origcase : string;
  80. actasmtoken : tasmtoken;
  81. prevasmtoken : tasmtoken;
  82. inexpression : boolean;
  83. procedure SetupTables;
  84. procedure GetToken;
  85. function consume(t : tasmtoken):boolean;
  86. procedure RecoverConsume(allowcomma:boolean);
  87. procedure AddReferences(dest,src : tz80operand);
  88. function is_locallabel(const s:string):boolean;
  89. function is_asmopcode(const s: string):boolean;
  90. Function is_asmdirective(const s: string):boolean;
  91. function is_register(const s:string):boolean;
  92. function is_condition(const s:string):boolean;
  93. function is_targetdirective(const s: string):boolean;
  94. procedure BuildRecordOffsetSize(const expr: string;out offset:tcgint;out size:tcgint; out mangledname: string; needvmtofs: boolean; out hastypecast: boolean);
  95. procedure BuildConstSymbolExpression(in_flags: tconstsymbolexpressioninputflags;out value:tcgint;out asmsym:string;out asmsymtyp:TAsmsymtype;out size:tcgint;out out_flags:tconstsymbolexpressionoutputflags);
  96. function BuildConstExpression:longint;
  97. function BuildRefConstExpression(out size:tcgint;startingminus:boolean=false):longint;
  98. procedure BuildConstantOperand(oper: tz80operand);
  99. procedure BuildReference(oper : tz80operand);
  100. procedure BuildOperand(oper: tz80operand;istypecast:boolean);
  101. procedure BuildOpCode(instr:TZ80Instruction);
  102. procedure BuildConstant(constsize: byte);
  103. procedure handleopcode;
  104. procedure ConvertCalljmp(instr : tz80instruction);
  105. function Assemble: tlinkedlist;override;
  106. end;
  107. Implementation
  108. uses
  109. { helpers }
  110. cutils,
  111. { global }
  112. globals,verbose,
  113. systems,
  114. { aasm }
  115. cpuinfo,aasmtai,aasmdata,aasmcpu,
  116. { symtable }
  117. symconst,symbase,symtype,symsym,symtable,symdef,symutil,
  118. { parser }
  119. scanner,pbase,
  120. procinfo,
  121. rabase,rautils,
  122. cgbase,cgutils,cgobj
  123. ;
  124. {*****************************************************************************
  125. tz80reader
  126. *****************************************************************************}
  127. procedure tz80reader.SetupTables;
  128. var
  129. i: TAsmOp;
  130. begin
  131. iasmops:=TFPHashList.create;
  132. for i:=firstop to lastop do
  133. iasmops.Add(upper(std_op2str[i]),Pointer(PtrInt(i)));
  134. end;
  135. procedure tz80reader.GetToken;
  136. var
  137. len: Integer;
  138. srsym : tsym;
  139. srsymtable : TSymtable;
  140. can_be_condition : Boolean;
  141. begin
  142. c:=scanner.c;
  143. { certain instructions can have a condition, as an operand. We need to set this flag,
  144. because 'C' can be either a register, or a condition, depending on the context }
  145. can_be_condition:=(actasmtoken=AS_OPCODE) and (actopcode in [A_JP,A_JR,A_JRJP,A_CALL,A_RET]);
  146. { save old token and reset new token }
  147. prevasmtoken:=actasmtoken;
  148. actasmtoken:=AS_NONE;
  149. { reset }
  150. actasmpattern:='';
  151. { while space and tab , continue scan... }
  152. while c in [' ',#9] do
  153. c:=current_scanner.asmgetchar;
  154. { get token pos }
  155. if not (c in [#10,#13,'{',';','/','(']) then
  156. current_scanner.gettokenpos;
  157. { Local Label, Label, Directive, Prefix or Opcode }
  158. if firsttoken and not(c in [#10,#13,'{',';','/','(']) then
  159. begin
  160. firsttoken:=FALSE;
  161. len:=0;
  162. { directive }
  163. if c = '.' then
  164. begin
  165. inc(len);
  166. actasmpattern[len]:=c;
  167. { Let us point to the next character }
  168. c:=current_scanner.asmgetchar;
  169. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  170. begin
  171. inc(len);
  172. actasmpattern[len]:=c;
  173. c:=current_scanner.asmgetchar;
  174. end;
  175. actasmpattern[0]:=chr(len);
  176. { must be a directive }
  177. if is_asmdirective(actasmpattern) then
  178. exit;
  179. if is_targetdirective(actasmpattern) then
  180. begin
  181. actasmtoken:=AS_TARGET_DIRECTIVE;
  182. exit;
  183. end;
  184. Message1(asmr_e_not_directive_or_local_symbol,actasmpattern);
  185. end;
  186. { only opcodes, global and local labels are allowed now. }
  187. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  188. begin
  189. inc(len);
  190. actasmpattern[len]:=c;
  191. c:=current_scanner.asmgetchar;
  192. end;
  193. actasmpattern[0]:=chr(len);
  194. actasmpattern_origcase:=actasmpattern;
  195. { Label ? }
  196. if c = ':' then
  197. begin
  198. { Local label ? }
  199. if is_locallabel(actasmpattern) then
  200. actasmtoken:=AS_LLABEL
  201. else
  202. actasmtoken:=AS_LABEL;
  203. { let us point to the next character }
  204. c:=current_scanner.asmgetchar;
  205. firsttoken:=true;
  206. exit;
  207. end;
  208. { Directive ? }
  209. if is_asmdirective(actasmpattern) then
  210. exit;
  211. { Opcode ? }
  212. if is_asmopcode(upper(actasmpattern)) then
  213. begin
  214. uppervar(actasmpattern);
  215. exit;
  216. end;
  217. { End of assemblerblock ? }
  218. if upper(actasmpattern) = 'END' then
  219. begin
  220. actasmtoken:=AS_END;
  221. exit;
  222. end;
  223. message1(asmr_e_unknown_opcode,actasmpattern);
  224. actasmtoken:=AS_NONE;
  225. end
  226. else { else firsttoken }
  227. { Here we must handle all possible cases }
  228. begin
  229. case c of
  230. '.' : { possiblities : - local label reference , such as in jmp @local1 }
  231. { - field of object/record }
  232. { - directive. }
  233. begin
  234. if (prevasmtoken in [AS_ID,AS_RPAREN]) then
  235. begin
  236. c:=current_scanner.asmgetchar;
  237. actasmtoken:=AS_DOT;
  238. exit;
  239. end;
  240. actasmpattern:=c;
  241. c:=current_scanner.asmgetchar;
  242. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  243. begin
  244. actasmpattern:=actasmpattern + c;
  245. c:=current_scanner.asmgetchar;
  246. end;
  247. if is_asmdirective(actasmpattern) then
  248. exit;
  249. if is_targetdirective(actasmpattern) then
  250. begin
  251. actasmtoken:=AS_TARGET_DIRECTIVE;
  252. exit;
  253. end;
  254. { local label references and directives }
  255. { are case sensitive }
  256. actasmtoken:=AS_ID;
  257. exit;
  258. end;
  259. { identifier, register, prefix or directive }
  260. '_','A'..'Z','a'..'z':
  261. begin
  262. len:=0;
  263. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  264. begin
  265. inc(len);
  266. actasmpattern[len]:=c;
  267. c:=current_scanner.asmgetchar;
  268. end;
  269. actasmpattern[0]:=chr(len);
  270. actasmpattern_origcase:=actasmpattern;
  271. uppervar(actasmpattern);
  272. { check for end which is a reserved word unlike the opcodes }
  273. if actasmpattern = 'END' then
  274. begin
  275. actasmtoken:=AS_END;
  276. exit;
  277. end;
  278. if actasmpattern = 'TYPE' then
  279. begin
  280. actasmtoken:=AS_TYPE;
  281. exit;
  282. end;
  283. if actasmpattern = 'OFFSET' then
  284. begin
  285. actasmtoken:=AS_OFFSET;
  286. exit;
  287. end;
  288. if actasmpattern = 'SIZEOF' then
  289. begin
  290. actasmtoken:=AS_SIZEOF;
  291. exit;
  292. end;
  293. if actasmpattern = 'VMTOFFSET' then
  294. begin
  295. actasmtoken:=AS_VMTOFFSET;
  296. exit;
  297. end;
  298. if can_be_condition and is_condition(actasmpattern) then
  299. begin
  300. actasmtoken:=AS_CONDITION;
  301. exit;
  302. end;
  303. if is_register(actasmpattern) then
  304. begin
  305. actasmtoken:=AS_REGISTER;
  306. { is it an alternate register? }
  307. if (c='''') and is_register(actasmpattern+'''') then
  308. begin
  309. actasmpattern:=actasmpattern+c;
  310. c:=current_scanner.asmgetchar;
  311. end;
  312. exit;
  313. end;
  314. { if next is a '.' and this is a unitsym then we also need to
  315. parse the identifier }
  316. if (c='.') then
  317. begin
  318. searchsym(actasmpattern,srsym,srsymtable);
  319. if assigned(srsym) and
  320. (srsym.typ=unitsym) and
  321. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  322. srsym.owner.iscurrentunit then
  323. begin
  324. actasmpattern:=actasmpattern+c;
  325. c:=current_scanner.asmgetchar;
  326. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  327. begin
  328. actasmpattern:=actasmpattern + upcase(c);
  329. c:=current_scanner.asmgetchar;
  330. end;
  331. end;
  332. end;
  333. actasmtoken:=AS_ID;
  334. exit;
  335. end;
  336. //'%' : { register or modulo }
  337. // handlepercent;
  338. '1'..'9': { integer number }
  339. begin
  340. len:=0;
  341. while c in ['0'..'9'] do
  342. Begin
  343. inc(len);
  344. actasmpattern[len]:=c;
  345. c:=current_scanner.asmgetchar;
  346. end;
  347. actasmpattern[0]:=chr(len);
  348. actasmpattern:=tostr(ParseVal(actasmpattern,10));
  349. actasmtoken:=AS_INTNUM;
  350. exit;
  351. end;
  352. '0' : { octal,hexa,real or binary number. }
  353. begin
  354. actasmpattern:=c;
  355. c:=current_scanner.asmgetchar;
  356. case upcase(c) of
  357. 'B': { binary }
  358. Begin
  359. c:=current_scanner.asmgetchar;
  360. while c in ['0','1'] do
  361. Begin
  362. actasmpattern:=actasmpattern + c;
  363. c:=current_scanner.asmgetchar;
  364. end;
  365. actasmpattern:=tostr(ParseVal(actasmpattern,2));
  366. actasmtoken:=AS_INTNUM;
  367. exit;
  368. end;
  369. 'D': { real }
  370. Begin
  371. c:=current_scanner.asmgetchar;
  372. { get ridd of the 0d }
  373. if (c in ['+','-']) then
  374. begin
  375. actasmpattern:=c;
  376. c:=current_scanner.asmgetchar;
  377. end
  378. else
  379. actasmpattern:='';
  380. while c in ['0'..'9'] do
  381. Begin
  382. actasmpattern:=actasmpattern + c;
  383. c:=current_scanner.asmgetchar;
  384. end;
  385. if c='.' then
  386. begin
  387. actasmpattern:=actasmpattern + c;
  388. c:=current_scanner.asmgetchar;
  389. while c in ['0'..'9'] do
  390. Begin
  391. actasmpattern:=actasmpattern + c;
  392. c:=current_scanner.asmgetchar;
  393. end;
  394. if upcase(c) = 'E' then
  395. begin
  396. actasmpattern:=actasmpattern + c;
  397. c:=current_scanner.asmgetchar;
  398. if (c in ['+','-']) then
  399. begin
  400. actasmpattern:=actasmpattern + c;
  401. c:=current_scanner.asmgetchar;
  402. end;
  403. while c in ['0'..'9'] do
  404. Begin
  405. actasmpattern:=actasmpattern + c;
  406. c:=current_scanner.asmgetchar;
  407. end;
  408. end;
  409. actasmtoken:=AS_REALNUM;
  410. exit;
  411. end
  412. else
  413. begin
  414. Message1(asmr_e_invalid_float_const,actasmpattern+c);
  415. actasmtoken:=AS_NONE;
  416. end;
  417. end;
  418. 'X': { hexadecimal }
  419. Begin
  420. c:=current_scanner.asmgetchar;
  421. while c in ['0'..'9','a'..'f','A'..'F'] do
  422. Begin
  423. actasmpattern:=actasmpattern + c;
  424. c:=current_scanner.asmgetchar;
  425. end;
  426. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  427. actasmtoken:=AS_INTNUM;
  428. exit;
  429. end;
  430. '1'..'7': { octal }
  431. begin
  432. actasmpattern:=actasmpattern + c;
  433. while c in ['0'..'7'] do
  434. Begin
  435. actasmpattern:=actasmpattern + c;
  436. c:=current_scanner.asmgetchar;
  437. end;
  438. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  439. actasmtoken:=AS_INTNUM;
  440. exit;
  441. end;
  442. else { octal number zero value...}
  443. Begin
  444. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  445. actasmtoken:=AS_INTNUM;
  446. exit;
  447. end;
  448. end; { end case }
  449. end;
  450. '&' :
  451. begin
  452. c:=current_scanner.asmgetchar;
  453. actasmtoken:=AS_AND;
  454. end;
  455. '''' : { char }
  456. begin
  457. actasmpattern:='';
  458. repeat
  459. c:=current_scanner.asmgetchar;
  460. case c of
  461. '\' :
  462. begin
  463. { copy also the next char so \" is parsed correctly }
  464. actasmpattern:=actasmpattern+c;
  465. c:=current_scanner.asmgetchar;
  466. actasmpattern:=actasmpattern+c;
  467. end;
  468. '''' :
  469. begin
  470. c:=current_scanner.asmgetchar;
  471. break;
  472. end;
  473. #10,#13:
  474. Message(scan_f_string_exceeds_line);
  475. else
  476. actasmpattern:=actasmpattern+c;
  477. end;
  478. until false;
  479. actasmpattern:=EscapeToPascal(actasmpattern);
  480. actasmtoken:=AS_STRING;
  481. exit;
  482. end;
  483. '"' : { string }
  484. begin
  485. actasmpattern:='';
  486. repeat
  487. c:=current_scanner.asmgetchar;
  488. case c of
  489. '\' :
  490. begin
  491. { copy also the next char so \" is parsed correctly }
  492. actasmpattern:=actasmpattern+c;
  493. c:=current_scanner.asmgetchar;
  494. actasmpattern:=actasmpattern+c;
  495. end;
  496. '"' :
  497. begin
  498. c:=current_scanner.asmgetchar;
  499. break;
  500. end;
  501. #10,#13:
  502. Message(scan_f_string_exceeds_line);
  503. else
  504. actasmpattern:=actasmpattern+c;
  505. end;
  506. until false;
  507. actasmpattern:=EscapeToPascal(actasmpattern);
  508. actasmtoken:=AS_STRING;
  509. exit;
  510. end;
  511. //'$' :
  512. // begin
  513. // handledollar;
  514. // exit;
  515. // end;
  516. '#' :
  517. begin
  518. actasmtoken:=AS_HASH;
  519. c:=current_scanner.asmgetchar;
  520. exit;
  521. end;
  522. '[' :
  523. begin
  524. actasmtoken:=AS_LBRACKET;
  525. c:=current_scanner.asmgetchar;
  526. exit;
  527. end;
  528. ']' :
  529. begin
  530. actasmtoken:=AS_RBRACKET;
  531. c:=current_scanner.asmgetchar;
  532. exit;
  533. end;
  534. '{' :
  535. begin
  536. {$ifdef arm}
  537. // the arm assembler uses { ... } for register sets
  538. // but compiler directives {$... } are still allowed
  539. c:=current_scanner.asmgetchar;
  540. if c<>'$' then
  541. actasmtoken:=AS_LSBRACKET
  542. else
  543. begin
  544. current_scanner.skipcomment(false);
  545. GetToken;
  546. end;
  547. {$else arm}
  548. current_scanner.skipcomment(true);
  549. GetToken;
  550. {$endif arm}
  551. exit;
  552. end;
  553. {$ifdef arm}
  554. '}' :
  555. begin
  556. actasmtoken:=AS_RSBRACKET;
  557. c:=current_scanner.asmgetchar;
  558. exit;
  559. end;
  560. '=' :
  561. begin
  562. actasmtoken:=AS_EQUAL;
  563. c:=current_scanner.asmgetchar;
  564. exit;
  565. end;
  566. {$endif arm}
  567. ',' :
  568. begin
  569. actasmtoken:=AS_COMMA;
  570. c:=current_scanner.asmgetchar;
  571. exit;
  572. end;
  573. '<' :
  574. begin
  575. actasmtoken:=AS_SHL;
  576. c:=current_scanner.asmgetchar;
  577. if c = '<' then
  578. c:=current_scanner.asmgetchar;
  579. exit;
  580. end;
  581. '>' :
  582. begin
  583. actasmtoken:=AS_SHL;
  584. c:=current_scanner.asmgetchar;
  585. if c = '>' then
  586. c:=current_scanner.asmgetchar;
  587. exit;
  588. end;
  589. '|' :
  590. begin
  591. actasmtoken:=AS_OR;
  592. c:=current_scanner.asmgetchar;
  593. exit;
  594. end;
  595. '^' :
  596. begin
  597. actasmtoken:=AS_XOR;
  598. c:=current_scanner.asmgetchar;
  599. exit;
  600. end;
  601. '(' :
  602. begin
  603. c:=current_scanner.asmgetchar;
  604. if c='*' then
  605. begin
  606. current_scanner.skipoldtpcomment(true);
  607. GetToken;
  608. end
  609. else
  610. actasmtoken:=AS_LPAREN;
  611. exit;
  612. end;
  613. ')' :
  614. begin
  615. actasmtoken:=AS_RPAREN;
  616. c:=current_scanner.asmgetchar;
  617. exit;
  618. end;
  619. ':' :
  620. begin
  621. actasmtoken:=AS_COLON;
  622. c:=current_scanner.asmgetchar;
  623. exit;
  624. end;
  625. '+' :
  626. begin
  627. actasmtoken:=AS_PLUS;
  628. c:=current_scanner.asmgetchar;
  629. exit;
  630. end;
  631. '-' :
  632. begin
  633. actasmtoken:=AS_MINUS;
  634. c:=current_scanner.asmgetchar;
  635. exit;
  636. end;
  637. '*' :
  638. begin
  639. actasmtoken:=AS_STAR;
  640. c:=current_scanner.asmgetchar;
  641. exit;
  642. end;
  643. '/' :
  644. begin
  645. c:=current_scanner.asmgetchar;
  646. if c='/' then
  647. begin
  648. current_scanner.skipdelphicomment;
  649. GetToken;
  650. end
  651. else
  652. actasmtoken:=AS_SLASH;
  653. exit;
  654. end;
  655. '!', '~' :
  656. begin
  657. actasmtoken:=AS_NOT;
  658. c:=current_scanner.asmgetchar;
  659. exit;
  660. end;
  661. '@' : { possiblities : - local label reference , such as in jmp @local1 }
  662. { - @Result, @Code or @Data special variables. }
  663. begin
  664. actasmpattern:=c;
  665. c:=current_scanner.asmgetchar;
  666. while c in ['A'..'Z','a'..'z','0'..'9','_','@','$','&','?'] do
  667. begin
  668. actasmpattern:=actasmpattern + c;
  669. c:=current_scanner.asmgetchar;
  670. end;
  671. actasmpattern_origcase:=actasmpattern;
  672. uppervar(actasmpattern);
  673. actasmtoken:=AS_ID;
  674. exit;
  675. end;
  676. #13,#10:
  677. begin
  678. current_scanner.linebreak;
  679. c:=current_scanner.asmgetchar;
  680. firsttoken:=TRUE;
  681. actasmtoken:=AS_SEPARATOR;
  682. exit;
  683. end;
  684. ';' :
  685. begin
  686. c:=current_scanner.asmgetchar;
  687. firsttoken:=TRUE;
  688. actasmtoken:=AS_SEPARATOR;
  689. exit;
  690. end;
  691. else
  692. current_scanner.illegal_char(c);
  693. end;
  694. end;
  695. end;
  696. function tz80reader.consume(t: tasmtoken): boolean;
  697. begin
  698. Consume:=true;
  699. if t<>actasmtoken then
  700. begin
  701. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  702. Consume:=false;
  703. end;
  704. repeat
  705. gettoken;
  706. until actasmtoken<>AS_NONE;
  707. end;
  708. procedure tz80reader.RecoverConsume(allowcomma: boolean);
  709. begin
  710. while not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  711. begin
  712. if allowcomma and (actasmtoken=AS_COMMA) then
  713. break;
  714. Consume(actasmtoken);
  715. end;
  716. end;
  717. procedure tz80reader.AddReferences(dest, src: tz80operand);
  718. procedure AddRegister(reg:tregister;scalefactor:byte);
  719. begin
  720. if reg=NR_NO then
  721. exit;
  722. if (dest.opr.ref.base=NR_NO) and (scalefactor=1) then
  723. begin
  724. dest.opr.ref.base:=reg;
  725. exit;
  726. end;
  727. if dest.opr.ref.index=NR_NO then
  728. begin
  729. dest.opr.ref.index:=reg;
  730. dest.opr.ref.scalefactor:=scalefactor;
  731. exit;
  732. end;
  733. if dest.opr.ref.index=reg then
  734. begin
  735. Inc(dest.opr.ref.scalefactor,scalefactor);
  736. exit;
  737. end;
  738. Message(asmr_e_multiple_index);
  739. end;
  740. var
  741. tmplocal: TOprRec;
  742. segreg: TRegister;
  743. begin
  744. case dest.opr.typ of
  745. OPR_REFERENCE:
  746. begin
  747. case src.opr.typ of
  748. OPR_REFERENCE:
  749. begin
  750. AddRegister(src.opr.ref.base,1);
  751. AddRegister(src.opr.ref.index,src.opr.ref.scalefactor);
  752. Inc(dest.opr.ref.offset,src.opr.ref.offset);
  753. Inc(dest.opr.constoffset,src.opr.constoffset);
  754. dest.haslabelref:=dest.haslabelref or src.haslabelref;
  755. dest.hasproc:=dest.hasproc or src.hasproc;
  756. dest.hasvar:=dest.hasvar or src.hasvar;
  757. if assigned(src.opr.ref.symbol) then
  758. begin
  759. if assigned(dest.opr.ref.symbol) then
  760. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  761. dest.opr.ref.symbol:=src.opr.ref.symbol;
  762. end;
  763. if assigned(src.opr.ref.relsymbol) then
  764. begin
  765. if assigned(dest.opr.ref.relsymbol) then
  766. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  767. dest.opr.ref.relsymbol:=src.opr.ref.relsymbol;
  768. end;
  769. if dest.opr.ref.refaddr=addr_no then
  770. dest.opr.ref.refaddr:=src.opr.ref.refaddr;
  771. end;
  772. OPR_LOCAL:
  773. begin
  774. tmplocal:=src.opr;
  775. if dest.opr.ref.base<>NR_NO then
  776. begin
  777. if tmplocal.localindexreg=NR_NO then
  778. begin
  779. tmplocal.localindexreg:=dest.opr.ref.base;
  780. tmplocal.localscale:=0;
  781. end
  782. else if tmplocal.localindexreg=dest.opr.ref.base then
  783. tmplocal.localscale:=Min(tmplocal.localscale,1)+1
  784. else
  785. Message(asmr_e_multiple_index);
  786. end;
  787. if dest.opr.ref.index<>NR_NO then
  788. begin
  789. if tmplocal.localindexreg=NR_NO then
  790. begin
  791. tmplocal.localindexreg:=dest.opr.ref.index;
  792. tmplocal.localscale:=dest.opr.ref.scalefactor;
  793. end
  794. else if tmplocal.localindexreg=dest.opr.ref.index then
  795. tmplocal.localscale:=Min(tmplocal.localscale,1)+Min(dest.opr.ref.scalefactor,1)
  796. else
  797. Message(asmr_e_multiple_index);
  798. end;
  799. Inc(tmplocal.localconstoffset,dest.opr.constoffset);
  800. Inc(tmplocal.localsymofs,dest.opr.ref.offset);
  801. dest.opr:=tmplocal;
  802. end;
  803. else
  804. internalerror(2018030711);
  805. end;
  806. end;
  807. OPR_LOCAL:
  808. begin
  809. case src.opr.typ of
  810. OPR_REFERENCE:
  811. begin
  812. if src.opr.ref.base<>NR_NO then
  813. begin
  814. if dest.opr.localindexreg=NR_NO then
  815. begin
  816. dest.opr.localindexreg:=src.opr.ref.base;
  817. dest.opr.localscale:=0;
  818. end
  819. else if dest.opr.localindexreg=src.opr.ref.base then
  820. dest.opr.localscale:=Min(dest.opr.localscale,1)+1
  821. else
  822. Message(asmr_e_multiple_index);
  823. end;
  824. if src.opr.ref.index<>NR_NO then
  825. begin
  826. if dest.opr.localindexreg=NR_NO then
  827. begin
  828. dest.opr.localindexreg:=src.opr.ref.index;
  829. dest.opr.localscale:=src.opr.ref.scalefactor;
  830. end
  831. else if dest.opr.localindexreg=src.opr.ref.index then
  832. dest.opr.localscale:=Min(dest.opr.localscale,1)+Min(src.opr.ref.scalefactor,1)
  833. else
  834. Message(asmr_e_multiple_index);
  835. end;
  836. Inc(dest.opr.localconstoffset,src.opr.constoffset);
  837. Inc(dest.opr.localsymofs,src.opr.ref.offset);
  838. end;
  839. OPR_LOCAL:
  840. Message(asmr_e_no_local_or_para_allowed);
  841. else
  842. internalerror(2018030713);
  843. end;
  844. end;
  845. else
  846. internalerror(2018030712);
  847. end;
  848. end;
  849. function tz80reader.is_locallabel(const s: string): boolean;
  850. begin
  851. is_locallabel:=(length(s)>1) and (s[1]='@');
  852. end;
  853. function tz80reader.is_asmopcode(const s: string):boolean;
  854. begin
  855. actcondition:=C_None;
  856. actopcode:=tasmop(PtrUInt(iasmops.Find(s)));
  857. if actopcode<>A_NONE then
  858. begin
  859. actasmtoken:=AS_OPCODE;
  860. is_asmopcode:=true;
  861. end
  862. else
  863. is_asmopcode:=false;
  864. end;
  865. function tz80reader.is_asmdirective(const s: string): boolean;
  866. var
  867. i : tasmtoken;
  868. hs : string;
  869. begin
  870. hs:=lower(s);
  871. for i:=firstdirective to lastdirective do
  872. if hs=token2str[i] then
  873. begin
  874. actasmtoken:=i;
  875. is_asmdirective:=true;
  876. exit;
  877. end;
  878. is_asmdirective:=false;
  879. end;
  880. function tz80reader.is_register(const s:string):boolean;
  881. begin
  882. is_register:=false;
  883. actasmregister:=std_regnum_search(lower(s));
  884. if actasmregister<>NR_NO then
  885. begin
  886. is_register:=true;
  887. actasmtoken:=AS_REGISTER;
  888. end;
  889. end;
  890. function tz80reader.is_condition(const s: string): boolean;
  891. var
  892. condstr: string;
  893. cond: TAsmCond;
  894. begin
  895. is_condition:=false;
  896. actasmcond:=C_None;
  897. condstr:=lower(s);
  898. for cond in TAsmCond do
  899. if (cond<>C_None) and (cond2str[cond]=condstr) then
  900. begin
  901. is_condition:=true;
  902. actasmtoken:=AS_CONDITION;
  903. actasmcond:=cond;
  904. exit;
  905. end;
  906. end;
  907. function tz80reader.is_targetdirective(const s: string): boolean;
  908. begin
  909. result:=false;
  910. end;
  911. procedure tz80reader.BuildRecordOffsetSize(const expr: string; out
  912. offset: tcgint; out size: tcgint; out mangledname: string;
  913. needvmtofs: boolean; out hastypecast: boolean);
  914. var
  915. s: string;
  916. Begin
  917. offset:=0;
  918. size:=0;
  919. mangledname:='';
  920. hastypecast:=false;
  921. s:=expr;
  922. while (actasmtoken=AS_DOT) do
  923. begin
  924. Consume(AS_DOT);
  925. if actasmtoken in [AS_ID,AS_REGISTER] then
  926. begin
  927. s:=s+'.'+actasmpattern;
  928. consume(actasmtoken);
  929. end
  930. else
  931. begin
  932. Consume(AS_ID);
  933. RecoverConsume(true);
  934. break;
  935. end;
  936. end;
  937. if not GetRecordOffsetSize(s,offset,size,mangledname,needvmtofs,hastypecast) then
  938. Message(asmr_e_building_record_offset);
  939. end;
  940. procedure tz80reader.BuildConstSymbolExpression(
  941. in_flags: tconstsymbolexpressioninputflags; out value: tcgint; out
  942. asmsym: string; out asmsymtyp: TAsmsymtype; out size: tcgint; out
  943. out_flags: tconstsymbolexpressionoutputflags);
  944. var
  945. tempstr,expr,hs,mangledname : string;
  946. parenlevel : longint;
  947. l,k : tcgint;
  948. hasparen,
  949. errorflag,
  950. needvmtofs : boolean;
  951. prevtok : tasmtoken;
  952. hl : tasmlabel;
  953. hssymtyp : Tasmsymtype;
  954. def : tdef;
  955. sym : tsym;
  956. srsymtable : TSymtable;
  957. hastypecast : boolean;
  958. Begin
  959. { reset }
  960. value:=0;
  961. asmsym:='';
  962. asmsymtyp:=AT_DATA;
  963. size:=0;
  964. out_flags:=[];
  965. errorflag:=FALSE;
  966. tempstr:='';
  967. expr:='';
  968. if cseif_startingminus in in_flags then
  969. expr:='-';
  970. inexpression:=TRUE;
  971. parenlevel:=0;
  972. sym:=nil;
  973. needvmtofs:=FALSE;
  974. Repeat
  975. { Support ugly delphi constructs like: [ECX].1+2[EDX] }
  976. if (cseif_isref in in_flags) and (actasmtoken=AS_LBRACKET) then
  977. break;
  978. if (cseif_referencelike in in_flags) and
  979. (actasmtoken in [AS_LBRACKET,AS_RBRACKET]) then
  980. case actasmtoken of
  981. AS_LBRACKET:
  982. begin
  983. Consume(AS_LBRACKET);
  984. if (length(expr)>0) and
  985. not (expr[length(expr)] in ['+','-']) then
  986. expr:=expr+'+';
  987. expr:=expr+'[';
  988. end;
  989. AS_RBRACKET:
  990. begin
  991. Consume(AS_RBRACKET);
  992. expr:=expr+']';
  993. end;
  994. else
  995. ;
  996. end;
  997. Case actasmtoken of
  998. AS_LPAREN:
  999. Begin
  1000. Consume(AS_LPAREN);
  1001. expr:=expr + '(';
  1002. inc(parenlevel);
  1003. end;
  1004. AS_RPAREN:
  1005. Begin
  1006. { Keep the AS_PAREN in actasmtoken, it is maybe a typecast }
  1007. if parenlevel=0 then
  1008. break;
  1009. Consume(AS_RPAREN);
  1010. expr:=expr + ')';
  1011. dec(parenlevel);
  1012. end;
  1013. AS_SHL:
  1014. Begin
  1015. Consume(AS_SHL);
  1016. expr:=expr + '<';
  1017. end;
  1018. AS_SHR:
  1019. Begin
  1020. Consume(AS_SHR);
  1021. expr:=expr + '>';
  1022. end;
  1023. AS_SLASH:
  1024. Begin
  1025. Consume(AS_SLASH);
  1026. expr:=expr + '/';
  1027. end;
  1028. AS_MOD:
  1029. Begin
  1030. Consume(AS_MOD);
  1031. expr:=expr + '%';
  1032. end;
  1033. AS_STAR:
  1034. Begin
  1035. Consume(AS_STAR);
  1036. if (cseif_isref in in_flags) and (actasmtoken=AS_REGISTER) then
  1037. break;
  1038. expr:=expr + '*';
  1039. end;
  1040. AS_PLUS:
  1041. Begin
  1042. Consume(AS_PLUS);
  1043. if (cseif_isref in in_flags) and ((actasmtoken=AS_REGISTER) or (actasmtoken=AS_LBRACKET)) then
  1044. break;
  1045. expr:=expr + '+';
  1046. end;
  1047. AS_MINUS:
  1048. Begin
  1049. Consume(AS_MINUS);
  1050. expr:=expr + '-';
  1051. end;
  1052. AS_AND:
  1053. Begin
  1054. Consume(AS_AND);
  1055. expr:=expr + '&';
  1056. end;
  1057. AS_NOT:
  1058. Begin
  1059. Consume(AS_NOT);
  1060. expr:=expr + '~';
  1061. end;
  1062. AS_XOR:
  1063. Begin
  1064. Consume(AS_XOR);
  1065. expr:=expr + '^';
  1066. end;
  1067. AS_OR:
  1068. Begin
  1069. Consume(AS_OR);
  1070. expr:=expr + '|';
  1071. end;
  1072. AS_INTNUM:
  1073. Begin
  1074. expr:=expr + actasmpattern;
  1075. Consume(AS_INTNUM);
  1076. end;
  1077. {$ifdef i8086}
  1078. AS_SEG:
  1079. begin
  1080. include(out_flags,cseof_isseg);
  1081. Consume(actasmtoken);
  1082. if actasmtoken<>AS_ID then
  1083. Message(asmr_e_seg_without_identifier);
  1084. end;
  1085. {$endif i8086}
  1086. AS_VMTOFFSET,
  1087. AS_OFFSET:
  1088. begin
  1089. if (actasmtoken = AS_OFFSET) then
  1090. begin
  1091. include(in_flags,cseif_needofs);
  1092. include(out_flags,cseof_hasofs);
  1093. end
  1094. else
  1095. needvmtofs:=true;
  1096. Consume(actasmtoken);
  1097. if actasmtoken<>AS_ID then
  1098. Message(asmr_e_offset_without_identifier);
  1099. end;
  1100. AS_SIZEOF,
  1101. AS_TYPE:
  1102. begin
  1103. l:=0;
  1104. hasparen:=false;
  1105. Consume(actasmtoken);
  1106. if actasmtoken=AS_LPAREN then
  1107. begin
  1108. hasparen:=true;
  1109. Consume(AS_LPAREN);
  1110. end;
  1111. if actasmtoken<>AS_ID then
  1112. Message(asmr_e_type_without_identifier)
  1113. else
  1114. begin
  1115. tempstr:=actasmpattern;
  1116. Consume(AS_ID);
  1117. if actasmtoken=AS_DOT then
  1118. begin
  1119. BuildRecordOffsetSize(tempstr,k,l,mangledname,false,hastypecast);
  1120. if mangledname<>'' then
  1121. { procsym }
  1122. Message(asmr_e_wrong_sym_type);
  1123. if hastypecast then
  1124. end
  1125. else
  1126. begin
  1127. asmsearchsym(tempstr,sym,srsymtable);
  1128. if assigned(sym) then
  1129. begin
  1130. case sym.typ of
  1131. staticvarsym,
  1132. localvarsym,
  1133. paravarsym :
  1134. l:=tabstractvarsym(sym).getsize;
  1135. typesym :
  1136. l:=ttypesym(sym).typedef.size;
  1137. else
  1138. Message(asmr_e_wrong_sym_type);
  1139. end;
  1140. end
  1141. else
  1142. Message1(sym_e_unknown_id,tempstr);
  1143. end;
  1144. end;
  1145. str(l, tempstr);
  1146. expr:=expr + tempstr;
  1147. if hasparen then
  1148. Consume(AS_RPAREN);
  1149. end;
  1150. //AS_PTR :
  1151. // begin
  1152. // { Support ugly delphi constructs like <constant> PTR [ref] }
  1153. // break;
  1154. // end;
  1155. AS_STRING:
  1156. begin
  1157. l:=0;
  1158. case Length(actasmpattern) of
  1159. 1 :
  1160. l:=ord(actasmpattern[1]);
  1161. 2 :
  1162. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  1163. 3 :
  1164. l:=ord(actasmpattern[3]) +
  1165. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  1166. 4 :
  1167. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  1168. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  1169. else
  1170. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  1171. end;
  1172. str(l, tempstr);
  1173. expr:=expr + tempstr;
  1174. Consume(AS_STRING);
  1175. end;
  1176. AS_ID:
  1177. begin
  1178. hs:='';
  1179. hssymtyp:=AT_DATA;
  1180. def:=nil;
  1181. tempstr:=actasmpattern;
  1182. prevtok:=prevasmtoken;
  1183. { stop parsing a constant expression if we find an opcode after a
  1184. non-operator like "db $66 mov eax,ebx" }
  1185. if (prevtok in [AS_ID,AS_INTNUM,AS_RPAREN]) and
  1186. is_asmopcode(actasmpattern) then
  1187. break;
  1188. consume(AS_ID);
  1189. if (tempstr='@CODE') or (tempstr='@DATA') then
  1190. begin
  1191. if asmsym='' then
  1192. begin
  1193. asmsym:=tempstr;
  1194. asmsymtyp:=AT_SECTION;
  1195. end
  1196. else
  1197. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1198. end
  1199. else if SearchIConstant(tempstr,l) then
  1200. begin
  1201. str(l, tempstr);
  1202. expr:=expr + tempstr;
  1203. end
  1204. else
  1205. begin
  1206. if is_locallabel(tempstr) then
  1207. begin
  1208. CreateLocalLabel(tempstr,hl,false);
  1209. hs:=hl.name;
  1210. hssymtyp:=AT_FUNCTION;
  1211. end
  1212. else
  1213. if SearchLabel(tempstr,hl,false) then
  1214. begin
  1215. hs:=hl.name;
  1216. hssymtyp:=AT_FUNCTION;
  1217. end
  1218. else
  1219. begin
  1220. asmsearchsym(tempstr,sym,srsymtable);
  1221. if assigned(sym) then
  1222. begin
  1223. case sym.typ of
  1224. staticvarsym :
  1225. begin
  1226. hs:=tstaticvarsym(sym).mangledname;
  1227. def:=tstaticvarsym(sym).vardef;
  1228. end;
  1229. localvarsym,
  1230. paravarsym :
  1231. begin
  1232. Message(asmr_e_no_local_or_para_allowed);
  1233. end;
  1234. procsym :
  1235. begin
  1236. if Tprocsym(sym).ProcdefList.Count>1 then
  1237. Message(asmr_w_calling_overload_func);
  1238. hs:=tprocdef(tprocsym(sym).ProcdefList[0]).mangledname;
  1239. {$ifdef i8086}
  1240. if is_proc_far(tprocdef(tprocsym(sym).ProcdefList[0]))
  1241. and not (po_interrupt in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions) then
  1242. include(out_flags,cseof_is_farproc_entry)
  1243. else
  1244. exclude(out_flags,cseof_is_farproc_entry);
  1245. {$endif i8086}
  1246. hssymtyp:=AT_FUNCTION;
  1247. end;
  1248. typesym :
  1249. begin
  1250. if not(ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1251. Message(asmr_e_wrong_sym_type);
  1252. size:=ttypesym(sym).typedef.size;
  1253. end;
  1254. fieldvarsym :
  1255. begin
  1256. tempstr:=upper(tdef(sym.owner.defowner).GetTypeName)+'.'+tempstr;
  1257. end;
  1258. else
  1259. Message(asmr_e_wrong_sym_type);
  1260. end;
  1261. end
  1262. else
  1263. Message1(sym_e_unknown_id,tempstr);
  1264. end;
  1265. { symbol found? }
  1266. if hs<>'' then
  1267. begin
  1268. if asmsym='' then
  1269. begin
  1270. asmsym:=hs;
  1271. asmsymtyp:=hssymtyp;
  1272. end
  1273. else
  1274. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1275. if (expr='') or (expr[length(expr)]='+') then
  1276. begin
  1277. { don't remove the + if there could be a record field }
  1278. if actasmtoken<>AS_DOT then
  1279. delete(expr,length(expr),1);
  1280. end
  1281. else if (cseif_needofs in in_flags) then
  1282. begin
  1283. if (prevtok<>AS_OFFSET) then
  1284. Message(asmr_e_need_offset);
  1285. end
  1286. else
  1287. Message(asmr_e_only_add_relocatable_symbol);
  1288. end;
  1289. if (actasmtoken=AS_DOT) or
  1290. (assigned(sym) and
  1291. is_normal_fieldvarsym(sym)) then
  1292. begin
  1293. BuildRecordOffsetSize(tempstr,l,size,hs,needvmtofs,hastypecast);
  1294. if hs <> '' then
  1295. hssymtyp:=AT_FUNCTION
  1296. else
  1297. begin
  1298. str(l, tempstr);
  1299. expr:=expr + tempstr;
  1300. end
  1301. end
  1302. else if (actasmtoken<>AS_DOT) and
  1303. assigned(sym) and
  1304. (sym.typ=typesym) and
  1305. (ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1306. begin
  1307. { just a record type (without being followed by dot)
  1308. evaluates to 0. Ugly, but TP7 compatible. }
  1309. expr:=expr+'0';
  1310. end
  1311. else
  1312. begin
  1313. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  1314. delete(expr,length(expr),1);
  1315. end;
  1316. if (actasmtoken=AS_LBRACKET) and
  1317. assigned(def) and
  1318. (def.typ=arraydef) then
  1319. begin
  1320. consume(AS_LBRACKET);
  1321. l:=BuildConstExpression;
  1322. if l<tarraydef(def).lowrange then
  1323. begin
  1324. Message(asmr_e_constant_out_of_bounds);
  1325. l:=0;
  1326. end
  1327. else
  1328. l:=(l-tarraydef(def).lowrange)*tarraydef(def).elesize;
  1329. str(l, tempstr);
  1330. expr:=expr + '+' + tempstr;
  1331. consume(AS_RBRACKET);
  1332. end;
  1333. end;
  1334. { check if there are wrong operator used like / or mod etc. }
  1335. if (hs<>'') and not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END,AS_RBRACKET]) then
  1336. Message(asmr_e_only_add_relocatable_symbol);
  1337. end;
  1338. //AS_ALIGN,
  1339. AS_DEFB,
  1340. AS_DEFW,
  1341. AS_END,
  1342. AS_RBRACKET,
  1343. AS_SEPARATOR,
  1344. AS_COMMA,
  1345. AS_COLON:
  1346. break;
  1347. else
  1348. begin
  1349. { write error only once. }
  1350. if not errorflag then
  1351. Message(asmr_e_invalid_constant_expression);
  1352. { consume tokens until we find COMMA or SEPARATOR }
  1353. Consume(actasmtoken);
  1354. errorflag:=TRUE;
  1355. end;
  1356. end;
  1357. Until false;
  1358. { calculate expression }
  1359. if not ErrorFlag then
  1360. value:=CalculateExpression(expr)
  1361. else
  1362. value:=0;
  1363. { no longer in an expression }
  1364. inexpression:=FALSE;
  1365. end;
  1366. function tz80reader.BuildConstExpression: longint;
  1367. var
  1368. l,size : tcgint;
  1369. hs : string;
  1370. hssymtyp : TAsmsymtype;
  1371. out_flags : tconstsymbolexpressionoutputflags;
  1372. begin
  1373. BuildConstSymbolExpression([],l,hs,hssymtyp,size,out_flags);
  1374. if hs<>'' then
  1375. Message(asmr_e_relocatable_symbol_not_allowed);
  1376. BuildConstExpression:=l;
  1377. end;
  1378. function tz80reader.BuildRefConstExpression(out size: tcgint;
  1379. startingminus: boolean): longint;
  1380. var
  1381. l : tcgint;
  1382. hs : string;
  1383. hssymtyp : TAsmsymtype;
  1384. in_flags : tconstsymbolexpressioninputflags;
  1385. out_flags : tconstsymbolexpressionoutputflags;
  1386. begin
  1387. in_flags:=[cseif_isref];
  1388. if startingminus then
  1389. include(in_flags,cseif_startingminus);
  1390. BuildConstSymbolExpression(in_flags,l,hs,hssymtyp,size,out_flags);
  1391. if hs<>'' then
  1392. Message(asmr_e_relocatable_symbol_not_allowed);
  1393. BuildRefConstExpression:=l;
  1394. end;
  1395. procedure tz80reader.BuildConstantOperand(oper: tz80operand);
  1396. var
  1397. l,size : tcgint;
  1398. tempstr : string;
  1399. tempsymtyp : tasmsymtype;
  1400. cse_out_flags : tconstsymbolexpressionoutputflags;
  1401. begin
  1402. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1403. Message(asmr_e_invalid_operand_type);
  1404. BuildConstSymbolExpression([cseif_needofs],l,tempstr,tempsymtyp,size,cse_out_flags);
  1405. if tempstr<>'' then
  1406. begin
  1407. oper.opr.typ:=OPR_SYMBOL;
  1408. oper.opr.symofs:=l;
  1409. oper.opr.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp);
  1410. oper.opr.symseg:=cseof_isseg in cse_out_flags;
  1411. oper.opr.sym_farproc_entry:=cseof_is_farproc_entry in cse_out_flags;
  1412. end
  1413. else
  1414. if oper.opr.typ=OPR_NONE then
  1415. begin
  1416. oper.opr.typ:=OPR_CONSTANT;
  1417. oper.opr.val:=l;
  1418. end
  1419. else
  1420. inc(oper.opr.val,l);
  1421. end;
  1422. procedure tz80reader.BuildReference(oper: tz80operand);
  1423. var
  1424. scale : byte;
  1425. k,l,size : tcgint;
  1426. tempstr,hs : string;
  1427. tempsymtyp : tasmsymtype;
  1428. code : integer;
  1429. hreg : tregister;
  1430. GotStar,GotOffset,HadVar,
  1431. GotPlus,Negative,BracketlessReference : boolean;
  1432. hl : tasmlabel;
  1433. hastypecast: boolean;
  1434. tmpoper: tz80operand;
  1435. cse_in_flags: tconstsymbolexpressioninputflags;
  1436. cse_out_flags: tconstsymbolexpressionoutputflags;
  1437. begin
  1438. if actasmtoken=AS_LPAREN then
  1439. begin
  1440. Consume(AS_LPAREN);
  1441. BracketlessReference:=false;
  1442. end
  1443. else
  1444. BracketlessReference:=true;
  1445. if not(oper.opr.typ in [OPR_LOCAL,OPR_REFERENCE]) then
  1446. oper.InitRef;
  1447. GotStar:=false;
  1448. GotPlus:=true;
  1449. GotOffset:=false;
  1450. Negative:=false;
  1451. Scale:=0;
  1452. repeat
  1453. if GotOffset and (actasmtoken<>AS_ID) then
  1454. Message(asmr_e_invalid_reference_syntax);
  1455. Case actasmtoken of
  1456. AS_ID, { Constant reference expression OR variable reference expression }
  1457. AS_VMTOFFSET:
  1458. Begin
  1459. if not GotPlus then
  1460. Message(asmr_e_invalid_reference_syntax);
  1461. GotStar:=false;
  1462. GotPlus:=false;
  1463. if (actasmtoken = AS_VMTOFFSET) or
  1464. (SearchIConstant(actasmpattern,l) or
  1465. SearchRecordType(actasmpattern)) then
  1466. begin
  1467. l:=BuildRefConstExpression(size,negative);
  1468. if size<>0 then
  1469. oper.SetSize(size,false);
  1470. negative:=false; { "l" was negated if necessary }
  1471. GotPlus:=(prevasmtoken=AS_PLUS);
  1472. GotStar:=(prevasmtoken=AS_STAR);
  1473. case oper.opr.typ of
  1474. OPR_LOCAL :
  1475. begin
  1476. if GotStar then
  1477. Message(asmr_e_invalid_reference_syntax);
  1478. Inc(oper.opr.localsymofs,l);
  1479. end;
  1480. OPR_REFERENCE :
  1481. begin
  1482. if GotStar then
  1483. oper.opr.ref.scalefactor:=l
  1484. else
  1485. Inc(oper.opr.ref.offset,l);
  1486. end;
  1487. else
  1488. internalerror(2019050715);
  1489. end;
  1490. end
  1491. else
  1492. Begin
  1493. if negative and not oper.hasvar then
  1494. Message(asmr_e_only_add_relocatable_symbol)
  1495. else if oper.hasvar and not GotOffset and
  1496. (not negative or assigned(oper.opr.ref.relsymbol)) then
  1497. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1498. HadVar:=oper.hasvar and GotOffset;
  1499. tempstr:=actasmpattern;
  1500. Consume(AS_ID);
  1501. { typecasting? }
  1502. if (actasmtoken=AS_LPAREN) and
  1503. SearchType(tempstr,l) then
  1504. begin
  1505. oper.hastype:=true;
  1506. oper.typesize:=l;
  1507. Consume(AS_LPAREN);
  1508. BuildOperand(oper,true);
  1509. Consume(AS_RPAREN);
  1510. end
  1511. else
  1512. if is_locallabel(tempstr) then
  1513. begin
  1514. CreateLocalLabel(tempstr,hl,false);
  1515. oper.InitRef;
  1516. oper.haslabelref:=true;
  1517. if not negative then
  1518. begin
  1519. oper.opr.ref.symbol:=hl;
  1520. oper.hasvar:=true;
  1521. end
  1522. else
  1523. oper.opr.ref.relsymbol:=hl;
  1524. {$ifdef i8086}
  1525. if oper.opr.ref.segment=NR_NO then
  1526. oper.opr.ref.segment:=NR_CS;
  1527. {$endif i8086}
  1528. end
  1529. else
  1530. if oper.SetupVar(tempstr,GotOffset) then
  1531. begin
  1532. { convert OPR_LOCAL register para into a reference base }
  1533. if (oper.opr.typ=OPR_LOCAL) and
  1534. AsmRegisterPara(oper.opr.localsym) then
  1535. oper.InitRefConvertLocal
  1536. else
  1537. begin
  1538. {$ifdef x86_64}
  1539. if actasmtoken=AS_WRT then
  1540. begin
  1541. if (oper.opr.typ=OPR_REFERENCE) then
  1542. begin
  1543. Consume(AS_WRT);
  1544. Consume(AS___GOTPCREL);
  1545. if (oper.opr.ref.base<>NR_NO) or
  1546. (oper.opr.ref.index<>NR_NO) or
  1547. (oper.opr.ref.offset<>0) then
  1548. Message(asmr_e_wrong_gotpcrel_intel_syntax);
  1549. if tf_no_pic_supported in target_info.flags then
  1550. Message(asmr_e_no_gotpcrel_support);
  1551. oper.opr.ref.refaddr:=addr_pic;
  1552. oper.opr.ref.base:=NR_RIP;
  1553. end
  1554. else
  1555. message(asmr_e_invalid_reference_syntax);
  1556. end;
  1557. {$endif x86_64}
  1558. end;
  1559. end
  1560. else
  1561. Message1(sym_e_unknown_id,tempstr);
  1562. { record.field ? }
  1563. if actasmtoken=AS_DOT then
  1564. begin
  1565. BuildRecordOffsetSize(tempstr,l,k,hs,false,hastypecast);
  1566. if (hs<>'') then
  1567. Message(asmr_e_invalid_symbol_ref);
  1568. case oper.opr.typ of
  1569. OPR_LOCAL :
  1570. inc(oper.opr.localsymofs,l);
  1571. OPR_REFERENCE :
  1572. inc(oper.opr.ref.offset,l);
  1573. else
  1574. internalerror(2019050716);
  1575. end;
  1576. if hastypecast then
  1577. oper.hastype:=true;
  1578. oper.SetSize(k,false);
  1579. end;
  1580. if GotOffset then
  1581. begin
  1582. if oper.hasvar and (oper.opr.ref.base=current_procinfo.framepointer) then
  1583. begin
  1584. if (oper.opr.typ=OPR_REFERENCE) then
  1585. oper.opr.ref.base:=NR_NO;
  1586. oper.hasvar:=hadvar;
  1587. end
  1588. else
  1589. begin
  1590. if oper.hasvar and hadvar then
  1591. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1592. { should we allow ?? }
  1593. end;
  1594. end;
  1595. end;
  1596. GotOffset:=false;
  1597. end;
  1598. AS_PLUS :
  1599. Begin
  1600. Consume(AS_PLUS);
  1601. Negative:=false;
  1602. GotPlus:=true;
  1603. GotStar:=false;
  1604. Scale:=0;
  1605. end;
  1606. AS_DOT :
  1607. Begin
  1608. { Handle like a + }
  1609. Consume(AS_DOT);
  1610. Negative:=false;
  1611. GotPlus:=true;
  1612. GotStar:=false;
  1613. Scale:=0;
  1614. end;
  1615. AS_MINUS :
  1616. begin
  1617. Consume(AS_MINUS);
  1618. Negative:=true;
  1619. GotPlus:=true;
  1620. GotStar:=false;
  1621. Scale:=0;
  1622. end;
  1623. AS_STAR : { Scaling, with eax*4 order }
  1624. begin
  1625. Consume(AS_STAR);
  1626. hs:='';
  1627. l:=0;
  1628. case actasmtoken of
  1629. AS_ID,
  1630. AS_LPAREN :
  1631. l:=BuildConstExpression;
  1632. AS_INTNUM:
  1633. Begin
  1634. hs:=actasmpattern;
  1635. Consume(AS_INTNUM);
  1636. end;
  1637. AS_REGISTER :
  1638. begin
  1639. case oper.opr.typ of
  1640. OPR_REFERENCE :
  1641. begin
  1642. if oper.opr.ref.scalefactor=0 then
  1643. begin
  1644. if scale<>0 then
  1645. begin
  1646. oper.opr.ref.scalefactor:=scale;
  1647. scale:=0;
  1648. end
  1649. else
  1650. Message(asmr_e_wrong_scale_factor);
  1651. end
  1652. else
  1653. Message(asmr_e_invalid_reference_syntax);
  1654. end;
  1655. OPR_LOCAL :
  1656. begin
  1657. if oper.opr.localscale=0 then
  1658. begin
  1659. if scale<>0 then
  1660. begin
  1661. oper.opr.localscale:=scale;
  1662. scale:=0;
  1663. end
  1664. else
  1665. Message(asmr_e_wrong_scale_factor);
  1666. end
  1667. else
  1668. Message(asmr_e_invalid_reference_syntax);
  1669. end;
  1670. else
  1671. internalerror(2019050719);
  1672. end;
  1673. end;
  1674. else
  1675. Message(asmr_e_invalid_reference_syntax);
  1676. end;
  1677. if actasmtoken<>AS_REGISTER then
  1678. begin
  1679. if hs<>'' then
  1680. val(hs,l,code);
  1681. case oper.opr.typ of
  1682. OPR_REFERENCE :
  1683. oper.opr.ref.scalefactor:=l;
  1684. OPR_LOCAL :
  1685. oper.opr.localscale:=l;
  1686. else
  1687. internalerror(2019050717);
  1688. end;
  1689. if l>9 then
  1690. Message(asmr_e_wrong_scale_factor);
  1691. end;
  1692. GotPlus:=false;
  1693. GotStar:=false;
  1694. end;
  1695. AS_REGISTER :
  1696. begin
  1697. hreg:=actasmregister;
  1698. Consume(AS_REGISTER);
  1699. if not((GotPlus and (not Negative)) or
  1700. GotStar) then
  1701. Message(asmr_e_invalid_reference_syntax);
  1702. { this register will be the index:
  1703. 1. just read a *
  1704. 2. next token is a *
  1705. 3. base register is already used }
  1706. case oper.opr.typ of
  1707. OPR_LOCAL :
  1708. begin
  1709. if (oper.opr.localindexreg<>NR_NO) then
  1710. Message(asmr_e_multiple_index);
  1711. oper.opr.localindexreg:=hreg;
  1712. if scale<>0 then
  1713. begin
  1714. oper.opr.localscale:=scale;
  1715. scale:=0;
  1716. end;
  1717. end;
  1718. OPR_REFERENCE :
  1719. begin
  1720. if (GotStar) or
  1721. (actasmtoken=AS_STAR) or
  1722. (oper.opr.ref.base<>NR_NO) then
  1723. begin
  1724. if (oper.opr.ref.index<>NR_NO) then
  1725. Message(asmr_e_multiple_index);
  1726. oper.opr.ref.index:=hreg;
  1727. if scale<>0 then
  1728. begin
  1729. oper.opr.ref.scalefactor:=scale;
  1730. scale:=0;
  1731. end;
  1732. end
  1733. else
  1734. begin
  1735. oper.opr.ref.base:=hreg;
  1736. {$ifdef x86_64}
  1737. { non-GOT based RIP-relative accesses are also position-independent }
  1738. if (oper.opr.ref.base=NR_RIP) and
  1739. (oper.opr.ref.refaddr<>addr_pic) then
  1740. oper.opr.ref.refaddr:=addr_pic_no_got;
  1741. {$endif x86_64}
  1742. end;
  1743. end;
  1744. else
  1745. internalerror(2019050718);
  1746. end;
  1747. GotPlus:=false;
  1748. GotStar:=false;
  1749. end;
  1750. AS_OFFSET :
  1751. begin
  1752. Consume(AS_OFFSET);
  1753. GotOffset:=true;
  1754. end;
  1755. AS_TYPE,
  1756. AS_NOT,
  1757. AS_STRING,
  1758. AS_INTNUM,
  1759. AS_LPAREN : { Constant reference expression }
  1760. begin
  1761. if not GotPlus and not GotStar then
  1762. Message(asmr_e_invalid_reference_syntax);
  1763. cse_in_flags:=[cseif_needofs,cseif_isref];
  1764. if GotPlus and negative then
  1765. include(cse_in_flags,cseif_startingminus);
  1766. BuildConstSymbolExpression(cse_in_flags,l,tempstr,tempsymtyp,size,cse_out_flags);
  1767. { already handled by BuildConstSymbolExpression(); must be
  1768. handled there to avoid [reg-1+1] being interpreted as
  1769. [reg-(1+1)] }
  1770. negative:=false;
  1771. if tempstr<>'' then
  1772. begin
  1773. if GotStar then
  1774. Message(asmr_e_only_add_relocatable_symbol);
  1775. if not assigned(oper.opr.ref.symbol) then
  1776. begin
  1777. oper.opr.ref.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp);
  1778. {$ifdef i8086}
  1779. if cseof_isseg in cse_out_flags then
  1780. begin
  1781. if not (oper.opr.ref.refaddr in [addr_fardataseg,addr_dgroup]) then
  1782. oper.opr.ref.refaddr:=addr_seg;
  1783. end
  1784. else if (tempsymtyp=AT_FUNCTION) and (oper.opr.ref.segment=NR_NO) then
  1785. oper.opr.ref.segment:=NR_CS;
  1786. {$endif i8086}
  1787. end
  1788. else
  1789. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1790. end;
  1791. case oper.opr.typ of
  1792. OPR_REFERENCE :
  1793. begin
  1794. if GotStar then
  1795. oper.opr.ref.scalefactor:=l
  1796. else if (prevasmtoken = AS_STAR) then
  1797. begin
  1798. if scale<>0 then
  1799. scale:=l*scale
  1800. else
  1801. scale:=l;
  1802. end
  1803. else
  1804. begin
  1805. Inc(oper.opr.ref.offset,l);
  1806. Inc(oper.opr.constoffset,l);
  1807. end;
  1808. end;
  1809. OPR_LOCAL :
  1810. begin
  1811. if GotStar then
  1812. oper.opr.localscale:=l
  1813. else if (prevasmtoken = AS_STAR) then
  1814. begin
  1815. if scale<>0 then
  1816. scale:=l*scale
  1817. else
  1818. scale:=l;
  1819. end
  1820. else
  1821. Inc(oper.opr.localsymofs,l);
  1822. end;
  1823. else
  1824. internalerror(2019050714);
  1825. end;
  1826. GotPlus:=(prevasmtoken=AS_PLUS) or
  1827. (prevasmtoken=AS_MINUS);
  1828. if GotPlus then
  1829. negative := prevasmtoken = AS_MINUS;
  1830. GotStar:=(prevasmtoken=AS_STAR);
  1831. end;
  1832. //AS_LBRACKET :
  1833. // begin
  1834. // if (GotPlus and Negative) or GotStar then
  1835. // Message(asmr_e_invalid_reference_syntax);
  1836. // tmpoper:=Tz80Operand.create;
  1837. // BuildReference(tmpoper);
  1838. // AddReferences(oper,tmpoper);
  1839. // tmpoper.Free;
  1840. // GotPlus:=false;
  1841. // GotStar:=false;
  1842. // end;
  1843. AS_RPAREN :
  1844. begin
  1845. if GotPlus or GotStar or BracketlessReference then
  1846. Message(asmr_e_invalid_reference_syntax);
  1847. Consume(AS_RPAREN);
  1848. if actasmtoken=AS_LPAREN then
  1849. begin
  1850. tmpoper:=Tz80Operand.create;
  1851. BuildReference(tmpoper);
  1852. AddReferences(oper,tmpoper);
  1853. tmpoper.Free;
  1854. end;
  1855. break;
  1856. end;
  1857. AS_SEPARATOR,
  1858. AS_END,
  1859. AS_COMMA:
  1860. begin
  1861. if not BracketlessReference then
  1862. begin
  1863. Message(asmr_e_invalid_reference_syntax);
  1864. RecoverConsume(true);
  1865. end;
  1866. break;
  1867. end;
  1868. else
  1869. Begin
  1870. Message(asmr_e_invalid_reference_syntax);
  1871. RecoverConsume(true);
  1872. break;
  1873. end;
  1874. end;
  1875. until false;
  1876. end;
  1877. procedure tz80reader.BuildOperand(oper: tz80operand; istypecast: boolean);
  1878. procedure AddLabelOperand(hl:tasmlabel);
  1879. begin
  1880. if (oper.opr.typ=OPR_NONE) and
  1881. is_calljmp(actopcode) then
  1882. begin
  1883. oper.opr.typ:=OPR_SYMBOL;
  1884. oper.opr.symbol:=hl;
  1885. end
  1886. else
  1887. begin
  1888. oper.InitRef;
  1889. oper.opr.ref.symbol:=hl;
  1890. oper.haslabelref:=true;
  1891. end;
  1892. end;
  1893. var
  1894. l: tcgint;
  1895. tsize: tcgint;
  1896. expr: string;
  1897. hl: tasmlabel;
  1898. begin
  1899. repeat
  1900. case actasmtoken of
  1901. AS_OFFSET,
  1902. AS_SIZEOF,
  1903. AS_VMTOFFSET,
  1904. AS_TYPE,
  1905. AS_NOT,
  1906. AS_STRING,
  1907. AS_PLUS,
  1908. AS_MINUS,
  1909. // AS_LPAREN,
  1910. AS_INTNUM :
  1911. begin
  1912. case oper.opr.typ of
  1913. OPR_REFERENCE :
  1914. begin
  1915. l := BuildRefConstExpression(tsize);
  1916. if tsize<>0 then
  1917. oper.SetSize(tsize,false);
  1918. inc(oper.opr.ref.offset,l);
  1919. inc(oper.opr.constoffset,l);
  1920. end;
  1921. OPR_LOCAL :
  1922. begin
  1923. l := BuildConstExpression;
  1924. inc(oper.opr.localsymofs,l);
  1925. inc(oper.opr.localconstoffset,l);
  1926. end;
  1927. OPR_NONE,
  1928. OPR_CONSTANT :
  1929. BuildConstantOperand(oper);
  1930. else
  1931. Message(asmr_e_invalid_operand_type);
  1932. end;
  1933. end;
  1934. AS_LPAREN:
  1935. begin
  1936. BuildReference(oper);
  1937. end;
  1938. AS_ID : { A constant expression, or a Variable ref. }
  1939. Begin
  1940. { Label or Special symbol reference? }
  1941. if actasmpattern[1] = '@' then
  1942. Begin
  1943. if actasmpattern = '@RESULT' then
  1944. Begin
  1945. oper.SetupResult;
  1946. Consume(AS_ID);
  1947. expr:='result';
  1948. end
  1949. else
  1950. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  1951. begin
  1952. Message(asmr_w_CODE_and_DATA_not_supported);
  1953. Consume(AS_ID);
  1954. end
  1955. else
  1956. { Local Label }
  1957. begin
  1958. CreateLocalLabel(actasmpattern,hl,false);
  1959. Consume(AS_ID);
  1960. AddLabelOperand(hl);
  1961. end;
  1962. end
  1963. else
  1964. { support result for delphi modes }
  1965. if (m_objpas in current_settings.modeswitches) and (actasmpattern='RESULT') then
  1966. begin
  1967. oper.SetUpResult;
  1968. Consume(AS_ID);
  1969. expr:='result';
  1970. end
  1971. { probably a variable or normal expression }
  1972. { or a procedure (such as in CALL ID) }
  1973. else
  1974. Begin
  1975. { is it a constant ? }
  1976. if SearchIConstant(actasmpattern,l) then
  1977. Begin
  1978. case oper.opr.typ of
  1979. OPR_REFERENCE :
  1980. begin
  1981. l := BuildRefConstExpression(tsize);
  1982. if tsize<>0 then
  1983. oper.SetSize(tsize,false);
  1984. inc(oper.opr.ref.offset,l);
  1985. inc(oper.opr.constoffset,l);
  1986. end;
  1987. OPR_LOCAL :
  1988. begin
  1989. l := BuildRefConstExpression(tsize);
  1990. if tsize<>0 then
  1991. oper.SetSize(tsize,false);
  1992. inc(oper.opr.localsymofs,l);
  1993. inc(oper.opr.localconstoffset,l);
  1994. end;
  1995. OPR_NONE,
  1996. OPR_CONSTANT :
  1997. BuildConstantOperand(oper);
  1998. else
  1999. Message(asmr_e_invalid_operand_type);
  2000. end;
  2001. end
  2002. else
  2003. { Check for pascal label }
  2004. if SearchLabel(actasmpattern,hl,false) then
  2005. begin
  2006. Consume(AS_ID);
  2007. AddLabelOperand(hl);
  2008. end
  2009. else
  2010. { is it a normal variable ? }
  2011. Begin
  2012. expr:=actasmpattern;
  2013. Consume(AS_ID);
  2014. { typecasting? }
  2015. if SearchType(expr,l) then
  2016. begin
  2017. oper.hastype:=true;
  2018. oper.typesize:=l;
  2019. case actasmtoken of
  2020. AS_LPAREN :
  2021. begin
  2022. { Support Type([Reference]) }
  2023. Consume(AS_LPAREN);
  2024. BuildOperand(oper,true);
  2025. { Delphi also supports Type(Register) and
  2026. interprets it the same as Type([Register]). }
  2027. if (oper.opr.typ = OPR_REGISTER) then
  2028. { This also sets base to the register. }
  2029. oper.InitRef;
  2030. Consume(AS_RPAREN);
  2031. end;
  2032. //AS_LBRACKET :
  2033. // begin
  2034. // { Support Var.Type[Index] }
  2035. // { Convert @label.Byte[1] to reference }
  2036. // if oper.opr.typ=OPR_SYMBOL then
  2037. // oper.initref;
  2038. // end;
  2039. else
  2040. ;
  2041. end;
  2042. end
  2043. else
  2044. begin
  2045. if not oper.SetupVar(expr,false) then
  2046. Begin
  2047. { not a variable, check special variables.. }
  2048. if expr = 'SELF' then
  2049. begin
  2050. oper.SetupSelf;
  2051. expr:='self';
  2052. end
  2053. else
  2054. begin
  2055. Message1(sym_e_unknown_id,expr);
  2056. expr:='';
  2057. end;
  2058. end;
  2059. { indexed access to variable? }
  2060. //if actasmtoken=AS_LBRACKET then
  2061. // begin
  2062. // { ... then the operand size is not known anymore }
  2063. // oper.size:=OS_NO;
  2064. // BuildReference(oper);
  2065. // end;
  2066. end;
  2067. end;
  2068. end;
  2069. end;
  2070. AS_REGISTER : { Register, a variable reference or a constant reference }
  2071. begin
  2072. Consume(AS_REGISTER);
  2073. { Simple register }
  2074. if (oper.opr.typ <> OPR_NONE) then
  2075. Message(asmr_e_syn_operand);
  2076. oper.opr.typ:=OPR_REGISTER;
  2077. oper.opr.reg:=actasmregister;
  2078. oper.SetSize(tcgsize2size[reg_cgsize(oper.opr.reg)],true);
  2079. end;
  2080. AS_SEPARATOR,
  2081. AS_END,
  2082. AS_COMMA:
  2083. begin
  2084. break;
  2085. end;
  2086. else
  2087. begin
  2088. Message(asmr_e_syn_operand);
  2089. RecoverConsume(true);
  2090. break;
  2091. end;
  2092. end;
  2093. until false;
  2094. end;
  2095. procedure tz80reader.BuildOpCode(instr: TZ80Instruction);
  2096. var
  2097. operandnum: Integer;
  2098. begin
  2099. instr.opcode:=actopcode;
  2100. operandnum:=1;
  2101. Consume(AS_OPCODE);
  2102. { Zero operand opcode ? }
  2103. if actasmtoken in [AS_SEPARATOR,AS_END] then
  2104. exit;
  2105. { Condition (e.g. 'NC' in 'JP NC, label') }
  2106. if actasmtoken=AS_CONDITION then
  2107. begin
  2108. instr.condition:=actasmcond;
  2109. Consume(AS_CONDITION);
  2110. if actasmtoken in [AS_SEPARATOR,AS_END] then
  2111. exit;
  2112. if actasmtoken=AS_COMMA then
  2113. Consume(AS_COMMA);
  2114. end;
  2115. { Read Operands }
  2116. repeat
  2117. case actasmtoken of
  2118. { End of asm operands for this opcode }
  2119. AS_END,
  2120. AS_SEPARATOR :
  2121. break;
  2122. { Operand delimiter }
  2123. AS_COMMA :
  2124. begin
  2125. { should have something before the comma }
  2126. if instr.operands[operandnum].opr.typ=OPR_NONE then
  2127. Message(asmr_e_syntax_error);
  2128. if operandnum >= max_operands then
  2129. Message(asmr_e_too_many_operands)
  2130. else
  2131. Inc(operandnum);
  2132. Consume(AS_COMMA);
  2133. end;
  2134. else
  2135. BuildOperand(instr.Operands[operandnum] as tz80operand,false);
  2136. end;
  2137. until false;
  2138. instr.ops:=operandnum;
  2139. end;
  2140. procedure tz80reader.BuildConstant(constsize: byte);
  2141. var
  2142. asmsymtyp : TAsmSymType;
  2143. asmsym,
  2144. expr : string;
  2145. value, sz : tcgint;
  2146. inflags : tconstsymbolexpressioninputflags;
  2147. outflags : tconstsymbolexpressionoutputflags;
  2148. begin
  2149. repeat
  2150. case actasmtoken of
  2151. AS_STRING:
  2152. Begin
  2153. expr:=actasmpattern;
  2154. if length(expr) > 1 then
  2155. Message(asmr_e_string_not_allowed_as_const);
  2156. Consume(AS_STRING);
  2157. case actasmtoken of
  2158. AS_COMMA: Consume(AS_COMMA);
  2159. AS_END,
  2160. AS_SEPARATOR: ;
  2161. else
  2162. Message(asmr_e_invalid_string_expression);
  2163. end; { end case }
  2164. ConcatString(curlist,expr);
  2165. end;
  2166. AS_INTNUM,
  2167. AS_PLUS,
  2168. AS_MINUS,
  2169. AS_LPAREN,
  2170. AS_TYPE,
  2171. AS_SIZEOF,
  2172. AS_NOT,
  2173. AS_VMTOFFSET,
  2174. AS_ID :
  2175. begin
  2176. inflags:=[];
  2177. BuildConstSymbolExpression(inflags,value,asmsym,asmsymtyp,sz,outflags);
  2178. if asmsym<>'' then
  2179. begin
  2180. if constsize<>sizeof(pint) then
  2181. Message(asmr_w_32bit_const_for_address);
  2182. ConcatConstSymbol(curlist,asmsym,'',asmsymtyp,value,constsize,true)
  2183. end
  2184. else
  2185. ConcatConstant(curlist,value,constsize);
  2186. end;
  2187. AS_COMMA:
  2188. Consume(AS_COMMA);
  2189. AS_END,
  2190. AS_SEPARATOR:
  2191. break;
  2192. else
  2193. begin
  2194. Message(asmr_e_syn_constant);
  2195. RecoverConsume(false);
  2196. end
  2197. end; { end case }
  2198. until false;
  2199. end;
  2200. procedure tz80reader.handleopcode;
  2201. var
  2202. instr: TZ80Instruction;
  2203. begin
  2204. instr:=TZ80Instruction.create(TZ80Operand);
  2205. BuildOpcode(instr);
  2206. with instr do
  2207. begin
  2208. //CheckNonCommutativeOpcodes;
  2209. //AddReferenceSizes;
  2210. //SetInstructionOpsize;
  2211. //CheckOperandSizes;
  2212. ConcatInstruction(curlist);
  2213. end;
  2214. instr.Free;
  2215. end;
  2216. procedure tz80reader.ConvertCalljmp(instr : tz80instruction);
  2217. var
  2218. newopr : toprrec;
  2219. begin
  2220. if instr.Operands[1].opr.typ=OPR_REFERENCE then
  2221. begin
  2222. newopr.typ:=OPR_SYMBOL;
  2223. newopr.symbol:=instr.Operands[1].opr.ref.symbol;
  2224. newopr.symofs:=instr.Operands[1].opr.ref.offset;
  2225. if (instr.Operands[1].opr.ref.base<>NR_NO) or
  2226. (instr.Operands[1].opr.ref.index<>NR_NO) then
  2227. Message(asmr_e_syn_operand);
  2228. instr.Operands[1].opr:=newopr;
  2229. end;
  2230. end;
  2231. function tz80reader.Assemble: tlinkedlist;
  2232. var
  2233. hl: tasmlabel;
  2234. sectionname: String;
  2235. section: tai_section;
  2236. begin
  2237. Message1(asmr_d_start_reading,'Z80');
  2238. firsttoken:=TRUE;
  2239. { sets up all opcode and register tables in uppercase }
  2240. if not _asmsorted then
  2241. begin
  2242. SetupTables;
  2243. _asmsorted:=TRUE;
  2244. end;
  2245. curlist:=TAsmList.Create;
  2246. { we might need to know which parameters are passed in registers }
  2247. if not parse_generic then
  2248. current_procinfo.generate_parameter_info;
  2249. { start tokenizer }
  2250. gettoken;
  2251. { main loop }
  2252. repeat
  2253. case actasmtoken of
  2254. AS_LLABEL:
  2255. Begin
  2256. if CreateLocalLabel(actasmpattern,hl,true) then
  2257. ConcatLabel(curlist,hl);
  2258. Consume(AS_LLABEL);
  2259. end;
  2260. AS_LABEL:
  2261. Begin
  2262. if SearchLabel(upper(actasmpattern),hl,true) then
  2263. begin
  2264. if hl.is_public then
  2265. ConcatPublic(curlist,actasmpattern_origcase);
  2266. ConcatLabel(curlist,hl);
  2267. end
  2268. else
  2269. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  2270. Consume(AS_LABEL);
  2271. end;
  2272. AS_END:
  2273. begin
  2274. break; { end assembly block }
  2275. end;
  2276. AS_SEPARATOR:
  2277. begin
  2278. Consume(AS_SEPARATOR);
  2279. end;
  2280. AS_DEFB :
  2281. Begin
  2282. inexpression:=true;
  2283. Consume(AS_DEFB);
  2284. BuildConstant(1);
  2285. inexpression:=false;
  2286. end;
  2287. AS_DEFW :
  2288. Begin
  2289. inexpression:=true;
  2290. Consume(AS_DEFW);
  2291. BuildConstant(2);
  2292. inexpression:=false;
  2293. end;
  2294. AS_AREA :
  2295. begin
  2296. Consume(AS_AREA);
  2297. sectionname:=actasmpattern;
  2298. {secflags:=[];
  2299. secprogbits:=SPB_None;}
  2300. Consume(AS_STRING);
  2301. {if actasmtoken=AS_COMMA then
  2302. begin
  2303. Consume(AS_COMMA);
  2304. if actasmtoken=AS_STRING then
  2305. begin
  2306. case actasmpattern of
  2307. 'a':
  2308. Include(secflags,SF_A);
  2309. 'w':
  2310. Include(secflags,SF_W);
  2311. 'x':
  2312. Include(secflags,SF_X);
  2313. '':
  2314. ;
  2315. else
  2316. Message(asmr_e_syntax_error);
  2317. end;
  2318. Consume(AS_STRING);
  2319. if actasmtoken=AS_COMMA then
  2320. begin
  2321. Consume(AS_COMMA);
  2322. if (actasmtoken=AS_MOD) or (actasmtoken=AS_AT) then
  2323. begin
  2324. Consume(actasmtoken);
  2325. if actasmtoken=AS_ID then
  2326. begin
  2327. case actasmpattern of
  2328. 'PROGBITS':
  2329. secprogbits:=SPB_PROGBITS;
  2330. 'NOBITS':
  2331. secprogbits:=SPB_NOBITS;
  2332. 'NOTE':
  2333. secprogbits:=SPB_NOTE;
  2334. else
  2335. Message(asmr_e_syntax_error);
  2336. end;
  2337. Consume(AS_ID);
  2338. end
  2339. else
  2340. Message(asmr_e_syntax_error);
  2341. end
  2342. else
  2343. Message(asmr_e_syntax_error);
  2344. end;
  2345. end
  2346. else
  2347. Message(asmr_e_syntax_error);
  2348. end;}
  2349. //curList.concat(tai_section.create(sec_user, actasmpattern, 0));
  2350. section:=new_section(curlist, sec_user, sectionname, 0);
  2351. //section.secflags:=secflags;
  2352. //section.secprogbits:=secprogbits;
  2353. end;
  2354. AS_OPCODE:
  2355. begin
  2356. HandleOpCode;
  2357. end;
  2358. else
  2359. begin
  2360. Message(asmr_e_syntax_error);
  2361. RecoverConsume(false);
  2362. end;
  2363. end;
  2364. until false;
  2365. { check that all referenced local labels are defined }
  2366. checklocallabels;
  2367. { Return the list in an asmnode }
  2368. assemble:=curlist;
  2369. Message1(asmr_d_finish_reading,'Z80');
  2370. end;
  2371. {*****************************************************************************
  2372. Initialize
  2373. *****************************************************************************}
  2374. const
  2375. { asmmode_z80_att_info : tasmmodeinfo =
  2376. (
  2377. id : asmmode_z80_gas;
  2378. idtxt : 'GAS';
  2379. casmreader : tz80attreader;
  2380. );}
  2381. asmmode_z80_standard_info : tasmmodeinfo =
  2382. (
  2383. id : asmmode_standard;
  2384. idtxt : 'STANDARD';
  2385. casmreader : tz80reader;
  2386. );
  2387. initialization
  2388. // RegisterAsmMode(asmmode_z80_att_info);
  2389. RegisterAsmMode(asmmode_z80_standard_info);
  2390. end.