rautils.pas 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. This unit implements some support routines for assembler parsing
  4. independent of the processor
  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. Unit RAUtils;
  18. {$i fpcdefs.inc}
  19. Interface
  20. Uses
  21. cutils,cclasses,
  22. globtype,aasmbase,aasmtai,aasmdata,cpubase,cpuinfo,cgbase,cgutils,
  23. symconst,symbase,symtype,symdef,symsym,constexp,symcpu;
  24. Const
  25. RPNMax = 10; { I think you only need 4, but just to be safe }
  26. OpMax = 25;
  27. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  28. {---------------------------------------------------------------------
  29. Instruction management
  30. ---------------------------------------------------------------------}
  31. type
  32. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,OPR_LOCAL,
  33. OPR_REFERENCE,OPR_REGISTER,OPR_COND,OPR_REGSET,
  34. OPR_SHIFTEROP,OPR_MODEFLAGS,OPR_SPECIALREG,
  35. OPR_REGPAIR);
  36. TOprRec = record
  37. case typ:TOprType of
  38. OPR_NONE : ();
  39. {$if defined(AVR)}
  40. OPR_CONSTANT : (val:longint);
  41. {$elseif defined(i8086)}
  42. OPR_CONSTANT : (val:longint);
  43. {$else}
  44. OPR_CONSTANT : (val:aint);
  45. {$endif}
  46. OPR_SYMBOL : (symbol:tasmsymbol;symofs:aint;symseg:boolean;sym_farproc_entry:boolean);
  47. OPR_REFERENCE : (varsize:asizeint; constoffset: asizeint;ref_farproc_entry:boolean;ref:treference);
  48. OPR_LOCAL : (localvarsize, localconstoffset: asizeint;localsym:tabstractnormalvarsym;localsymofs:aint;localindexreg:tregister;localscale:byte;localgetoffset,localforceref:boolean);
  49. OPR_REGISTER : (reg:tregister);
  50. {$ifdef m68k}
  51. OPR_REGSET : (regsetdata,regsetaddr,regsetfpu : tcpuregisterset);
  52. OPR_REGPAIR : (reghi,reglo: tregister);
  53. {$endif m68k}
  54. {$ifdef powerpc}
  55. OPR_COND : (cond : tasmcond);
  56. {$endif powerpc}
  57. {$ifdef POWERPC64}
  58. OPR_COND : (cond : tasmcond);
  59. {$endif POWERPC64}
  60. {$ifdef arm}
  61. OPR_REGSET : (regset : tcpuregisterset; regtype: tregistertype; subreg: tsubregister; usermode: boolean);
  62. OPR_SHIFTEROP : (shifterop : tshifterop);
  63. OPR_COND : (cc : tasmcond);
  64. OPR_MODEFLAGS : (flags : tcpumodeflags);
  65. OPR_SPECIALREG: (specialreg : tregister; specialregflags : tspecialregflags);
  66. {$endif arm}
  67. {$ifdef aarch64}
  68. OPR_SHIFTEROP : (shifterop : tshifterop);
  69. OPR_COND : (cc : tasmcond);
  70. {$endif aarch64}
  71. end;
  72. TOperand = class
  73. opr : TOprRec;
  74. typesize : byte;
  75. haslabelref, { if the operand has a label, used in a reference like a
  76. var (e.g. 'mov ax, word ptr [label+5]', but *not*
  77. e.g. 'jmp label') }
  78. hasproc, { if the operand has a procedure/function reference }
  79. hastype, { if the operand has typecasted variable }
  80. hasvar : boolean; { if the operand is loaded with a variable }
  81. size : TCGSize;
  82. constructor create;virtual;
  83. destructor destroy;override;
  84. Procedure SetSize(_size:longint;force:boolean);virtual;
  85. Procedure SetCorrectSize(opcode:tasmop);virtual;
  86. Function SetupResult:boolean;virtual;
  87. Function SetupSelf:boolean;
  88. Function SetupOldEBP:boolean;
  89. Function SetupVar(const s:string;GetOffset : boolean): Boolean;
  90. Function CheckOperand: boolean; virtual;
  91. Procedure InitRef;
  92. Procedure InitRefConvertLocal;
  93. protected
  94. Procedure InitRefError;
  95. end;
  96. TCOperand = class of TOperand;
  97. TInstruction = class
  98. operands : array[1..max_operands] of toperand;
  99. opcode : tasmop;
  100. condition : tasmcond;
  101. ops : byte;
  102. labeled : boolean;
  103. filepos : tfileposinfo;
  104. constructor create(optype : tcoperand);virtual;
  105. destructor destroy;override;
  106. { converts the instruction to an instruction how it's used by the assembler writer
  107. and concats it to the passed list. The newly created item is returned if the
  108. instruction was valid, otherwise nil is returned }
  109. function ConcatInstruction(p:TAsmList) : tai;virtual;
  110. end;
  111. {---------------------------------------------------------------------}
  112. { Expression parser types }
  113. {---------------------------------------------------------------------}
  114. TExprOperator = record
  115. ch: char; { operator }
  116. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  117. end;
  118. {**********************************************************************}
  119. { The following operators are supported: }
  120. { '+' : addition }
  121. { '-' : subtraction }
  122. { '*' : multiplication }
  123. { '/' : modulo division }
  124. { '^' : exclusive or }
  125. { '<' : shift left }
  126. { '>' : shift right }
  127. { '&' : bitwise and }
  128. { '|' : bitwise or }
  129. { '~' : bitwise complement }
  130. { '%' : modulo division }
  131. { nnn: longint numbers }
  132. { ( and ) parenthesis }
  133. {**********************************************************************}
  134. TExprParse = class
  135. public
  136. Constructor create;
  137. Destructor Destroy;override;
  138. Function Evaluate(Expr: String): tcgint;
  139. Function Priority(_Operator: Char): aint;
  140. private
  141. RPNStack : Array[1..RPNMax] of tcgint; { Stack For RPN calculator }
  142. RPNTop : tcgint;
  143. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  144. OpTop : tcgint;
  145. Procedure RPNPush(Num: tcgint);
  146. Function RPNPop: tcgint;
  147. Procedure RPNCalc(const token: String; prefix: boolean);
  148. Procedure OpPush(_Operator: char; prefix: boolean);
  149. { In reality returns TExprOperaotr }
  150. Procedure OpPop(var _Operator:TExprOperator);
  151. end;
  152. { Evaluate an expression string to a tcgint }
  153. Function CalculateExpression(const expression: string): tcgint;
  154. {---------------------------------------------------------------------}
  155. { String routines }
  156. {---------------------------------------------------------------------}
  157. Function ParseVal(const S:String;base:byte):tcgint;
  158. Function PadZero(Var s: String; n: byte): Boolean;
  159. Function EscapeToPascal(const s:string): string;
  160. {---------------------------------------------------------------------
  161. Symbol helper routines
  162. ---------------------------------------------------------------------}
  163. procedure AsmSearchSym(const s:string;out srsym:tsym;out srsymtable:TSymtable);
  164. Function GetRecordOffsetSize(s:string;out Offset: tcgint;out Size:tcgint; out mangledname: string; needvmtofs: boolean; out hastypecast: boolean):boolean;
  165. Function SearchType(const hs:string;out size:tcgint): Boolean;
  166. Function SearchRecordType(const s:string): boolean;
  167. Function SearchIConstant(const s:string; var l:tcgint): boolean;
  168. Function AsmRegisterPara(sym: tabstractnormalvarsym): boolean;
  169. {---------------------------------------------------------------------
  170. Instruction generation routines
  171. ---------------------------------------------------------------------}
  172. Procedure ConcatLabel(p: TAsmList;var l : tasmlabel);
  173. Procedure ConcatConstant(p : TAsmList;value: tcgint; constsize:byte);
  174. Procedure ConcatConstSymbol(p : TAsmList;const sym:string;symtyp:tasmsymtype;l:tcgint;constsize:byte;isofs:boolean);
  175. Procedure ConcatRealConstant(p : TAsmList;value: bestreal; real_typ : tfloattype);
  176. Procedure ConcatString(p : TAsmList;s:string);
  177. procedure ConcatAlign(p:TAsmList;l:tcgint);
  178. Procedure ConcatPublic(p:TAsmList;const s : string);
  179. Procedure ConcatLocal(p:TAsmList;const s : string);
  180. Implementation
  181. uses
  182. SysUtils,
  183. defutil,systems,verbose,globals,
  184. symtable,paramgr,
  185. aasmcpu,
  186. procinfo;
  187. {*************************************************************************
  188. TExprParse
  189. *************************************************************************}
  190. Constructor TExprParse.create;
  191. Begin
  192. end;
  193. Procedure TExprParse.RPNPush(Num : tcgint);
  194. { Add an operand to the top of the RPN stack }
  195. begin
  196. if RPNTop < RPNMax then
  197. begin
  198. Inc(RPNTop);
  199. RPNStack[RPNTop]:=Num;
  200. end
  201. else
  202. Message(asmr_e_expr_illegal);
  203. end;
  204. Function TExprParse.RPNPop : tcgint;
  205. { Get the operand at the top of the RPN stack }
  206. begin
  207. RPNPop:=0;
  208. if RPNTop > 0 then
  209. begin
  210. RPNPop:=RPNStack[RPNTop];
  211. Dec(RPNTop);
  212. end
  213. else
  214. Message(asmr_e_expr_illegal);
  215. end;
  216. Procedure TExprParse.RPNCalc(const Token : String; prefix:boolean); { RPN Calculator }
  217. Var
  218. Temp : tcgint;
  219. n1,n2 : tcgint;
  220. LocalError : Integer;
  221. begin
  222. { Handle operators }
  223. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  224. Case Token[1] of
  225. '+' :
  226. Begin
  227. if not prefix then
  228. RPNPush(RPNPop + RPNPop);
  229. end;
  230. '-' :
  231. Begin
  232. if prefix then
  233. RPNPush(-(RPNPop))
  234. else
  235. begin
  236. n1:=RPNPop;
  237. n2:=RPNPop;
  238. RPNPush(n2 - n1);
  239. end;
  240. end;
  241. '*' : RPNPush(RPNPop * RPNPop);
  242. '&' :
  243. begin
  244. n1:=RPNPop;
  245. n2:=RPNPop;
  246. RPNPush(n2 and n1);
  247. end;
  248. '|' :
  249. begin
  250. n1:=RPNPop;
  251. n2:=RPNPop;
  252. RPNPush(n2 or n1);
  253. end;
  254. '~' : RPNPush(NOT RPNPop);
  255. '<' :
  256. begin
  257. n1:=RPNPop;
  258. n2:=RPNPop;
  259. RPNPush(n2 SHL n1);
  260. end;
  261. '>' :
  262. begin
  263. n1:=RPNPop;
  264. n2:=RPNPop;
  265. RPNPush(n2 SHR n1);
  266. end;
  267. '%' :
  268. begin
  269. Temp:=RPNPop;
  270. if Temp <> 0 then
  271. RPNPush(RPNPop mod Temp)
  272. else
  273. begin
  274. Message(asmr_e_expr_zero_divide);
  275. { push 1 for error recovery }
  276. RPNPush(1);
  277. end;
  278. end;
  279. '^' : RPNPush(RPNPop XOR RPNPop);
  280. '/' :
  281. begin
  282. Temp:=RPNPop;
  283. if Temp <> 0 then
  284. RPNPush(RPNPop div Temp)
  285. else
  286. begin
  287. Message(asmr_e_expr_zero_divide);
  288. { push 1 for error recovery }
  289. RPNPush(1);
  290. end;
  291. end;
  292. end
  293. else
  294. begin
  295. { Convert String to number and add to stack }
  296. Val(Token, Temp, LocalError);
  297. if LocalError = 0 then
  298. RPNPush(Temp)
  299. else
  300. begin
  301. Message(asmr_e_expr_illegal);
  302. { push 1 for error recovery }
  303. RPNPush(1);
  304. end;
  305. end;
  306. end;
  307. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  308. { Add an operator onto top of the stack }
  309. begin
  310. if OpTop < OpMax then
  311. begin
  312. Inc(OpTop);
  313. OpStack[OpTop].ch:=_Operator;
  314. OpStack[OpTop].is_prefix:=prefix;
  315. end
  316. else
  317. Message(asmr_e_expr_illegal);
  318. end;
  319. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  320. { Get operator at the top of the stack }
  321. begin
  322. if OpTop > 0 then
  323. begin
  324. _Operator:=OpStack[OpTop];
  325. Dec(OpTop);
  326. end
  327. else
  328. Message(asmr_e_expr_illegal);
  329. end;
  330. Function TExprParse.Priority(_Operator : Char) : aint;
  331. { Return priority of operator }
  332. { The greater the priority, the higher the precedence }
  333. begin
  334. Priority:=0;
  335. Case _Operator OF
  336. '(' :
  337. Priority:=0;
  338. '|','^','~' : // the lowest priority: OR, XOR, NOT
  339. Priority:=0;
  340. '&' : // bigger priority: AND
  341. Priority:=1;
  342. '+', '-' : // bigger priority: +, -
  343. Priority:=2;
  344. '*', '/','%','<','>' : // the highest priority: *, /, MOD, SHL, SHR
  345. Priority:=3;
  346. else
  347. Message(asmr_e_expr_illegal);
  348. end;
  349. end;
  350. Function TExprParse.Evaluate(Expr : String):tcgint;
  351. Var
  352. I : longint;
  353. Token : String;
  354. opr : TExprOperator;
  355. begin
  356. Evaluate:=0;
  357. { Reset stacks }
  358. OpTop :=0;
  359. RPNTop:=0;
  360. Token :='';
  361. { nothing to do ? }
  362. if Expr='' then
  363. exit;
  364. For I:=1 to Length(Expr) DO
  365. begin
  366. if Expr[I] in ['0'..'9'] then
  367. begin { Build multi-digit numbers }
  368. Token:=Token + Expr[I];
  369. if I = Length(Expr) then { Send last one to calculator }
  370. RPNCalc(Token,false);
  371. end
  372. else
  373. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  374. begin
  375. if Token <> '' then
  376. begin { Send last built number to calc. }
  377. RPNCalc(Token,false);
  378. Token:='';
  379. end;
  380. Case Expr[I] OF
  381. '(' : OpPush('(',false);
  382. ')' : begin
  383. While (OpTop>0) and (OpStack[OpTop].ch <> '(') DO
  384. Begin
  385. OpPop(opr);
  386. RPNCalc(opr.ch,opr.is_prefix);
  387. end;
  388. OpPop(opr); { Pop off and ignore the '(' }
  389. end;
  390. '+','-','~' : Begin
  391. { workaround for -2147483648 }
  392. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  393. begin
  394. token:='-';
  395. expr[i]:='+';
  396. end;
  397. { if start of expression then surely a prefix }
  398. { or if previous char was also an operator }
  399. if (I = 1) or (not (Expr[I-1] in ['0'..'9',')'])) then
  400. OpPush(Expr[I],true)
  401. else
  402. Begin
  403. { Evaluate all higher priority operators }
  404. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  405. Begin
  406. OpPop(opr);
  407. RPNCalc(opr.ch,opr.is_prefix);
  408. end;
  409. OpPush(Expr[I],false);
  410. End;
  411. end;
  412. '*', '/',
  413. '^','|','&',
  414. '%','<','>' : begin
  415. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  416. Begin
  417. OpPop(opr);
  418. RPNCalc(opr.ch,opr.is_prefix);
  419. end;
  420. OpPush(Expr[I],false);
  421. end;
  422. end; { Case }
  423. end
  424. else
  425. Message(asmr_e_expr_illegal); { Handle bad input error }
  426. end;
  427. { Pop off the remaining operators }
  428. While OpTop > 0 do
  429. Begin
  430. OpPop(opr);
  431. RPNCalc(opr.ch,opr.is_prefix);
  432. end;
  433. { The result is stored on the top of the stack }
  434. Evaluate:=RPNPop;
  435. end;
  436. Destructor TExprParse.Destroy;
  437. Begin
  438. end;
  439. Function CalculateExpression(const expression: string): tcgint;
  440. var
  441. expr: TExprParse;
  442. Begin
  443. expr:=TExprParse.create;
  444. CalculateExpression:=expr.Evaluate(expression);
  445. expr.Free;
  446. end;
  447. {*************************************************************************}
  448. { String conversions/utils }
  449. {*************************************************************************}
  450. Function EscapeToPascal(const s:string): string;
  451. { converts a C styled string - which contains escape }
  452. { characters to a pascal style string. }
  453. var
  454. i,len : asizeint;
  455. hs : string;
  456. temp : string;
  457. c : char;
  458. Begin
  459. hs:='';
  460. len:=0;
  461. i:=0;
  462. while (i<length(s)) and (len<255) do
  463. begin
  464. Inc(i);
  465. if (s[i]='\') and (i<length(s)) then
  466. Begin
  467. inc(i);
  468. case s[i] of
  469. '\' :
  470. c:='\';
  471. 'b':
  472. c:=#8;
  473. 'f':
  474. c:=#12;
  475. 'n':
  476. c:=#10;
  477. 'r':
  478. c:=#13;
  479. 't':
  480. c:=#9;
  481. '"':
  482. c:='"';
  483. '0'..'7':
  484. Begin
  485. temp:=s[i];
  486. temp:=temp+s[i+1];
  487. temp:=temp+s[i+2];
  488. inc(i,2);
  489. c:=chr(ParseVal(temp,8));
  490. end;
  491. 'x':
  492. Begin
  493. temp:=s[i+1];
  494. temp:=temp+s[i+2];
  495. inc(i,2);
  496. c:=chr(ParseVal(temp,16));
  497. end;
  498. else
  499. Begin
  500. Message1(asmr_e_escape_seq_ignored,s[i]);
  501. c:=s[i];
  502. end;
  503. end;
  504. end
  505. else
  506. c:=s[i];
  507. inc(len);
  508. hs[len]:=c;
  509. end;
  510. hs[0]:=chr(len);
  511. EscapeToPascal:=hs;
  512. end;
  513. Function ParseVal(const S:String;base:byte):tcgint;
  514. { Converts a decimal string to tcgint }
  515. var
  516. code : integer;
  517. errmsg : word;
  518. prefix : string[2];
  519. Begin
  520. case base of
  521. 2 :
  522. begin
  523. errmsg:=asmr_e_error_converting_binary;
  524. prefix:='%';
  525. end;
  526. 8 :
  527. begin
  528. errmsg:=asmr_e_error_converting_octal;
  529. prefix:='&';
  530. end;
  531. 10 :
  532. begin
  533. errmsg:=asmr_e_error_converting_decimal;
  534. prefix:='';
  535. end;
  536. 16 :
  537. begin
  538. errmsg:=asmr_e_error_converting_hexadecimal;
  539. prefix:='$';
  540. end;
  541. else
  542. internalerror(200501202);
  543. end;
  544. val(prefix+s,result,code);
  545. if code<>0 then
  546. begin
  547. val(prefix+s,result,code);
  548. if code<>0 then
  549. begin
  550. Message1(errmsg,s);
  551. result:=0;
  552. end;
  553. end;
  554. end;
  555. Function PadZero(Var s: String; n: byte): Boolean;
  556. Begin
  557. PadZero:=TRUE;
  558. { Do some error checking first }
  559. if Length(s) = n then
  560. exit
  561. else
  562. if Length(s) > n then
  563. Begin
  564. PadZero:=FALSE;
  565. delete(s,n+1,length(s));
  566. exit;
  567. end
  568. else
  569. PadZero:=TRUE;
  570. { Fill it up with the specified character }
  571. fillchar(s[length(s)+1],n-1,#0);
  572. s[0]:=chr(n);
  573. end;
  574. {****************************************************************************
  575. TOperand
  576. ****************************************************************************}
  577. constructor TOperand.Create;
  578. begin
  579. size:=OS_NO;
  580. hasproc:=false;
  581. hastype:=false;
  582. hasvar:=false;
  583. FillChar(Opr,sizeof(Opr),0);
  584. end;
  585. destructor TOperand.destroy;
  586. begin
  587. end;
  588. Procedure TOperand.SetSize(_size:longint;force:boolean);
  589. begin
  590. if force or
  591. ((size = OS_NO) and (_size<=16)) then
  592. Begin
  593. case _size of
  594. 1 : size:=OS_8;
  595. 2 : size:=OS_16{ could be S_IS};
  596. 4 : size:=OS_32{ could be S_IL or S_FS};
  597. 8 : size:=OS_64{ could be S_D or S_FL};
  598. 10 : size:=OS_F80;
  599. 16 : size:=OS_128;
  600. end;
  601. end;
  602. end;
  603. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  604. begin
  605. end;
  606. function TOperand.SetupResult:boolean;
  607. begin
  608. SetupResult:=false;
  609. { replace by correct offset. }
  610. with current_procinfo.procdef do
  611. if (not is_void(returndef)) then
  612. begin
  613. if (m_tp7 in current_settings.modeswitches) and
  614. not (df_generic in defoptions) and
  615. (po_assembler in procoptions) and
  616. (not paramanager.ret_in_param(returndef,current_procinfo.procdef)) then
  617. begin
  618. message(asmr_e_cannot_use_RESULT_here);
  619. exit;
  620. end;
  621. SetupResult:=setupvar('result',false)
  622. end
  623. else
  624. message(asmr_e_void_function);
  625. end;
  626. Function TOperand.SetupSelf:boolean;
  627. Begin
  628. SetupSelf:=false;
  629. if assigned(current_structdef) then
  630. SetupSelf:=setupvar('self',false)
  631. else
  632. Message(asmr_e_cannot_use_SELF_outside_a_method);
  633. end;
  634. Function TOperand.SetupOldEBP:boolean;
  635. Begin
  636. SetupOldEBP:=false;
  637. if current_procinfo.procdef.parast.symtablelevel>normal_function_level then
  638. SetupOldEBP:=setupvar('parentframe',false)
  639. else
  640. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  641. end;
  642. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  643. function symtable_has_localvarsyms(st:TSymtable):boolean;
  644. var
  645. sym : tsym;
  646. i : longint;
  647. begin
  648. result:=false;
  649. for i:=0 to st.SymList.Count-1 do
  650. begin
  651. sym:=tsym(st.SymList[i]);
  652. if sym.typ=localvarsym then
  653. begin
  654. result:=true;
  655. exit;
  656. end;
  657. end;
  658. end;
  659. procedure setconst(l:aint);
  660. begin
  661. { We return the address of the field, just like Delphi/TP }
  662. case opr.typ of
  663. OPR_NONE :
  664. begin
  665. opr.typ:=OPR_CONSTANT;
  666. opr.val:=l;
  667. end;
  668. OPR_CONSTANT :
  669. inc(opr.val,l);
  670. OPR_REFERENCE :
  671. inc(opr.ref.offset,l);
  672. OPR_LOCAL :
  673. inc(opr.localsymofs,l);
  674. else
  675. Message(asmr_e_invalid_operand_type);
  676. end;
  677. end;
  678. procedure setvarsize(sym: tabstractvarsym);
  679. var
  680. harrdef: tarraydef;
  681. l: asizeint;
  682. begin
  683. case sym.vardef.typ of
  684. orddef,
  685. enumdef,
  686. pointerdef,
  687. procvardef,
  688. floatdef :
  689. SetSize(sym.getsize,false);
  690. arraydef :
  691. begin
  692. { for arrays try to get the element size, take care of
  693. multiple indexes }
  694. harrdef:=tarraydef(sym.vardef);
  695. { calc array size }
  696. if is_special_array(harrdef) then
  697. l := -1
  698. else
  699. l := harrdef.size;
  700. case opr.typ of
  701. OPR_REFERENCE: opr.varsize := l;
  702. OPR_LOCAL: opr.localvarsize := l;
  703. end;
  704. while assigned(harrdef.elementdef) and
  705. (harrdef.elementdef.typ=arraydef) do
  706. harrdef:=tarraydef(harrdef.elementdef);
  707. if not is_packed_array(harrdef) then
  708. SetSize(harrdef.elesize,false)
  709. else
  710. if (harrdef.elepackedbitsize mod 8) = 0 then
  711. SetSize(harrdef.elepackedbitsize div 8,false);
  712. end;
  713. recorddef:
  714. case opr.typ of
  715. OPR_REFERENCE: opr.varsize := sym.getsize;
  716. OPR_LOCAL: opr.localvarsize := sym.getsize;
  717. end;
  718. end;
  719. end;
  720. { search and sets up the correct fields in the Instr record }
  721. { for the NON-constant identifier passed to the routine. }
  722. { if not found returns FALSE. }
  723. var
  724. sym : tsym;
  725. srsymtable : TSymtable;
  726. indexreg : tregister;
  727. plist : ppropaccesslistitem;
  728. size_set_from_absolute : boolean = false;
  729. { offset fixup (in bytes), coming from an absolute declaration with an index
  730. (e.g. var tralala: word absolute moo[5]; ) }
  731. absoffset: asizeint=0;
  732. harrdef: tarraydef;
  733. tmpprocinfo: tprocinfo;
  734. Begin
  735. SetupVar:=false;
  736. asmsearchsym(s,sym,srsymtable);
  737. if sym = nil then
  738. exit;
  739. if sym.typ=absolutevarsym then
  740. begin
  741. case tabsolutevarsym(sym).abstyp of
  742. tovar:
  743. begin
  744. { Only support simple loads }
  745. plist:=tabsolutevarsym(sym).ref.firstsym;
  746. if assigned(plist) and
  747. (plist^.sltype=sl_load) then
  748. begin
  749. setvarsize(tabstractvarsym(sym));
  750. size_set_from_absolute:=true;
  751. sym:=plist^.sym;
  752. { resolve the chain of array indexes (if there are any) }
  753. harrdef:=nil;
  754. while assigned(plist^.next) do
  755. begin
  756. plist:=plist^.next;
  757. if (plist^.sltype=sl_vec) and (tabstractvarsym(sym).vardef.typ=arraydef) then
  758. begin
  759. if harrdef=nil then
  760. harrdef:=tarraydef(tabstractvarsym(sym).vardef)
  761. else if harrdef.elementdef.typ=arraydef then
  762. harrdef:=tarraydef(harrdef.elementdef)
  763. else
  764. begin
  765. Message(asmr_e_unsupported_symbol_type);
  766. exit;
  767. end;
  768. if is_special_array(harrdef) then
  769. begin
  770. Message(asmr_e_unsupported_symbol_type);
  771. exit;
  772. end;
  773. if not is_packed_array(harrdef) then
  774. Inc(absoffset,asizeint(Int64(plist^.value-harrdef.lowrange))*harrdef.elesize)
  775. else if (Int64(plist^.value-harrdef.lowrange)*harrdef.elepackedbitsize mod 8)=0 then
  776. Inc(absoffset,asizeint(Int64(plist^.value-harrdef.lowrange)*harrdef.elepackedbitsize div 8))
  777. else
  778. Message(asmr_e_packed_element);
  779. end
  780. else
  781. begin
  782. Message(asmr_e_unsupported_symbol_type);
  783. exit;
  784. end;
  785. end;
  786. end
  787. else
  788. begin
  789. Message(asmr_e_unsupported_symbol_type);
  790. exit;
  791. end;
  792. end;
  793. toaddr:
  794. begin
  795. initref;
  796. opr.ref.offset:=tabsolutevarsym(sym).addroffset;
  797. setvarsize(tabstractvarsym(sym));
  798. size_set_from_absolute:=true;
  799. hasvar:=true;
  800. Result:=true;
  801. exit;
  802. end;
  803. else
  804. begin
  805. Message(asmr_e_unsupported_symbol_type);
  806. exit;
  807. end;
  808. end;
  809. end;
  810. case sym.typ of
  811. fieldvarsym :
  812. begin
  813. if not tabstractrecordsymtable(sym.owner).is_packed then
  814. setconst(absoffset+tfieldvarsym(sym).fieldoffset)
  815. else if tfieldvarsym(sym).fieldoffset mod 8 = 0 then
  816. setconst(absoffset+tfieldvarsym(sym).fieldoffset div 8)
  817. else
  818. Message(asmr_e_packed_element);
  819. if not size_set_from_absolute then
  820. setvarsize(tabstractvarsym(sym));
  821. hasvar:=true;
  822. SetupVar:=true;
  823. end;
  824. staticvarsym,
  825. localvarsym,
  826. paravarsym :
  827. begin
  828. { we always assume in asm statements that }
  829. { that the variable is valid. }
  830. tabstractvarsym(sym).varstate:=vs_readwritten;
  831. inc(tabstractvarsym(sym).refs);
  832. { variable can't be placed in a register }
  833. tabstractvarsym(sym).varregable:=vr_none;
  834. { and anything may happen with its address }
  835. tabstractvarsym(sym).addr_taken:=true;
  836. case sym.typ of
  837. staticvarsym :
  838. begin
  839. initref;
  840. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tstaticvarsym(sym).mangledname,AT_DATA);
  841. Inc(opr.ref.offset,absoffset);
  842. end;
  843. paravarsym,
  844. localvarsym :
  845. begin
  846. tmpprocinfo:=current_procinfo;
  847. while assigned(tmpprocinfo) do
  848. begin
  849. if (sym.owner=tmpprocinfo.procdef.localst) or
  850. (sym.owner=tmpprocinfo.procdef.parast) then
  851. begin
  852. tmpprocinfo.procdef.init_paraloc_info(calleeside);
  853. break;
  854. end;
  855. tmpprocinfo:=tmpprocinfo.parent;
  856. end;
  857. if opr.typ=OPR_REFERENCE then
  858. begin
  859. indexreg:=opr.ref.base;
  860. if opr.ref.index<>NR_NO then
  861. begin
  862. if indexreg=NR_NO then
  863. indexreg:=opr.ref.index
  864. else
  865. Message(asmr_e_multiple_index);
  866. end;
  867. end
  868. else
  869. indexreg:=NR_NO;
  870. opr.typ:=OPR_LOCAL;
  871. if assigned(current_procinfo.parent) and
  872. not(po_inline in current_procinfo.procdef.procoptions) and
  873. (sym.owner<>current_procinfo.procdef.localst) and
  874. (sym.owner<>current_procinfo.procdef.parast) and
  875. (current_procinfo.procdef.localst.symtablelevel>normal_function_level) and
  876. symtable_has_localvarsyms(current_procinfo.procdef.localst) then
  877. message1(asmr_e_local_para_unreachable,s);
  878. opr.localsym:=tabstractnormalvarsym(sym);
  879. opr.localsymofs:=absoffset;
  880. opr.localindexreg:=indexreg;
  881. opr.localscale:=0;
  882. opr.localgetoffset:=GetOffset;
  883. if paramanager.push_addr_param(tabstractvarsym(sym).varspez,tabstractvarsym(sym).vardef,current_procinfo.procdef.proccalloption) then
  884. SetSize(sizeof(pint),false);
  885. end;
  886. end;
  887. if not size_set_from_absolute then
  888. setvarsize(tabstractvarsym(sym));
  889. hasvar:=true;
  890. SetupVar:=true;
  891. Exit;
  892. end;
  893. constsym :
  894. begin
  895. if tconstsym(sym).consttyp=constord then
  896. begin
  897. setconst(tconstsym(sym).value.valueord.svalue);
  898. SetupVar:=true;
  899. Exit;
  900. end;
  901. end;
  902. typesym :
  903. begin
  904. if ttypesym(sym).typedef.typ in [recorddef,objectdef] then
  905. begin
  906. setconst(0);
  907. SetupVar:=TRUE;
  908. Exit;
  909. end;
  910. end;
  911. procsym :
  912. begin
  913. if Tprocsym(sym).ProcdefList.Count>1 then
  914. Message(asmr_w_calling_overload_func);
  915. case opr.typ of
  916. OPR_REFERENCE:
  917. begin
  918. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tprocdef(tprocsym(sym).ProcdefList[0]).mangledname,AT_FUNCTION);
  919. Inc(opr.ref.offset,absoffset);
  920. {$ifdef i8086}
  921. opr.ref_farproc_entry:=is_proc_far(tprocdef(tprocsym(sym).ProcdefList[0]))
  922. and not (po_interrupt in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions);
  923. {$endif i8086}
  924. end;
  925. OPR_NONE:
  926. begin
  927. opr.typ:=OPR_SYMBOL;
  928. opr.symbol:=current_asmdata.RefAsmSymbol(tprocdef(tprocsym(sym).ProcdefList[0]).mangledname,AT_FUNCTION);
  929. {$ifdef i8086}
  930. opr.sym_farproc_entry:=is_proc_far(tprocdef(tprocsym(sym).ProcdefList[0]))
  931. and not (po_interrupt in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions);
  932. {$endif i8086}
  933. opr.symofs:=absoffset;
  934. end;
  935. else
  936. Message(asmr_e_invalid_operand_type);
  937. end;
  938. hasproc:=true;
  939. hasvar:=true;
  940. SetupVar:=TRUE;
  941. Exit;
  942. end;
  943. {$ifdef i8086}
  944. labelsym :
  945. begin
  946. case opr.typ of
  947. OPR_REFERENCE:
  948. begin
  949. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tlabelsym(sym).mangledname,AT_FUNCTION);
  950. Inc(opr.ref.offset,absoffset);
  951. if opr.ref.segment=NR_NO then
  952. opr.ref.segment:=NR_CS;
  953. end;
  954. else
  955. begin
  956. Message(asmr_e_unsupported_symbol_type);
  957. exit;
  958. end;
  959. end;
  960. haslabelref:=true;
  961. hasvar:=true;
  962. SetupVar:=TRUE;
  963. Exit;
  964. end
  965. {$endif i8086}
  966. else
  967. begin
  968. Message(asmr_e_unsupported_symbol_type);
  969. exit;
  970. end;
  971. end;
  972. end;
  973. procedure TOperand.InitRef;
  974. {*********************************************************************}
  975. { Description: This routine first check if the opcode is of }
  976. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  977. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  978. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  979. { to point to the default segment. }
  980. {*********************************************************************}
  981. var
  982. l : aint;
  983. hsymofs : aint;
  984. hsymbol : tasmsymbol;
  985. reg : tregister;
  986. hsym_farprocentry: Boolean;
  987. Begin
  988. case opr.typ of
  989. OPR_REFERENCE :
  990. exit;
  991. OPR_CONSTANT :
  992. begin
  993. l:=opr.val;
  994. opr.typ:=OPR_REFERENCE;
  995. Fillchar(opr.ref,sizeof(treference),0);
  996. opr.Ref.Offset:=l;
  997. opr.varsize:=0;
  998. opr.constoffset:=0;
  999. opr.ref_farproc_entry:=false;
  1000. end;
  1001. OPR_NONE :
  1002. begin
  1003. opr.typ:=OPR_REFERENCE;
  1004. opr.varsize:=0;
  1005. opr.constoffset:=0;
  1006. opr.ref_farproc_entry:=false;
  1007. Fillchar(opr.ref,sizeof(treference),0);
  1008. end;
  1009. OPR_REGISTER :
  1010. begin
  1011. reg:=opr.reg;
  1012. opr.typ:=OPR_REFERENCE;
  1013. opr.varsize:=0;
  1014. opr.constoffset:=0;
  1015. opr.ref_farproc_entry:=false;
  1016. Fillchar(opr.ref,sizeof(treference),0);
  1017. opr.Ref.base:=reg;
  1018. end;
  1019. OPR_SYMBOL :
  1020. begin
  1021. hsymbol:=opr.symbol;
  1022. hsymofs:=opr.symofs;
  1023. hsym_farprocentry:=opr.sym_farproc_entry;
  1024. opr.typ:=OPR_REFERENCE;
  1025. opr.varsize:=0;
  1026. opr.constoffset:=0;
  1027. Fillchar(opr.ref,sizeof(treference),0);
  1028. opr.ref.symbol:=hsymbol;
  1029. opr.ref.offset:=hsymofs;
  1030. opr.ref_farproc_entry:=hsym_farprocentry;
  1031. end;
  1032. else
  1033. InitRefError;
  1034. end;
  1035. end;
  1036. procedure TOperand.InitRefConvertLocal;
  1037. var
  1038. localvarsize,localconstoffset: asizeint;
  1039. localsym:tabstractnormalvarsym;
  1040. localsymofs:aint;
  1041. localindexreg:tregister;
  1042. localscale:byte;
  1043. begin
  1044. if opr.typ=OPR_LOCAL then
  1045. begin
  1046. if AsmRegisterPara(opr.localsym) and
  1047. not opr.localgetoffset then
  1048. begin
  1049. localvarsize:=opr.localvarsize;
  1050. localconstoffset:=opr.localconstoffset;
  1051. localsym:=opr.localsym;
  1052. localsymofs:=opr.localsymofs;
  1053. localindexreg:=opr.localindexreg;
  1054. localscale:=opr.localscale;;
  1055. opr.typ:=OPR_REFERENCE;
  1056. hasvar:=false;
  1057. Fillchar(opr.ref,sizeof(treference),0);
  1058. opr.varsize:=localvarsize;
  1059. opr.constoffset:=localconstoffset;
  1060. opr.ref_farproc_entry:=false;
  1061. opr.ref.base:=tparavarsym(localsym).paraloc[calleeside].Location^.register;
  1062. opr.ref.offset:=localsymofs;
  1063. opr.ref.index:=localindexreg;
  1064. opr.ref.scalefactor:=localscale;
  1065. end
  1066. else
  1067. InitRefError;
  1068. end
  1069. else
  1070. InitRef;
  1071. end;
  1072. procedure TOperand.InitRefError;
  1073. begin
  1074. Message(asmr_e_invalid_operand_type);
  1075. { Recover }
  1076. opr.typ:=OPR_REFERENCE;
  1077. opr.varsize:=0;
  1078. opr.constoffset:=0;
  1079. opr.ref_farproc_entry:=false;
  1080. Fillchar(opr.ref,sizeof(treference),0);
  1081. end;
  1082. Function TOperand.CheckOperand: boolean;
  1083. {*********************************************************************}
  1084. { Description: This routine checks if the operand is of }
  1085. { valid, and returns false if it isn't. Does nothing by default. }
  1086. {*********************************************************************}
  1087. begin
  1088. result:=true;
  1089. end;
  1090. {****************************************************************************
  1091. TInstruction
  1092. ****************************************************************************}
  1093. constructor TInstruction.create(optype : tcoperand);
  1094. var
  1095. i : longint;
  1096. Begin
  1097. { these field are set to 0 anyways by the constructor helper (FK)
  1098. Opcode:=A_NONE;
  1099. Condition:=C_NONE;
  1100. Ops:=0;
  1101. }
  1102. filepos:=current_filepos;
  1103. for i:=1 to max_operands do
  1104. Operands[i]:=optype.create;
  1105. Labeled:=false;
  1106. end;
  1107. destructor TInstruction.destroy;
  1108. var
  1109. i : longint;
  1110. Begin
  1111. for i:=1 to max_operands do
  1112. Operands[i].free;
  1113. end;
  1114. function TInstruction.ConcatInstruction(p:TAsmList) : tai;
  1115. var
  1116. ai : taicpu;
  1117. i : longint;
  1118. begin
  1119. for i:=1 to Ops do
  1120. operands[i].CheckOperand;
  1121. ai:=taicpu.op_none(opcode);
  1122. ai.fileinfo:=filepos;
  1123. ai.Ops:=Ops;
  1124. ai.Allocate_oper(Ops);
  1125. for i:=1 to Ops do
  1126. with operands[i].opr do
  1127. begin
  1128. case typ of
  1129. OPR_CONSTANT :
  1130. ai.loadconst(i-1,val);
  1131. OPR_REGISTER:
  1132. ai.loadreg(i-1,reg);
  1133. OPR_SYMBOL:
  1134. ai.loadsymbol(i-1,symbol,symofs);
  1135. OPR_LOCAL :
  1136. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  1137. localscale,localgetoffset,localforceref);
  1138. OPR_REFERENCE:
  1139. ai.loadref(i-1,ref);
  1140. {$ifdef m68k}
  1141. OPR_REGSET:
  1142. ai.loadregset(i-1,regsetdata,regsetaddr,regsetfpu);
  1143. OPR_REGPAIR:
  1144. ai.loadregpair(i-1,reghi,reglo);
  1145. {$endif}
  1146. {$ifdef ARM}
  1147. OPR_REGSET:
  1148. ai.loadregset(i-1,regtype,subreg,regset,usermode);
  1149. OPR_MODEFLAGS:
  1150. ai.loadmodeflags(i-1,flags);
  1151. OPR_SPECIALREG:
  1152. ai.loadspecialreg(i-1,specialreg,specialregflags);
  1153. {$endif ARM}
  1154. {$if defined(arm) or defined(aarch64)}
  1155. OPR_SHIFTEROP:
  1156. ai.loadshifterop(i-1,shifterop);
  1157. OPR_COND:
  1158. ai.loadconditioncode(i-1,cc);
  1159. {$endif arm or aarch64}
  1160. { ignore wrong operand }
  1161. OPR_NONE:
  1162. ;
  1163. else
  1164. internalerror(200501051);
  1165. end;
  1166. end;
  1167. ai.SetCondition(condition);
  1168. { Concat the opcode or give an error }
  1169. if assigned(ai) then
  1170. p.concat(ai)
  1171. else
  1172. Message(asmr_e_invalid_opcode_and_operand);
  1173. result:=ai;
  1174. end;
  1175. {****************************************************************************
  1176. Symbol table helper routines
  1177. ****************************************************************************}
  1178. procedure AsmSearchSym(const s:string;out srsym:tsym;out srsymtable:TSymtable);
  1179. var
  1180. i : integer;
  1181. begin
  1182. i:=pos('.',s);
  1183. { allow unit.identifier }
  1184. if i>0 then
  1185. begin
  1186. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1187. if assigned(srsym) then
  1188. begin
  1189. if (srsym.typ=unitsym) and
  1190. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  1191. srsym.owner.iscurrentunit then
  1192. searchsym_in_module(tunitsym(srsym).module,Copy(s,i+1,255),srsym,srsymtable)
  1193. else
  1194. begin
  1195. srsym:=nil;
  1196. srsymtable:=nil;
  1197. end;
  1198. end;
  1199. end
  1200. else
  1201. searchsym(s,srsym,srsymtable);
  1202. { in asm routines, the function result variable, that matches the function
  1203. name should be avoided, because:
  1204. 1) there's already a @Result directive (even in TP7) that can be used, if
  1205. you want to access the function result
  1206. 2) there's no other way to disambiguate between the function result variable
  1207. and the function's address (using asm syntax only)
  1208. This fixes code, such as:
  1209. function test1: word;
  1210. begin
  1211. asm
  1212. mov ax, offset test1
  1213. end;
  1214. end;
  1215. and makes it work in a consistent manner as this code:
  1216. procedure test2;
  1217. begin
  1218. asm
  1219. mov ax, offset test2
  1220. end;
  1221. end; }
  1222. if assigned(srsym) and
  1223. assigned(srsymtable) and
  1224. (srsym.typ=absolutevarsym) and
  1225. (vo_is_funcret in tabsolutevarsym(srsym).varoptions) and
  1226. (srsymtable.symtabletype=localsymtable) and
  1227. assigned(srsymtable.defowner) and
  1228. (srsymtable.defowner.typ=procdef) and
  1229. (tprocdef(srsymtable.defowner).procsym.name=tabsolutevarsym(srsym).Name) then
  1230. begin
  1231. srsym:=tprocdef(srsymtable.defowner).procsym;
  1232. srsymtable:=srsym.Owner;
  1233. end;
  1234. end;
  1235. Function SearchType(const hs:string;out size:tcgint): Boolean;
  1236. var
  1237. srsym : tsym;
  1238. srsymtable : TSymtable;
  1239. begin
  1240. result:=false;
  1241. size:=0;
  1242. asmsearchsym(hs,srsym,srsymtable);
  1243. if assigned(srsym) and
  1244. (srsym.typ=typesym) then
  1245. begin
  1246. size:=ttypesym(srsym).typedef.size;
  1247. result:=true;
  1248. end;
  1249. end;
  1250. Function SearchRecordType(const s:string): boolean;
  1251. var
  1252. srsym : tsym;
  1253. srsymtable : TSymtable;
  1254. Begin
  1255. SearchRecordType:=false;
  1256. { Check the constants in symtable }
  1257. asmsearchsym(s,srsym,srsymtable);
  1258. if srsym <> nil then
  1259. Begin
  1260. case srsym.typ of
  1261. typesym :
  1262. begin
  1263. if ttypesym(srsym).typedef.typ in [recorddef,objectdef] then
  1264. begin
  1265. SearchRecordType:=true;
  1266. exit;
  1267. end;
  1268. end;
  1269. fieldvarsym :
  1270. begin
  1271. if (tfieldvarsym(srsym).vardef.typ in [recorddef,objectdef]) then
  1272. begin
  1273. SearchRecordType:=true;
  1274. exit;
  1275. end;
  1276. end;
  1277. end;
  1278. end;
  1279. end;
  1280. Function SearchIConstant(const s:string; var l:tcgint): boolean;
  1281. {**********************************************************************}
  1282. { Description: Searches for a CONSTANT of name s in either the local }
  1283. { symbol list, then in the global symbol list, and returns the value }
  1284. { of that constant in l. Returns TRUE if successfull, if not found, }
  1285. { or if the constant is not of correct type, then returns FALSE }
  1286. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1287. { respectively. }
  1288. {**********************************************************************}
  1289. var
  1290. srsym : tsym;
  1291. srsymtable : TSymtable;
  1292. Begin
  1293. SearchIConstant:=false;
  1294. { check for TRUE or FALSE reserved words first }
  1295. if s = 'TRUE' then
  1296. Begin
  1297. SearchIConstant:=TRUE;
  1298. l:=1;
  1299. exit;
  1300. end;
  1301. if s = 'FALSE' then
  1302. Begin
  1303. SearchIConstant:=TRUE;
  1304. l:=0;
  1305. exit;
  1306. end;
  1307. { Check the constants in symtable }
  1308. asmsearchsym(s,srsym,srsymtable);
  1309. if srsym <> nil then
  1310. Begin
  1311. case srsym.typ of
  1312. constsym :
  1313. begin
  1314. if tconstsym(srsym).consttyp=constord then
  1315. Begin
  1316. l:=tconstsym(srsym).value.valueord.svalue;
  1317. SearchIConstant:=TRUE;
  1318. exit;
  1319. end;
  1320. end;
  1321. enumsym:
  1322. Begin
  1323. l:=tenumsym(srsym).value;
  1324. SearchIConstant:=TRUE;
  1325. exit;
  1326. end;
  1327. end;
  1328. end;
  1329. end;
  1330. function AsmRegisterPara(sym: tabstractnormalvarsym): boolean;
  1331. begin
  1332. result:=
  1333. (po_assembler in current_procinfo.procdef.procoptions) and
  1334. (sym.typ=paravarsym) and
  1335. (tparavarsym(sym).paraloc[calleeside].Location^.Loc=LOC_REGISTER);
  1336. end;
  1337. Function GetRecordOffsetSize(s:string;out Offset: tcgint;out Size:tcgint; out mangledname: string; needvmtofs: boolean; out hastypecast: boolean):boolean;
  1338. { search and returns the offset and size of records/objects of the base }
  1339. { with field name setup in field. }
  1340. { returns FALSE if not found. }
  1341. { used when base is a variable or a typed constant name. }
  1342. var
  1343. st : TSymtable;
  1344. harrdef : tarraydef;
  1345. sym : tsym;
  1346. srsymtable : TSymtable;
  1347. i : longint;
  1348. base : string;
  1349. procdef: tprocdef;
  1350. Begin
  1351. GetRecordOffsetSize:=FALSE;
  1352. Offset:=0;
  1353. Size:=0;
  1354. mangledname:='';
  1355. hastypecast:=false;
  1356. i:=pos('.',s);
  1357. if i=0 then
  1358. i:=255;
  1359. base:=Copy(s,1,i-1);
  1360. delete(s,1,i);
  1361. if base='SELF' then
  1362. st:=current_structdef.symtable
  1363. else
  1364. begin
  1365. asmsearchsym(base,sym,srsymtable);
  1366. st:=nil;
  1367. { we can start with a var,type,typedconst }
  1368. if assigned(sym) then
  1369. case sym.typ of
  1370. staticvarsym,
  1371. localvarsym,
  1372. paravarsym :
  1373. st:=Tabstractvarsym(sym).vardef.GetSymtable(gs_record);
  1374. typesym :
  1375. st:=Ttypesym(sym).typedef.GetSymtable(gs_record);
  1376. end
  1377. else
  1378. s:='';
  1379. end;
  1380. { now walk all recordsymtables }
  1381. while assigned(st) and (s<>'') do
  1382. begin
  1383. { load next field in base }
  1384. i:=pos('.',s);
  1385. if i=0 then
  1386. i:=255;
  1387. base:=Copy(s,1,i-1);
  1388. delete(s,1,i);
  1389. sym:=search_struct_member(tabstractrecorddef(st.defowner),base);
  1390. if not assigned(sym) then
  1391. begin
  1392. GetRecordOffsetSize:=false;
  1393. exit;
  1394. end;
  1395. st:=nil;
  1396. case sym.typ of
  1397. fieldvarsym :
  1398. with Tfieldvarsym(sym) do
  1399. begin
  1400. if not tabstractrecordsymtable(sym.owner).is_packed then
  1401. inc(Offset,fieldoffset)
  1402. else if tfieldvarsym(sym).fieldoffset mod 8 = 0 then
  1403. inc(Offset,fieldoffset div 8)
  1404. else
  1405. Message(asmr_e_packed_element);
  1406. size:=getsize;
  1407. case vardef.typ of
  1408. arraydef :
  1409. begin
  1410. { for arrays try to get the element size, take care of
  1411. multiple indexes }
  1412. harrdef:=tarraydef(vardef);
  1413. while assigned(harrdef.elementdef) and
  1414. (harrdef.elementdef.typ=arraydef) do
  1415. harrdef:=tarraydef(harrdef.elementdef);
  1416. if not is_packed_array(harrdef) then
  1417. size:=harrdef.elesize
  1418. else
  1419. begin
  1420. if (harrdef.elepackedbitsize mod 8) <> 0 then
  1421. Message(asmr_e_packed_element);
  1422. size := (harrdef.elepackedbitsize + 7) div 8;
  1423. end;
  1424. end;
  1425. recorddef :
  1426. st:=trecorddef(vardef).symtable;
  1427. objectdef :
  1428. st:=tobjectdef(vardef).symtable;
  1429. end;
  1430. end;
  1431. procsym:
  1432. begin
  1433. st:=nil;
  1434. if Tprocsym(sym).ProcdefList.Count>1 then
  1435. Message(asmr_w_calling_overload_func);
  1436. procdef:=tprocdef(tprocsym(sym).ProcdefList[0]);
  1437. if (not needvmtofs) then
  1438. begin
  1439. mangledname:=procdef.mangledname;
  1440. end
  1441. else
  1442. begin
  1443. { can only get the vmtoffset of virtual methods }
  1444. if not(po_virtualmethod in procdef.procoptions) or
  1445. is_objectpascal_helper(procdef.struct) then
  1446. Message1(asmr_e_no_vmtoffset_possible,FullTypeName(procdef,nil))
  1447. else
  1448. begin
  1449. { size = sizeof(target_system_pointer) }
  1450. size:=sizeof(pint);
  1451. offset:=tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber)
  1452. end;
  1453. end;
  1454. { if something comes after the procsym, it's invalid assembler syntax }
  1455. GetRecordOffsetSize:=(s='');
  1456. exit;
  1457. end;
  1458. end;
  1459. end;
  1460. { Support Field.Type as typecasting }
  1461. if (st=nil) and (s<>'') then
  1462. begin
  1463. asmsearchsym(s,sym,srsymtable);
  1464. if assigned(sym) and (sym.typ=typesym) then
  1465. begin
  1466. size:=ttypesym(sym).typedef.size;
  1467. s:='';
  1468. hastypecast:=true;
  1469. end;
  1470. end;
  1471. GetRecordOffsetSize:=(s='');
  1472. end;
  1473. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1474. var
  1475. sym : tsym;
  1476. srsymtable : TSymtable;
  1477. hs : string;
  1478. Begin
  1479. hl:=nil;
  1480. SearchLabel:=false;
  1481. { Check for pascal labels, which are case insensetive }
  1482. hs:=upper(s);
  1483. asmsearchsym(hs,sym,srsymtable);
  1484. if sym=nil then
  1485. exit;
  1486. case sym.typ of
  1487. labelsym :
  1488. begin
  1489. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  1490. begin
  1491. Tlabelsym(sym).nonlocal:=true;
  1492. if emit then
  1493. exclude(current_procinfo.procdef.procoptions,po_inline);
  1494. end;
  1495. if not(assigned(tlabelsym(sym).asmblocklabel)) then
  1496. if Tlabelsym(sym).nonlocal then
  1497. current_asmdata.getglobaljumplabel(tlabelsym(sym).asmblocklabel)
  1498. else
  1499. current_asmdata.getjumplabel(tlabelsym(sym).asmblocklabel);
  1500. hl:=tlabelsym(sym).asmblocklabel;
  1501. if emit then
  1502. begin
  1503. if tlabelsym(sym).defined then
  1504. Message(sym_e_label_already_defined);
  1505. tlabelsym(sym).defined:=true
  1506. end
  1507. else
  1508. tlabelsym(sym).used:=true;
  1509. SearchLabel:=true;
  1510. end;
  1511. end;
  1512. end;
  1513. {*************************************************************************}
  1514. { Instruction Generation Utilities }
  1515. {*************************************************************************}
  1516. Procedure ConcatString(p : TAsmList;s:string);
  1517. {*********************************************************************}
  1518. { PROCEDURE ConcatString(s:string); }
  1519. { Description: This routine adds the character chain pointed to in }
  1520. { s to the instruction linked list. }
  1521. {*********************************************************************}
  1522. Begin
  1523. p.concat(Tai_string.Create(s));
  1524. end;
  1525. Procedure ConcatConstant(p: TAsmList; value: tcgint; constsize:byte);
  1526. {*********************************************************************}
  1527. { PROCEDURE ConcatConstant(value: aint; maxvalue: aint); }
  1528. { Description: This routine adds the value constant to the current }
  1529. { instruction linked list. }
  1530. { maxvalue -> indicates the size of the data to initialize: }
  1531. { $ff -> create a byte node. }
  1532. { $ffff -> create a word node. }
  1533. { $ffffffff -> create a dword node. }
  1534. {*********************************************************************}
  1535. var
  1536. rangelo,rangehi : int64;
  1537. Begin
  1538. case constsize of
  1539. 1 :
  1540. begin
  1541. p.concat(Tai_const.Create_8bit(byte(value)));
  1542. rangelo:=low(shortint);
  1543. rangehi:=high(byte);
  1544. end;
  1545. 2 :
  1546. begin
  1547. p.concat(Tai_const.Create_16bit(word(value)));
  1548. rangelo:=low(smallint);
  1549. rangehi:=high(word);
  1550. end;
  1551. 4 :
  1552. begin
  1553. p.concat(Tai_const.Create_32bit(longint(value)));
  1554. rangelo:=low(longint);
  1555. rangehi:=high(cardinal);
  1556. end;
  1557. 8 :
  1558. begin
  1559. p.concat(Tai_const.Create_64bit(int64(value)));
  1560. rangelo:=0;
  1561. rangehi:=0;
  1562. end;
  1563. else
  1564. internalerror(200405011);
  1565. end;
  1566. { check for out of bounds }
  1567. if (rangelo<>0) and
  1568. ((value>rangehi) or (value<rangelo)) then
  1569. Message(asmr_e_constant_out_of_bounds);
  1570. end;
  1571. Procedure ConcatConstSymbol(p : TAsmList;const sym:string;symtyp:tasmsymtype;l:tcgint;constsize:byte;isofs:boolean);
  1572. begin
  1573. {$ifdef i8086}
  1574. { 'DW xx' as well as 'DW OFFSET xx' are just near pointers }
  1575. if constsize=2 then
  1576. p.concat(Tai_const.Createname_near(sym,l))
  1577. else if constsize=4 then
  1578. begin
  1579. if isofs then
  1580. begin
  1581. { 'DD OFFSET xx' is a 32-bit offset; since we don't produce 32-bit
  1582. relocations yet, just do a 16-bit one and set the high word to 0 }
  1583. p.concat(Tai_const.Createname_near(sym,l));
  1584. p.concat(Tai_const.Create_16bit(0));
  1585. end
  1586. else
  1587. { 'DD xx' is a far pointer }
  1588. p.concat(Tai_const.Createname_far(sym,l));
  1589. end
  1590. else
  1591. internalerror(2018020701);
  1592. {$else i8086}
  1593. p.concat(Tai_const.Createname(sym,l));
  1594. {$endif i8086}
  1595. end;
  1596. Procedure ConcatRealConstant(p : TAsmList;value: bestreal; real_typ : tfloattype);
  1597. {***********************************************************************}
  1598. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1599. { Description: This routine adds the value constant to the current }
  1600. { instruction linked list. }
  1601. { real_typ -> indicates the type of the real data to initialize: }
  1602. { s32real -> create a single node. }
  1603. { s64real -> create a double node. }
  1604. { s80real -> create an extended node. }
  1605. { s64bit -> create a comp node. }
  1606. { f32bit -> create a fixed node. (not used normally) }
  1607. {***********************************************************************}
  1608. Begin
  1609. case real_typ of
  1610. s32real : p.concat(tai_realconst.create_s32real(value));
  1611. s64real :
  1612. {$ifdef ARM}
  1613. if is_double_hilo_swapped then
  1614. p.concat(tai_realconst.create_s64real_hiloswapped(value))
  1615. else
  1616. {$endif ARM}
  1617. p.concat(tai_realconst.create_s64real(value));
  1618. s80real : p.concat(tai_realconst.create_s80real(value,s80floattype.size));
  1619. sc80real : p.concat(tai_realconst.create_s80real(value,sc80floattype.size));
  1620. s64comp : p.concat(tai_realconst.create_s64compreal(trunc(value)));
  1621. else
  1622. internalerror(2014050608);
  1623. end;
  1624. end;
  1625. Procedure ConcatLabel(p: TAsmList;var l : tasmlabel);
  1626. {*********************************************************************}
  1627. { PROCEDURE ConcatLabel }
  1628. { Description: This routine either emits a label or a labeled }
  1629. { instruction to the linked list of instructions. }
  1630. {*********************************************************************}
  1631. begin
  1632. p.concat(Tai_label.Create(l));
  1633. end;
  1634. procedure ConcatAlign(p:TAsmList;l:tcgint);
  1635. {*********************************************************************}
  1636. { PROCEDURE ConcatPublic }
  1637. { Description: This routine emits an global definition to the }
  1638. { linked list of instructions.(used by AT&T styled asm) }
  1639. {*********************************************************************}
  1640. begin
  1641. p.concat(Tai_align.Create(l));
  1642. end;
  1643. procedure ConcatPublic(p:TAsmList;const s : string);
  1644. {*********************************************************************}
  1645. { PROCEDURE ConcatPublic }
  1646. { Description: This routine emits an global definition to the }
  1647. { linked list of instructions.(used by AT&T styled asm) }
  1648. {*********************************************************************}
  1649. begin
  1650. p.concat(Tai_symbol.Createname_global(s,AT_LABEL,0,voidcodepointertype));
  1651. end;
  1652. procedure ConcatLocal(p:TAsmList;const s : string);
  1653. {*********************************************************************}
  1654. { PROCEDURE ConcatLocal }
  1655. { Description: This routine emits an local definition to the }
  1656. { linked list of instructions. }
  1657. {*********************************************************************}
  1658. begin
  1659. p.concat(Tai_symbol.Createname(s,AT_LABEL,0,voidcodepointertype));
  1660. end;
  1661. end.