rautils.pas 47 KB

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