rautils.pas 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  4. This unit implements some support routines for assembler parsing
  5. independent of the processor
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. **********************************************************************}
  18. Unit RAUtils;
  19. {$i fpcdefs.inc}
  20. Interface
  21. Uses
  22. cutils,cclasses,
  23. globtype,aasmbase,aasmtai,cpubase,cpuinfo,cgbase,
  24. symconst,symbase,symtype,symdef,symsym;
  25. Const
  26. RPNMax = 10; { I think you only need 4, but just to be safe }
  27. OpMax = 25;
  28. {---------------------------------------------------------------------
  29. Local Label Management
  30. ---------------------------------------------------------------------}
  31. Type
  32. { Each local label has this structure associated with it }
  33. TLocalLabel = class(TNamedIndexItem)
  34. Emitted : boolean;
  35. constructor Create(const n:string);
  36. function Gettasmlabel:tasmlabel;
  37. private
  38. lab : tasmlabel;
  39. end;
  40. TLocalLabelList = class(TDictionary)
  41. procedure CheckEmitted;
  42. end;
  43. var
  44. LocalLabelList : TLocalLabelList;
  45. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  46. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  47. {---------------------------------------------------------------------
  48. Instruction management
  49. ---------------------------------------------------------------------}
  50. type
  51. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,OPR_LOCAL,
  52. OPR_REFERENCE,OPR_REGISTER,OPR_REGLIST,OPR_COND,OPR_REGSET);
  53. TOprRec = record
  54. case typ:TOprType of
  55. OPR_NONE : ();
  56. OPR_CONSTANT : (val:aint);
  57. OPR_SYMBOL : (symbol:tasmsymbol;symofs:aint);
  58. OPR_REFERENCE : (ref:treference);
  59. OPR_LOCAL : (localsym:tvarsym;localsymofs:aint;localindexreg:tregister;localscale:byte;localgetoffset:boolean);
  60. OPR_REGISTER : (reg:tregister);
  61. {$ifdef m68k}
  62. OPR_REGLIST : (regset : tcpuregisterset);
  63. {$endif m68k}
  64. {$ifdef powerpc}
  65. OPR_COND : (cond : tasmcond);
  66. {$endif powerpc}
  67. {$ifdef arm}
  68. OPR_REGSET : (regset : tcpuregisterset);
  69. {$endif arm}
  70. end;
  71. TOperand = class
  72. hastype, { if the operand has typecasted variable }
  73. hasvar : boolean; { if the operand is loaded with a variable }
  74. size : TCGSize;
  75. opr : TOprRec;
  76. constructor create;virtual;
  77. destructor destroy;override;
  78. Procedure SetSize(_size:longint;force:boolean);virtual;
  79. Procedure SetCorrectSize(opcode:tasmop);virtual;
  80. Function SetupResult:boolean;virtual;
  81. Function SetupSelf:boolean;
  82. Function SetupOldEBP:boolean;
  83. Function SetupVar(const s:string;GetOffset : boolean): Boolean;
  84. Function SetupDirectVar(const hs:string): Boolean;
  85. Procedure InitRef;
  86. end;
  87. TCOperand = class of TOperand;
  88. TInstruction = class
  89. opcode : tasmop;
  90. condition : tasmcond;
  91. ops : byte;
  92. labeled : boolean;
  93. operands : array[1..max_operands] of toperand;
  94. constructor create(optype : tcoperand);virtual;
  95. destructor destroy;override;
  96. Procedure BuildOpcode;virtual;abstract;
  97. { converts the instruction to an instruction how it's used by the assembler writer
  98. and concats it to the passed list, the newly created item is returned }
  99. function ConcatInstruction(p:TAAsmoutput) : tai;virtual;
  100. Procedure Swapoperands;
  101. end;
  102. tstr2opentry = class(Tnamedindexitem)
  103. op: TAsmOp;
  104. end;
  105. {---------------------------------------------------------------------}
  106. { Expression parser types }
  107. {---------------------------------------------------------------------}
  108. TExprOperator = record
  109. ch: char; { operator }
  110. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  111. end;
  112. {**********************************************************************}
  113. { The following operators are supported: }
  114. { '+' : addition }
  115. { '-' : subtraction }
  116. { '*' : multiplication }
  117. { '/' : modulo division }
  118. { '^' : exclusive or }
  119. { '<' : shift left }
  120. { '>' : shift right }
  121. { '&' : bitwise and }
  122. { '|' : bitwise or }
  123. { '~' : bitwise complement }
  124. { '%' : modulo division }
  125. { nnn: longint numbers }
  126. { ( and ) parenthesis }
  127. {**********************************************************************}
  128. TExprParse = class
  129. public
  130. Constructor create;
  131. Destructor Destroy;override;
  132. Function Evaluate(Expr: String): aint;
  133. Function Priority(_Operator: Char): aint;
  134. private
  135. RPNStack : Array[1..RPNMax] of aint; { Stack For RPN calculator }
  136. RPNTop : aint;
  137. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  138. OpTop : aint;
  139. Procedure RPNPush(Num: aint);
  140. Function RPNPop: aint;
  141. Procedure RPNCalc(const token: String; prefix: boolean);
  142. Procedure OpPush(_Operator: char; prefix: boolean);
  143. { In reality returns TExprOperaotr }
  144. Procedure OpPop(var _Operator:TExprOperator);
  145. end;
  146. { Evaluate an expression string to a aint }
  147. Function CalculateExpression(const expression: string): aint;
  148. {---------------------------------------------------------------------}
  149. { String routines }
  150. {---------------------------------------------------------------------}
  151. Function ValDecimal(const S:String):aint;
  152. Function ValOctal(const S:String):aint;
  153. Function ValBinary(const S:String):aint;
  154. Function ValHexaDecimal(const S:String):aint;
  155. Function PadZero(Var s: String; n: byte): Boolean;
  156. Function EscapeToPascal(const s:string): string;
  157. {---------------------------------------------------------------------
  158. Symbol helper routines
  159. ---------------------------------------------------------------------}
  160. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  161. Function GetRecordOffsetSize(s:string;Var Offset: aint;var Size:aint):boolean;
  162. Function SearchType(const hs:string;var size:aint): Boolean;
  163. Function SearchRecordType(const s:string): boolean;
  164. Function SearchIConstant(const s:string; var l:aint): boolean;
  165. {---------------------------------------------------------------------
  166. Instruction generation routines
  167. ---------------------------------------------------------------------}
  168. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  169. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  170. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  171. Procedure ConcatConstant(p : TAAsmoutput;value: aint; constsize:byte);
  172. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:aint);
  173. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  174. Procedure ConcatString(p : TAAsmoutput;s:string);
  175. procedure ConcatAlign(p:TAAsmoutput;l:aint);
  176. Procedure ConcatPublic(p:TAAsmoutput;const s : string);
  177. Procedure ConcatLocal(p:TAAsmoutput;const s : string);
  178. Procedure ConcatGlobalBss(const s : string;size : aint);
  179. Procedure ConcatLocalBss(const s : string;size : aint);
  180. Implementation
  181. uses
  182. {$ifdef delphi}
  183. sysutils,
  184. {$else}
  185. strings,
  186. {$endif}
  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. '+', '-' :
  341. Priority:=1;
  342. '*', '/','%','<','>' :
  343. Priority:=2;
  344. '|','&','^','~' :
  345. Priority:=0;
  346. else
  347. Message(asmr_e_expr_illegal);
  348. end;
  349. end;
  350. Function TExprParse.Evaluate(Expr : String):aint;
  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 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): aint;
  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 : aint;
  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(ValOctal(temp));
  490. end;
  491. 'x':
  492. Begin
  493. temp:=s[i+1];
  494. temp:=temp+s[i+2];
  495. inc(i,2);
  496. c:=chr(ValHexaDecimal(temp));
  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 ValDecimal(const S:String):aint;
  514. { Converts a decimal string to aint }
  515. var
  516. vs : aint;
  517. c : longint;
  518. Begin
  519. vs:=0;
  520. for c:=1 to length(s) do
  521. begin
  522. vs:=vs*10;
  523. if s[c] in ['0'..'9'] then
  524. inc(vs,ord(s[c])-ord('0'))
  525. else
  526. begin
  527. Message1(asmr_e_error_converting_decimal,s);
  528. ValDecimal:=0;
  529. exit;
  530. end;
  531. end;
  532. ValDecimal:=vs;
  533. end;
  534. Function ValOctal(const S:String):aint;
  535. { Converts an octal string to aint }
  536. var
  537. vs : aint;
  538. c : longint;
  539. Begin
  540. vs:=0;
  541. for c:=1 to length(s) do
  542. begin
  543. vs:=vs shl 3;
  544. if s[c] in ['0'..'7'] then
  545. inc(vs,ord(s[c])-ord('0'))
  546. else
  547. begin
  548. Message1(asmr_e_error_converting_octal,s);
  549. ValOctal:=0;
  550. exit;
  551. end;
  552. end;
  553. ValOctal:=vs;
  554. end;
  555. Function ValBinary(const S:String):aint;
  556. { Converts a binary string to aint }
  557. var
  558. vs : aint;
  559. c : longint;
  560. Begin
  561. vs:=0;
  562. for c:=1 to length(s) do
  563. begin
  564. vs:=vs shl 1;
  565. if s[c] in ['0'..'1'] then
  566. inc(vs,ord(s[c])-ord('0'))
  567. else
  568. begin
  569. Message1(asmr_e_error_converting_binary,s);
  570. ValBinary:=0;
  571. exit;
  572. end;
  573. end;
  574. ValBinary:=vs;
  575. end;
  576. Function ValHexadecimal(const S:String):aint;
  577. { Converts a binary string to aint }
  578. var
  579. vs : aint;
  580. c : longint;
  581. Begin
  582. vs:=0;
  583. for c:=1 to length(s) do
  584. begin
  585. vs:=vs shl 4;
  586. case s[c] of
  587. '0'..'9' :
  588. inc(vs,ord(s[c])-ord('0'));
  589. 'A'..'F' :
  590. inc(vs,ord(s[c])-ord('A')+10);
  591. 'a'..'f' :
  592. inc(vs,ord(s[c])-ord('a')+10);
  593. else
  594. begin
  595. Message1(asmr_e_error_converting_hexadecimal,s);
  596. ValHexadecimal:=0;
  597. exit;
  598. end;
  599. end;
  600. end;
  601. ValHexadecimal:=vs;
  602. end;
  603. Function PadZero(Var s: String; n: byte): Boolean;
  604. Begin
  605. PadZero:=TRUE;
  606. { Do some error checking first }
  607. if Length(s) = n then
  608. exit
  609. else
  610. if Length(s) > n then
  611. Begin
  612. PadZero:=FALSE;
  613. delete(s,n+1,length(s));
  614. exit;
  615. end
  616. else
  617. PadZero:=TRUE;
  618. { Fill it up with the specified character }
  619. fillchar(s[length(s)+1],n-1,#0);
  620. s[0]:=chr(n);
  621. end;
  622. {****************************************************************************
  623. TOperand
  624. ****************************************************************************}
  625. constructor TOperand.Create;
  626. begin
  627. size:=OS_NO;
  628. hastype:=false;
  629. hasvar:=false;
  630. FillChar(Opr,sizeof(Opr),0);
  631. end;
  632. destructor TOperand.destroy;
  633. begin
  634. end;
  635. Procedure TOperand.SetSize(_size:longint;force:boolean);
  636. begin
  637. if force or
  638. ((size = OS_NO) and (_size<=extended_size)) then
  639. Begin
  640. case _size of
  641. 1 : size:=OS_8;
  642. 2 : size:=OS_16{ could be S_IS};
  643. 4 : size:=OS_32{ could be S_IL or S_FS};
  644. 8 : size:=OS_64{ could be S_D or S_FL};
  645. else
  646. begin
  647. { extended_size can also be 8, resulting in a
  648. duplicate label }
  649. if _size=extended_size then
  650. size:=OS_F80;
  651. end;
  652. end;
  653. end;
  654. end;
  655. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  656. begin
  657. end;
  658. function TOperand.SetupResult:boolean;
  659. begin
  660. SetupResult:=false;
  661. { replace by correct offset. }
  662. with current_procinfo.procdef do
  663. if (not is_void(rettype.def)) then
  664. begin
  665. if (m_tp7 in aktmodeswitches) and
  666. (not paramanager.ret_in_param(rettype.def,proccalloption)) then
  667. begin
  668. message(asmr_e_cannot_use_RESULT_here);
  669. exit;
  670. end;
  671. SetupResult:=setupvar('result',false)
  672. end
  673. else
  674. message(asmr_e_void_function);
  675. end;
  676. Function TOperand.SetupSelf:boolean;
  677. Begin
  678. SetupSelf:=false;
  679. if assigned(current_procinfo.procdef._class) then
  680. SetupSelf:=setupvar('self',false)
  681. else
  682. Message(asmr_e_cannot_use_SELF_outside_a_method);
  683. end;
  684. Function TOperand.SetupOldEBP:boolean;
  685. Begin
  686. SetupOldEBP:=false;
  687. if current_procinfo.procdef.parast.symtablelevel>normal_function_level then
  688. SetupOldEBP:=setupvar('parentframe',false)
  689. else
  690. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  691. end;
  692. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  693. function symtable_has_varsyms(st:tsymtable):boolean;
  694. var
  695. sym : tsym;
  696. begin
  697. result:=false;
  698. sym:=tsym(st.symindex.first);
  699. while assigned(sym) do
  700. begin
  701. if sym.typ=varsym then
  702. begin
  703. result:=true;
  704. exit;
  705. end;
  706. sym:=tsym(sym.indexnext);
  707. end;
  708. end;
  709. procedure setconst(l:aint);
  710. begin
  711. { We return the address of the field, just like Delphi/TP }
  712. case opr.typ of
  713. OPR_NONE :
  714. begin
  715. opr.typ:=OPR_CONSTANT;
  716. opr.val:=l;
  717. end;
  718. OPR_CONSTANT :
  719. inc(opr.val,l);
  720. OPR_REFERENCE :
  721. inc(opr.ref.offset,l);
  722. OPR_LOCAL :
  723. inc(opr.localsymofs,l);
  724. else
  725. Message(asmr_e_invalid_operand_type);
  726. end;
  727. end;
  728. { search and sets up the correct fields in the Instr record }
  729. { for the NON-constant identifier passed to the routine. }
  730. { if not found returns FALSE. }
  731. var
  732. sym : tsym;
  733. srsymtable : tsymtable;
  734. harrdef : tarraydef;
  735. indexreg : tregister;
  736. l : aint;
  737. plist : psymlistitem;
  738. Begin
  739. SetupVar:=false;
  740. asmsearchsym(s,sym,srsymtable);
  741. if sym = nil then
  742. exit;
  743. if sym.typ=absolutesym then
  744. begin
  745. if (tabsolutesym(sym).abstyp=tovar) then
  746. begin
  747. { Only support simple loads }
  748. plist:=tabsolutesym(sym).ref.firstsym;
  749. if assigned(plist) and
  750. (plist^.sltype=sl_load) then
  751. sym:=plist^.sym
  752. else
  753. begin
  754. Message(asmr_e_unsupported_symbol_type);
  755. exit;
  756. end;
  757. end
  758. else
  759. begin
  760. Message(asmr_e_unsupported_symbol_type);
  761. exit;
  762. end;
  763. end;
  764. case sym.typ of
  765. varsym :
  766. begin
  767. { we always assume in asm statements that }
  768. { that the variable is valid. }
  769. tvarsym(sym).varstate:=vs_used;
  770. inc(tvarsym(sym).refs);
  771. case tvarsym(sym).owner.symtabletype of
  772. objectsymtable :
  773. begin
  774. setconst(tvarsym(sym).fieldoffset);
  775. hasvar:=true;
  776. SetupVar:=true;
  777. Exit;
  778. end;
  779. globalsymtable,
  780. staticsymtable :
  781. begin
  782. initref;
  783. opr.ref.symbol:=objectlibrary.newasmsymbol(tvarsym(sym).mangledname,AB_EXTERNAL,AT_DATA);
  784. end;
  785. parasymtable,
  786. localsymtable :
  787. begin
  788. if (vo_is_external in tvarsym(sym).varoptions) then
  789. begin
  790. initref;
  791. opr.ref.symbol:=objectlibrary.newasmsymbol(tvarsym(sym).mangledname,AB_EXTERNAL,AT_DATA)
  792. end
  793. else
  794. begin
  795. if opr.typ=OPR_REFERENCE then
  796. begin
  797. indexreg:=opr.ref.base;
  798. if opr.ref.index<>NR_NO then
  799. begin
  800. if indexreg=NR_NO then
  801. indexreg:=opr.ref.index
  802. else
  803. Message(asmr_e_multiple_index);
  804. end;
  805. end
  806. else
  807. indexreg:=NR_NO;
  808. opr.typ:=OPR_LOCAL;
  809. if assigned(current_procinfo.parent) and
  810. (current_procinfo.procdef.proccalloption<>pocall_inline) and
  811. (tvarsym(sym).owner<>current_procinfo.procdef.localst) and
  812. (tvarsym(sym).owner<>current_procinfo.procdef.parast) and
  813. (current_procinfo.procdef.localst.symtablelevel>normal_function_level) and
  814. symtable_has_varsyms(current_procinfo.procdef.localst) then
  815. message1(asmr_e_local_para_unreachable,s);
  816. { variable can't be placed in a register anymore }
  817. tvarsym(sym).varregable:=vr_none;
  818. opr.localsym:=tvarsym(sym);
  819. opr.localsymofs:=0;
  820. opr.localindexreg:=indexreg;
  821. opr.localscale:=0;
  822. opr.localgetoffset:=GetOffset;
  823. end;
  824. if paramanager.push_addr_param(tvarsym(sym).varspez,tvarsym(sym).vartype.def,current_procinfo.procdef.proccalloption) then
  825. SetSize(sizeof(aint),false);
  826. end;
  827. end;
  828. case tvarsym(sym).vartype.def.deftype of
  829. orddef,
  830. enumdef,
  831. pointerdef,
  832. floatdef :
  833. SetSize(tvarsym(sym).getsize,false);
  834. arraydef :
  835. begin
  836. { for arrays try to get the element size, take care of
  837. multiple indexes }
  838. harrdef:=tarraydef(tvarsym(sym).vartype.def);
  839. while assigned(harrdef.elementtype.def) and
  840. (harrdef.elementtype.def.deftype=arraydef) do
  841. harrdef:=tarraydef(harrdef.elementtype.def);
  842. SetSize(harrdef.elesize,false);
  843. end;
  844. end;
  845. hasvar:=true;
  846. SetupVar:=true;
  847. Exit;
  848. end;
  849. typedconstsym :
  850. begin
  851. initref;
  852. opr.ref.symbol:=objectlibrary.newasmsymbol(ttypedconstsym(sym).mangledname,AB_EXTERNAL,AT_DATA);
  853. case ttypedconstsym(sym).typedconsttype.def.deftype of
  854. orddef,
  855. enumdef,
  856. pointerdef,
  857. floatdef :
  858. SetSize(ttypedconstsym(sym).getsize,false);
  859. arraydef :
  860. begin
  861. { for arrays try to get the element size, take care of
  862. multiple indexes }
  863. harrdef:=tarraydef(ttypedconstsym(sym).typedconsttype.def);
  864. while assigned(harrdef.elementtype.def) and
  865. (harrdef.elementtype.def.deftype=arraydef) do
  866. harrdef:=tarraydef(harrdef.elementtype.def);
  867. SetSize(harrdef.elesize,false);
  868. end;
  869. end;
  870. hasvar:=true;
  871. SetupVar:=true;
  872. Exit;
  873. end;
  874. constsym :
  875. begin
  876. if tconstsym(sym).consttyp=constord then
  877. begin
  878. setconst(tconstsym(sym).value.valueord);
  879. SetupVar:=true;
  880. Exit;
  881. end;
  882. end;
  883. typesym :
  884. begin
  885. if ttypesym(sym).restype.def.deftype in [recorddef,objectdef] then
  886. begin
  887. setconst(0);
  888. SetupVar:=TRUE;
  889. Exit;
  890. end;
  891. end;
  892. procsym :
  893. begin
  894. if opr.typ<>OPR_NONE then
  895. Message(asmr_e_invalid_operand_type);
  896. if Tprocsym(sym).procdef_count>1 then
  897. Message(asmr_w_calling_overload_func);
  898. l:=opr.ref.offset;
  899. opr.typ:=OPR_SYMBOL;
  900. opr.symbol:=objectlibrary.newasmsymbol(tprocsym(sym).first_procdef.mangledname,AB_EXTERNAL,AT_FUNCTION);
  901. opr.symofs:=l;
  902. hasvar:=true;
  903. SetupVar:=TRUE;
  904. Exit;
  905. end;
  906. else
  907. begin
  908. Message(asmr_e_unsupported_symbol_type);
  909. exit;
  910. end;
  911. end;
  912. end;
  913. { looks for internal names of variables and routines }
  914. Function TOperand.SetupDirectVar(const hs:string): Boolean;
  915. var
  916. p : tasmsymbol;
  917. begin
  918. SetupDirectVar:=false;
  919. p:=objectlibrary.getasmsymbol(hs);
  920. if assigned(p) then
  921. begin
  922. InitRef;
  923. opr.ref.symbol:=p;
  924. hasvar:=true;
  925. SetupDirectVar:=true;
  926. end;
  927. end;
  928. procedure TOperand.InitRef;
  929. {*********************************************************************}
  930. { Description: This routine first check if the opcode is of }
  931. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  932. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  933. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  934. { to point to the default segment. }
  935. {*********************************************************************}
  936. var
  937. l : aint;
  938. reg : tregister;
  939. Begin
  940. case opr.typ of
  941. OPR_REFERENCE :
  942. exit;
  943. OPR_CONSTANT :
  944. begin
  945. l:=opr.val;
  946. opr.typ:=OPR_REFERENCE;
  947. Fillchar(opr.ref,sizeof(treference),0);
  948. opr.Ref.Offset:=l;
  949. end;
  950. OPR_NONE :
  951. begin
  952. opr.typ:=OPR_REFERENCE;
  953. Fillchar(opr.ref,sizeof(treference),0);
  954. end;
  955. OPR_REGISTER :
  956. begin
  957. reg:=opr.reg;
  958. opr.typ:=OPR_REFERENCE;
  959. Fillchar(opr.ref,sizeof(treference),0);
  960. opr.Ref.base:=reg;
  961. end;
  962. else
  963. begin
  964. Message(asmr_e_invalid_operand_type);
  965. { Recover }
  966. opr.typ:=OPR_REFERENCE;
  967. Fillchar(opr.ref,sizeof(treference),0);
  968. end;
  969. end;
  970. end;
  971. {****************************************************************************
  972. TInstruction
  973. ****************************************************************************}
  974. constructor TInstruction.create(optype : tcoperand);
  975. var
  976. i : longint;
  977. Begin
  978. { these field are set to 0 anyways by the constructor helper (FK)
  979. Opcode:=A_NONE;
  980. Condition:=C_NONE;
  981. Ops:=0;
  982. }
  983. for i:=1 to max_operands do
  984. Operands[i]:=optype.create;
  985. Labeled:=false;
  986. end;
  987. destructor TInstruction.destroy;
  988. var
  989. i : longint;
  990. Begin
  991. for i:=1 to max_operands do
  992. Operands[i].free;
  993. end;
  994. Procedure TInstruction.Swapoperands;
  995. Var
  996. p : toperand;
  997. Begin
  998. case Ops of
  999. 2 :
  1000. begin
  1001. p:=Operands[1];
  1002. Operands[1]:=Operands[2];
  1003. Operands[2]:=p;
  1004. end;
  1005. 3 :
  1006. begin
  1007. p:=Operands[1];
  1008. Operands[1]:=Operands[3];
  1009. Operands[3]:=p;
  1010. end;
  1011. end;
  1012. end;
  1013. function TInstruction.ConcatInstruction(p:TAAsmoutput) : tai;
  1014. var
  1015. ai : taicpu;
  1016. i : longint;
  1017. begin
  1018. ai:=taicpu.op_none(opcode);
  1019. ai.Ops:=Ops;
  1020. ai.Allocate_oper(Ops);
  1021. for i:=1 to Ops do
  1022. with operands[i].opr do
  1023. begin
  1024. case typ of
  1025. OPR_CONSTANT :
  1026. ai.loadconst(i-1,val);
  1027. OPR_REGISTER:
  1028. ai.loadreg(i-1,reg);
  1029. OPR_SYMBOL:
  1030. ai.loadsymbol(i-1,symbol,symofs);
  1031. OPR_LOCAL :
  1032. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  1033. localscale,localgetoffset);
  1034. OPR_REFERENCE:
  1035. ai.loadref(i-1,ref);
  1036. {$ifdef ARM}
  1037. OPR_REGSET:
  1038. ai.loadregset(i-1,regset);
  1039. {$endif ARM}
  1040. end;
  1041. end;
  1042. ai.SetCondition(condition);
  1043. { Concat the opcode or give an error }
  1044. if assigned(ai) then
  1045. p.concat(ai)
  1046. else
  1047. Message(asmr_e_invalid_opcode_and_operand);
  1048. result:=ai;
  1049. end;
  1050. {***************************************************************************
  1051. TLocalLabel
  1052. ***************************************************************************}
  1053. constructor TLocalLabel.create(const n:string);
  1054. begin
  1055. inherited CreateName(n);
  1056. lab:=nil;
  1057. emitted:=false;
  1058. end;
  1059. function TLocalLabel.Gettasmlabel:tasmlabel;
  1060. begin
  1061. if not assigned(lab) then
  1062. begin
  1063. objectlibrary.getlabel(lab);
  1064. { this label is forced to be used so it's always written }
  1065. lab.increfs;
  1066. end;
  1067. Gettasmlabel:=lab;
  1068. end;
  1069. {***************************************************************************
  1070. TLocalLabelList
  1071. ***************************************************************************}
  1072. procedure LocalLabelEmitted(p:tnamedindexitem;arg:pointer);
  1073. begin
  1074. if not TLocalLabel(p).emitted then
  1075. Message1(asmr_e_unknown_label_identifier,p.name);
  1076. end;
  1077. procedure TLocalLabelList.CheckEmitted;
  1078. begin
  1079. ForEach_Static({$ifdef FPCPROCVAR}@{$endif}LocalLabelEmitted,nil)
  1080. end;
  1081. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  1082. var
  1083. lab : TLocalLabel;
  1084. Begin
  1085. CreateLocalLabel:=true;
  1086. { Check if it already is defined }
  1087. lab:=TLocalLabel(LocalLabellist.Search(s));
  1088. if not assigned(lab) then
  1089. begin
  1090. lab:=TLocalLabel.Create(s);
  1091. LocalLabellist.Insert(lab);
  1092. end;
  1093. { set emitted flag and check for dup syms }
  1094. if emit then
  1095. begin
  1096. if lab.Emitted then
  1097. begin
  1098. Message1(asmr_e_dup_local_sym,lab.Name);
  1099. CreateLocalLabel:=false;
  1100. end;
  1101. lab.Emitted:=true;
  1102. end;
  1103. hl:=lab.Gettasmlabel;
  1104. end;
  1105. {****************************************************************************
  1106. Symbol table helper routines
  1107. ****************************************************************************}
  1108. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  1109. var
  1110. i : integer;
  1111. begin
  1112. i:=pos('.',s);
  1113. { allow unit.identifier }
  1114. if i>0 then
  1115. begin
  1116. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1117. if assigned(srsym) then
  1118. begin
  1119. if (srsym.typ=unitsym) and
  1120. (srsym.owner.unitid=0) then
  1121. srsym:=searchsymonlyin(tunitsym(srsym).unitsymtable,Copy(s,i+1,255))
  1122. else
  1123. srsym:=nil;
  1124. end;
  1125. end
  1126. else
  1127. searchsym(s,srsym,srsymtable);
  1128. end;
  1129. Function SearchType(const hs:string;var size:aint): Boolean;
  1130. var
  1131. srsym : tsym;
  1132. srsymtable : tsymtable;
  1133. begin
  1134. result:=false;
  1135. size:=0;
  1136. asmsearchsym(hs,srsym,srsymtable);
  1137. if assigned(srsym) and
  1138. (srsym.typ=typesym) then
  1139. begin
  1140. size:=ttypesym(srsym).restype.def.size;
  1141. result:=true;
  1142. end;
  1143. end;
  1144. Function SearchRecordType(const s:string): boolean;
  1145. var
  1146. srsym : tsym;
  1147. srsymtable : tsymtable;
  1148. Begin
  1149. SearchRecordType:=false;
  1150. { Check the constants in symtable }
  1151. asmsearchsym(s,srsym,srsymtable);
  1152. if srsym <> nil then
  1153. Begin
  1154. case srsym.typ of
  1155. typesym :
  1156. begin
  1157. if ttypesym(srsym).restype.def.deftype in [recorddef,objectdef] then
  1158. begin
  1159. SearchRecordType:=true;
  1160. exit;
  1161. end;
  1162. end;
  1163. end;
  1164. end;
  1165. end;
  1166. Function SearchIConstant(const s:string; var l:aint): boolean;
  1167. {**********************************************************************}
  1168. { Description: Searches for a CONSTANT of name s in either the local }
  1169. { symbol list, then in the global symbol list, and returns the value }
  1170. { of that constant in l. Returns TRUE if successfull, if not found, }
  1171. { or if the constant is not of correct type, then returns FALSE }
  1172. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1173. { respectively. }
  1174. {**********************************************************************}
  1175. var
  1176. srsym : tsym;
  1177. srsymtable : tsymtable;
  1178. Begin
  1179. SearchIConstant:=false;
  1180. { check for TRUE or FALSE reserved words first }
  1181. if s = 'TRUE' then
  1182. Begin
  1183. SearchIConstant:=TRUE;
  1184. l:=1;
  1185. exit;
  1186. end;
  1187. if s = 'FALSE' then
  1188. Begin
  1189. SearchIConstant:=TRUE;
  1190. l:=0;
  1191. exit;
  1192. end;
  1193. { Check the constants in symtable }
  1194. asmsearchsym(s,srsym,srsymtable);
  1195. if srsym <> nil then
  1196. Begin
  1197. case srsym.typ of
  1198. constsym :
  1199. begin
  1200. if tconstsym(srsym).consttyp=constord then
  1201. Begin
  1202. l:=tconstsym(srsym).value.valueord;
  1203. SearchIConstant:=TRUE;
  1204. exit;
  1205. end;
  1206. end;
  1207. enumsym:
  1208. Begin
  1209. l:=tenumsym(srsym).value;
  1210. SearchIConstant:=TRUE;
  1211. exit;
  1212. end;
  1213. end;
  1214. end;
  1215. end;
  1216. Function GetRecordOffsetSize(s:string;Var Offset: aint;var Size:aint):boolean;
  1217. { search and returns the offset and size of records/objects of the base }
  1218. { with field name setup in field. }
  1219. { returns FALSE if not found. }
  1220. { used when base is a variable or a typed constant name. }
  1221. var
  1222. st : tsymtable;
  1223. harrdef : tarraydef;
  1224. sym : tsym;
  1225. srsymtable : tsymtable;
  1226. i : longint;
  1227. base : string;
  1228. Begin
  1229. GetRecordOffsetSize:=FALSE;
  1230. Offset:=0;
  1231. Size:=0;
  1232. i:=pos('.',s);
  1233. if i=0 then
  1234. i:=255;
  1235. base:=Copy(s,1,i-1);
  1236. delete(s,1,i);
  1237. if base='SELF' then
  1238. st:=current_procinfo.procdef._class.symtable
  1239. else
  1240. begin
  1241. asmsearchsym(base,sym,srsymtable);
  1242. st:=nil;
  1243. { we can start with a var,type,typedconst }
  1244. if assigned(sym) then
  1245. case sym.typ of
  1246. varsym :
  1247. with Tvarsym(sym).vartype do
  1248. case def.deftype of
  1249. recorddef :
  1250. st:=trecorddef(def).symtable;
  1251. objectdef :
  1252. st:=tobjectdef(def).symtable;
  1253. end;
  1254. typesym :
  1255. with Ttypesym(sym).restype do
  1256. case def.deftype of
  1257. recorddef :
  1258. st:=trecorddef(def).symtable;
  1259. objectdef :
  1260. st:=tobjectdef(def).symtable;
  1261. end;
  1262. typedconstsym :
  1263. with Ttypedconstsym(sym).typedconsttype do
  1264. case def.deftype of
  1265. recorddef :
  1266. st:=trecorddef(def).symtable;
  1267. objectdef :
  1268. st:=tobjectdef(def).symtable;
  1269. end;
  1270. end
  1271. else
  1272. s:='';
  1273. end;
  1274. { now walk all recordsymtables }
  1275. while assigned(st) and (s<>'') do
  1276. begin
  1277. { load next field in base }
  1278. i:=pos('.',s);
  1279. if i=0 then
  1280. i:=255;
  1281. base:=Copy(s,1,i-1);
  1282. delete(s,1,i);
  1283. if st.symtabletype=objectsymtable then
  1284. sym:=search_class_member(tobjectdef(st.defowner),base)
  1285. else
  1286. sym:=tsym(st.search(base));
  1287. if not assigned(sym) then
  1288. begin
  1289. GetRecordOffsetSize:=false;
  1290. exit;
  1291. end;
  1292. st:=nil;
  1293. case sym.typ of
  1294. varsym :
  1295. with Tvarsym(sym) do
  1296. begin
  1297. inc(Offset,fieldoffset);
  1298. size:=getsize;
  1299. with vartype do
  1300. case def.deftype of
  1301. arraydef :
  1302. begin
  1303. { for arrays try to get the element size, take care of
  1304. multiple indexes }
  1305. harrdef:=tarraydef(def);
  1306. while assigned(harrdef.elementtype.def) and
  1307. (harrdef.elementtype.def.deftype=arraydef) do
  1308. harrdef:=tarraydef(harrdef.elementtype.def);
  1309. size:=harrdef.elesize;
  1310. end;
  1311. recorddef :
  1312. st:=trecorddef(def).symtable;
  1313. objectdef :
  1314. st:=tobjectdef(def).symtable;
  1315. end;
  1316. end;
  1317. end;
  1318. end;
  1319. GetRecordOffsetSize:=(s='');
  1320. end;
  1321. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1322. var
  1323. sym : tsym;
  1324. srsymtable : tsymtable;
  1325. hs : string;
  1326. Begin
  1327. hl:=nil;
  1328. SearchLabel:=false;
  1329. { Check for pascal labels, which are case insensetive }
  1330. hs:=upper(s);
  1331. asmsearchsym(hs,sym,srsymtable);
  1332. if sym=nil then
  1333. exit;
  1334. case sym.typ of
  1335. labelsym :
  1336. begin
  1337. hl:=tlabelsym(sym).lab;
  1338. if emit then
  1339. tlabelsym(sym).defined:=true
  1340. else
  1341. tlabelsym(sym).used:=true;
  1342. SearchLabel:=true;
  1343. end;
  1344. end;
  1345. end;
  1346. {*************************************************************************}
  1347. { Instruction Generation Utilities }
  1348. {*************************************************************************}
  1349. Procedure ConcatString(p : TAAsmoutput;s:string);
  1350. {*********************************************************************}
  1351. { PROCEDURE ConcatString(s:string); }
  1352. { Description: This routine adds the character chain pointed to in }
  1353. { s to the instruction linked list. }
  1354. {*********************************************************************}
  1355. Var
  1356. pc: PChar;
  1357. Begin
  1358. getmem(pc,length(s)+1);
  1359. p.concat(Tai_string.Create_length_pchar(strpcopy(pc,s),length(s)));
  1360. end;
  1361. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  1362. {*********************************************************************}
  1363. { PROCEDURE ConcatPasString(s:string); }
  1364. { Description: This routine adds the character chain pointed to in }
  1365. { s to the instruction linked list, contrary to ConcatString it }
  1366. { uses a pascal style string, so it conserves null characters. }
  1367. {*********************************************************************}
  1368. Begin
  1369. p.concat(Tai_string.Create(s));
  1370. end;
  1371. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  1372. {*********************************************************************}
  1373. { PROCEDURE ConcatDirect(s:string) }
  1374. { Description: This routine output the string directly to the asm }
  1375. { output, it is only sed when writing special labels in AT&T mode, }
  1376. { and should not be used without due consideration, since it may }
  1377. { cause problems. }
  1378. {*********************************************************************}
  1379. Var
  1380. pc: PChar;
  1381. Begin
  1382. getmem(pc,length(s)+1);
  1383. p.concat(Tai_direct.Create(strpcopy(pc,s)));
  1384. end;
  1385. Procedure ConcatConstant(p: TAAsmoutput; value: aint; constsize:byte);
  1386. {*********************************************************************}
  1387. { PROCEDURE ConcatConstant(value: aint; maxvalue: aint); }
  1388. { Description: This routine adds the value constant to the current }
  1389. { instruction linked list. }
  1390. { maxvalue -> indicates the size of the data to initialize: }
  1391. { $ff -> create a byte node. }
  1392. { $ffff -> create a word node. }
  1393. { $ffffffff -> create a dword node. }
  1394. {*********************************************************************}
  1395. var
  1396. rangelo,rangehi : int64;
  1397. Begin
  1398. case constsize of
  1399. 1 :
  1400. begin
  1401. p.concat(Tai_const.Create_8bit(byte(value)));
  1402. rangelo:=low(shortint);
  1403. rangehi:=high(byte);
  1404. end;
  1405. 2 :
  1406. begin
  1407. p.concat(Tai_const.Create_16bit(word(value)));
  1408. rangelo:=low(smallint);
  1409. rangehi:=high(word);
  1410. end;
  1411. 4 :
  1412. begin
  1413. p.concat(Tai_const.Create_32bit(longint(value)));
  1414. rangelo:=low(longint);
  1415. rangehi:=high(cardinal);
  1416. end;
  1417. 8 :
  1418. begin
  1419. p.concat(Tai_const.Create_64bit(int64(value)));
  1420. rangelo:=0;
  1421. rangehi:=0;
  1422. end;
  1423. else
  1424. internalerror(200405011);
  1425. end;
  1426. { check for out of bounds }
  1427. if (rangelo<>0) and
  1428. ((value>rangehi) or (value<rangelo)) then
  1429. Message(asmr_e_constant_out_of_bounds);
  1430. end;
  1431. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:aint);
  1432. begin
  1433. p.concat(Tai_const.Createname(sym,AT_DATA,l));
  1434. end;
  1435. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  1436. {***********************************************************************}
  1437. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1438. { Description: This routine adds the value constant to the current }
  1439. { instruction linked list. }
  1440. { real_typ -> indicates the type of the real data to initialize: }
  1441. { s32real -> create a single node. }
  1442. { s64real -> create a double node. }
  1443. { s80real -> create an extended node. }
  1444. { s64bit -> create a comp node. }
  1445. { f32bit -> create a fixed node. (not used normally) }
  1446. {***********************************************************************}
  1447. Begin
  1448. case real_typ of
  1449. s32real : p.concat(Tai_real_32bit.Create(value));
  1450. s64real :
  1451. {$ifdef ARM}
  1452. if aktfputype in [fpu_fpa,fpu_fpa10,fpu_fpa11] then
  1453. p.concat(Tai_real_64bit.Create_hiloswapped(value))
  1454. else
  1455. {$endif ARM}
  1456. p.concat(Tai_real_64bit.Create(value));
  1457. s80real : p.concat(Tai_real_80bit.Create(value));
  1458. s64comp : p.concat(Tai_comp_64bit.Create(trunc(value)));
  1459. end;
  1460. end;
  1461. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  1462. {*********************************************************************}
  1463. { PROCEDURE ConcatLabel }
  1464. { Description: This routine either emits a label or a labeled }
  1465. { instruction to the linked list of instructions. }
  1466. {*********************************************************************}
  1467. begin
  1468. p.concat(Tai_label.Create(l));
  1469. end;
  1470. procedure ConcatAlign(p:TAAsmoutput;l:aint);
  1471. {*********************************************************************}
  1472. { PROCEDURE ConcatPublic }
  1473. { Description: This routine emits an global definition to the }
  1474. { linked list of instructions.(used by AT&T styled asm) }
  1475. {*********************************************************************}
  1476. begin
  1477. p.concat(Tai_align.Create(l));
  1478. end;
  1479. procedure ConcatPublic(p:TAAsmoutput;const s : string);
  1480. {*********************************************************************}
  1481. { PROCEDURE ConcatPublic }
  1482. { Description: This routine emits an global definition to the }
  1483. { linked list of instructions.(used by AT&T styled asm) }
  1484. {*********************************************************************}
  1485. begin
  1486. p.concat(Tai_symbol.Createname_global(s,AT_FUNCTION,0));
  1487. end;
  1488. procedure ConcatLocal(p:TAAsmoutput;const s : string);
  1489. {*********************************************************************}
  1490. { PROCEDURE ConcatLocal }
  1491. { Description: This routine emits an local definition to the }
  1492. { linked list of instructions. }
  1493. {*********************************************************************}
  1494. begin
  1495. p.concat(Tai_symbol.Createname(s,AT_FUNCTION,0));
  1496. end;
  1497. Procedure ConcatGlobalBss(const s : string;size : aint);
  1498. {*********************************************************************}
  1499. { PROCEDURE ConcatGlobalBss }
  1500. { Description: This routine emits an global datablock to the }
  1501. { linked list of instructions. }
  1502. {*********************************************************************}
  1503. begin
  1504. bssSegment.concat(Tai_datablock.Create_global(s,size));
  1505. end;
  1506. Procedure ConcatLocalBss(const s : string;size : aint);
  1507. {*********************************************************************}
  1508. { PROCEDURE ConcatLocalBss }
  1509. { Description: This routine emits a local datablcok to the }
  1510. { linked list of instructions. }
  1511. {*********************************************************************}
  1512. begin
  1513. bssSegment.concat(Tai_datablock.Create(s,size));
  1514. end;
  1515. end.
  1516. {
  1517. $Log$
  1518. Revision 1.90 2004-10-08 17:09:43 peter
  1519. * tvarsym.varregable added, split vo_regable from varoptions
  1520. Revision 1.89 2004/09/27 15:14:34 peter
  1521. * reference local varsyms can't be regvar
  1522. Revision 1.88 2004/06/20 08:55:30 florian
  1523. * logs truncated
  1524. Revision 1.87 2004/06/16 20:07:09 florian
  1525. * dwarf branch merged
  1526. Revision 1.86 2004/05/06 20:30:51 florian
  1527. * m68k compiler compilation fixed
  1528. Revision 1.85.2.7 2004/05/25 21:38:53 peter
  1529. * assembler reader/writer updates
  1530. Revision 1.85.2.6 2004/05/18 20:24:03 florian
  1531. * fixed crash with unknown symbols
  1532. Revision 1.85.2.5 2004/05/02 00:13:16 peter
  1533. * remove limit of 15 digits
  1534. }