rautils.pas 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  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);
  53. TOprRec = record
  54. case typ:TOprType of
  55. OPR_NONE : ();
  56. OPR_CONSTANT : (val:longint);
  57. OPR_SYMBOL : (symbol:tasmsymbol;symofs:longint);
  58. OPR_REFERENCE : (ref:treference);
  59. OPR_LOCAL : (localsym:tvarsym;localsymofs:longint);
  60. OPR_REGISTER : (reg:tregister);
  61. {$ifdef m68k}
  62. OPR_REGLIST : (reglist:Tsupregset);
  63. {$else not m68k}
  64. OPR_REGLIST : ();
  65. {$endif m68k}
  66. end;
  67. TOperand = class
  68. hastype, { if the operand has typecasted variable }
  69. hasvar : boolean; { if the operand is loaded with a variable }
  70. size : TCGSize;
  71. opr : TOprRec;
  72. constructor create;
  73. destructor destroy;override;
  74. Procedure BuildOperand;virtual;
  75. Procedure SetSize(_size:longint;force:boolean);virtual;
  76. Procedure SetCorrectSize(opcode:tasmop);virtual;
  77. Function SetupResult:boolean;virtual;
  78. Function SetupSelf:boolean;
  79. Function SetupOldEBP:boolean;
  80. Function SetupVar(const s:string;GetOffset : boolean): Boolean;
  81. Function SetupDirectVar(const hs:string): Boolean;
  82. Procedure InitRef;
  83. end;
  84. TInstruction = class
  85. opcode : tasmop;
  86. condition : tasmcond;
  87. ops : byte;
  88. labeled : boolean;
  89. operands : array[1..max_operands] of toperand;
  90. constructor create;
  91. destructor destroy;override;
  92. Procedure InitOperands;virtual;
  93. Procedure BuildOpcode;virtual;
  94. procedure ConcatInstruction(p:TAAsmoutput);virtual;
  95. Procedure Swapoperands;
  96. end;
  97. {---------------------------------------------------------------------}
  98. { Expression parser types }
  99. {---------------------------------------------------------------------}
  100. TExprOperator = record
  101. ch: char; { operator }
  102. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  103. end;
  104. String15 = String[15];
  105. {**********************************************************************}
  106. { The following operators are supported: }
  107. { '+' : addition }
  108. { '-' : subtraction }
  109. { '*' : multiplication }
  110. { '/' : modulo division }
  111. { '^' : exclusive or }
  112. { '<' : shift left }
  113. { '>' : shift right }
  114. { '&' : bitwise and }
  115. { '|' : bitwise or }
  116. { '~' : bitwise complement }
  117. { '%' : modulo division }
  118. { nnn: longint numbers }
  119. { ( and ) parenthesis }
  120. {**********************************************************************}
  121. TExprParse = class
  122. public
  123. Constructor create;
  124. Destructor Destroy;override;
  125. Function Evaluate(Expr: String): longint;
  126. Function Priority(_Operator: Char): longint;
  127. private
  128. RPNStack : Array[1..RPNMax] of longint; { Stack For RPN calculator }
  129. RPNTop : longint;
  130. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  131. OpTop : longint;
  132. Procedure RPNPush(Num: Longint);
  133. Function RPNPop: Longint;
  134. Procedure RPNCalc(token: String15; prefix: boolean);
  135. Procedure OpPush(_Operator: char; prefix: boolean);
  136. { In reality returns TExprOperaotr }
  137. Procedure OpPop(var _Operator:TExprOperator);
  138. end;
  139. { Evaluate an expression string to a longint }
  140. Function CalculateExpression(const expression: string): longint;
  141. {---------------------------------------------------------------------}
  142. { String routines }
  143. {---------------------------------------------------------------------}
  144. Function ValDecimal(const S:String):longint;
  145. Function ValOctal(const S:String):longint;
  146. Function ValBinary(const S:String):longint;
  147. Function ValHexaDecimal(const S:String):longint;
  148. Function PadZero(Var s: String; n: byte): Boolean;
  149. Function EscapeToPascal(const s:string): string;
  150. {---------------------------------------------------------------------
  151. Symbol helper routines
  152. ---------------------------------------------------------------------}
  153. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  154. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  155. Function SearchType(const hs:string;var size:longint): Boolean;
  156. Function SearchRecordType(const s:string): boolean;
  157. Function SearchIConstant(const s:string; var l:longint): boolean;
  158. {---------------------------------------------------------------------
  159. Instruction generation routines
  160. ---------------------------------------------------------------------}
  161. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  162. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  163. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  164. Procedure ConcatConstant(p : TAAsmoutput;value: longint; maxvalue: longint);
  165. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:longint);
  166. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  167. Procedure ConcatString(p : TAAsmoutput;s:string);
  168. procedure ConcatAlign(p:TAAsmoutput;l:longint);
  169. Procedure ConcatPublic(p:TAAsmoutput;const s : string);
  170. Procedure ConcatLocal(p:TAAsmoutput;const s : string);
  171. Procedure ConcatGlobalBss(const s : string;size : longint);
  172. Procedure ConcatLocalBss(const s : string;size : longint);
  173. Implementation
  174. uses
  175. {$ifdef delphi}
  176. sysutils,
  177. {$else}
  178. strings,
  179. {$endif}
  180. defutil,systems,verbose,globals,
  181. symtable,paramgr,
  182. aasmcpu,
  183. procinfo;
  184. {*************************************************************************
  185. TExprParse
  186. *************************************************************************}
  187. Constructor TExprParse.create;
  188. Begin
  189. end;
  190. Procedure TExprParse.RPNPush(Num : longint);
  191. { Add an operand to the top of the RPN stack }
  192. begin
  193. if RPNTop < RPNMax then
  194. begin
  195. Inc(RPNTop);
  196. RPNStack[RPNTop]:=Num;
  197. end
  198. else
  199. Message(asmr_e_expr_illegal);
  200. end;
  201. Function TExprParse.RPNPop : longint;
  202. { Get the operand at the top of the RPN stack }
  203. begin
  204. if RPNTop > 0 then
  205. begin
  206. RPNPop:=RPNStack[RPNTop];
  207. Dec(RPNTop);
  208. end
  209. else
  210. Message(asmr_e_expr_illegal);
  211. end;
  212. Procedure TExprParse.RPNCalc(Token : String15; prefix:boolean); { RPN Calculator }
  213. Var
  214. Temp : longint;
  215. n1,n2 : longint;
  216. LocalError : Integer;
  217. begin
  218. { Handle operators }
  219. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  220. Case Token[1] of
  221. '+' :
  222. Begin
  223. if not prefix then
  224. RPNPush(RPNPop + RPNPop);
  225. end;
  226. '-' :
  227. Begin
  228. if prefix then
  229. RPNPush(-(RPNPop))
  230. else
  231. begin
  232. n1:=RPNPop;
  233. n2:=RPNPop;
  234. RPNPush(n2 - n1);
  235. end;
  236. end;
  237. '*' : RPNPush(RPNPop * RPNPop);
  238. '&' :
  239. begin
  240. n1:=RPNPop;
  241. n2:=RPNPop;
  242. RPNPush(n2 and n1);
  243. end;
  244. '|' :
  245. begin
  246. n1:=RPNPop;
  247. n2:=RPNPop;
  248. RPNPush(n2 or n1);
  249. end;
  250. '~' : RPNPush(NOT RPNPop);
  251. '<' :
  252. begin
  253. n1:=RPNPop;
  254. n2:=RPNPop;
  255. RPNPush(n2 SHL n1);
  256. end;
  257. '>' :
  258. begin
  259. n1:=RPNPop;
  260. n2:=RPNPop;
  261. RPNPush(n2 SHR n1);
  262. end;
  263. '%' :
  264. begin
  265. Temp:=RPNPop;
  266. if Temp <> 0 then
  267. RPNPush(RPNPop mod Temp)
  268. else
  269. begin
  270. Message(asmr_e_expr_zero_divide);
  271. { push 1 for error recovery }
  272. RPNPush(1);
  273. end;
  274. end;
  275. '^' : RPNPush(RPNPop XOR RPNPop);
  276. '/' :
  277. begin
  278. Temp:=RPNPop;
  279. if Temp <> 0 then
  280. RPNPush(RPNPop div Temp)
  281. else
  282. begin
  283. Message(asmr_e_expr_zero_divide);
  284. { push 1 for error recovery }
  285. RPNPush(1);
  286. end;
  287. end;
  288. end
  289. else
  290. begin
  291. { Convert String to number and add to stack }
  292. Val(Token, Temp, LocalError);
  293. if LocalError = 0 then
  294. RPNPush(Temp)
  295. else
  296. begin
  297. Message(asmr_e_expr_illegal);
  298. { push 1 for error recovery }
  299. RPNPush(1);
  300. end;
  301. end;
  302. end;
  303. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  304. { Add an operator onto top of the stack }
  305. begin
  306. if OpTop < OpMax then
  307. begin
  308. Inc(OpTop);
  309. OpStack[OpTop].ch:=_Operator;
  310. OpStack[OpTop].is_prefix:=prefix;
  311. end
  312. else
  313. Message(asmr_e_expr_illegal);
  314. end;
  315. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  316. { Get operator at the top of the stack }
  317. begin
  318. if OpTop > 0 then
  319. begin
  320. _Operator:=OpStack[OpTop];
  321. Dec(OpTop);
  322. end
  323. else
  324. Message(asmr_e_expr_illegal);
  325. end;
  326. Function TExprParse.Priority(_Operator : Char) : longint;
  327. { Return priority of operator }
  328. { The greater the priority, the higher the precedence }
  329. begin
  330. Case _Operator OF
  331. '(' :
  332. Priority:=0;
  333. '+', '-' :
  334. Priority:=1;
  335. '*', '/','%','<','>' :
  336. Priority:=2;
  337. '|','&','^','~' :
  338. Priority:=0;
  339. else
  340. Message(asmr_e_expr_illegal);
  341. end;
  342. end;
  343. Function TExprParse.Evaluate(Expr : String):longint;
  344. Var
  345. I : LongInt;
  346. Token : String15;
  347. opr : TExprOperator;
  348. begin
  349. Evaluate:=0;
  350. { Reset stacks }
  351. OpTop :=0;
  352. RPNTop:=0;
  353. Token :='';
  354. { nothing to do ? }
  355. if Expr='' then
  356. exit;
  357. For I:=1 to Length(Expr) DO
  358. begin
  359. if Expr[I] in ['0'..'9'] then
  360. begin { Build multi-digit numbers }
  361. Token:=Token + Expr[I];
  362. if I = Length(Expr) then { Send last one to calculator }
  363. RPNCalc(Token,false);
  364. end
  365. else
  366. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  367. begin
  368. if Token <> '' then
  369. begin { Send last built number to calc. }
  370. RPNCalc(Token,false);
  371. Token:='';
  372. end;
  373. Case Expr[I] OF
  374. '(' : OpPush('(',false);
  375. ')' : begin
  376. While OpStack[OpTop].ch <> '(' DO
  377. Begin
  378. OpPop(opr);
  379. RPNCalc(opr.ch,opr.is_prefix);
  380. end;
  381. OpPop(opr); { Pop off and ignore the '(' }
  382. end;
  383. '+','-','~' : Begin
  384. { workaround for -2147483648 }
  385. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  386. begin
  387. token:='-';
  388. expr[i]:='+';
  389. end;
  390. { if start of expression then surely a prefix }
  391. { or if previous char was also an operator }
  392. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  393. OpPush(Expr[I],true)
  394. else
  395. Begin
  396. { Evaluate all higher priority operators }
  397. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  398. Begin
  399. OpPop(opr);
  400. RPNCalc(opr.ch,opr.is_prefix);
  401. end;
  402. OpPush(Expr[I],false);
  403. End;
  404. end;
  405. '*', '/',
  406. '^','|','&',
  407. '%','<','>' : begin
  408. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  409. Begin
  410. OpPop(opr);
  411. RPNCalc(opr.ch,opr.is_prefix);
  412. end;
  413. OpPush(Expr[I],false);
  414. end;
  415. end; { Case }
  416. end
  417. else
  418. Message(asmr_e_expr_illegal); { Handle bad input error }
  419. end;
  420. { Pop off the remaining operators }
  421. While OpTop > 0 do
  422. Begin
  423. OpPop(opr);
  424. RPNCalc(opr.ch,opr.is_prefix);
  425. end;
  426. { The result is stored on the top of the stack }
  427. Evaluate:=RPNPop;
  428. end;
  429. Destructor TExprParse.Destroy;
  430. Begin
  431. end;
  432. Function CalculateExpression(const expression: string): longint;
  433. var
  434. expr: TExprParse;
  435. Begin
  436. expr:=TExprParse.create;
  437. CalculateExpression:=expr.Evaluate(expression);
  438. expr.Free;
  439. end;
  440. {*************************************************************************}
  441. { String conversions/utils }
  442. {*************************************************************************}
  443. Function EscapeToPascal(const s:string): string;
  444. { converts a C styled string - which contains escape }
  445. { characters to a pascal style string. }
  446. var
  447. i,len : longint;
  448. hs : string;
  449. temp : string;
  450. c : char;
  451. Begin
  452. hs:='';
  453. len:=0;
  454. i:=0;
  455. while (i<length(s)) and (len<255) do
  456. begin
  457. Inc(i);
  458. if (s[i]='\') and (i<length(s)) then
  459. Begin
  460. inc(i);
  461. case s[i] of
  462. '\' :
  463. c:='\';
  464. 'b':
  465. c:=#8;
  466. 'f':
  467. c:=#12;
  468. 'n':
  469. c:=#10;
  470. 'r':
  471. c:=#13;
  472. 't':
  473. c:=#9;
  474. '"':
  475. c:='"';
  476. '0'..'7':
  477. Begin
  478. temp:=s[i];
  479. temp:=temp+s[i+1];
  480. temp:=temp+s[i+2];
  481. inc(i,2);
  482. c:=chr(ValOctal(temp));
  483. end;
  484. 'x':
  485. Begin
  486. temp:=s[i+1];
  487. temp:=temp+s[i+2];
  488. inc(i,2);
  489. c:=chr(ValHexaDecimal(temp));
  490. end;
  491. else
  492. Begin
  493. Message1(asmr_e_escape_seq_ignored,s[i]);
  494. c:=s[i];
  495. end;
  496. end;
  497. end
  498. else
  499. c:=s[i];
  500. inc(len);
  501. hs[len]:=c;
  502. end;
  503. hs[0]:=chr(len);
  504. EscapeToPascal:=hs;
  505. end;
  506. Function ValDecimal(const S:String):longint;
  507. { Converts a decimal string to longint }
  508. var
  509. vs,c : longint;
  510. Begin
  511. vs:=0;
  512. for c:=1 to length(s) do
  513. begin
  514. vs:=vs*10;
  515. if s[c] in ['0'..'9'] then
  516. inc(vs,ord(s[c])-ord('0'))
  517. else
  518. begin
  519. Message1(asmr_e_error_converting_decimal,s);
  520. ValDecimal:=0;
  521. exit;
  522. end;
  523. end;
  524. ValDecimal:=vs;
  525. end;
  526. Function ValOctal(const S:String):longint;
  527. { Converts an octal string to longint }
  528. var
  529. vs,c : longint;
  530. Begin
  531. vs:=0;
  532. for c:=1 to length(s) do
  533. begin
  534. vs:=vs shl 3;
  535. if s[c] in ['0'..'7'] then
  536. inc(vs,ord(s[c])-ord('0'))
  537. else
  538. begin
  539. Message1(asmr_e_error_converting_octal,s);
  540. ValOctal:=0;
  541. exit;
  542. end;
  543. end;
  544. ValOctal:=vs;
  545. end;
  546. Function ValBinary(const S:String):longint;
  547. { Converts a binary string to longint }
  548. var
  549. vs,c : longint;
  550. Begin
  551. vs:=0;
  552. for c:=1 to length(s) do
  553. begin
  554. vs:=vs shl 1;
  555. if s[c] in ['0'..'1'] then
  556. inc(vs,ord(s[c])-ord('0'))
  557. else
  558. begin
  559. Message1(asmr_e_error_converting_binary,s);
  560. ValBinary:=0;
  561. exit;
  562. end;
  563. end;
  564. ValBinary:=vs;
  565. end;
  566. Function ValHexadecimal(const S:String):longint;
  567. { Converts a binary string to longint }
  568. var
  569. vs,c : longint;
  570. Begin
  571. vs:=0;
  572. for c:=1 to length(s) do
  573. begin
  574. vs:=vs shl 4;
  575. case s[c] of
  576. '0'..'9' :
  577. inc(vs,ord(s[c])-ord('0'));
  578. 'A'..'F' :
  579. inc(vs,ord(s[c])-ord('A')+10);
  580. 'a'..'f' :
  581. inc(vs,ord(s[c])-ord('a')+10);
  582. else
  583. begin
  584. Message1(asmr_e_error_converting_hexadecimal,s);
  585. ValHexadecimal:=0;
  586. exit;
  587. end;
  588. end;
  589. end;
  590. ValHexadecimal:=vs;
  591. end;
  592. Function PadZero(Var s: String; n: byte): Boolean;
  593. Begin
  594. PadZero:=TRUE;
  595. { Do some error checking first }
  596. if Length(s) = n then
  597. exit
  598. else
  599. if Length(s) > n then
  600. Begin
  601. PadZero:=FALSE;
  602. delete(s,n+1,length(s));
  603. exit;
  604. end
  605. else
  606. PadZero:=TRUE;
  607. { Fill it up with the specified character }
  608. fillchar(s[length(s)+1],n-1,#0);
  609. s[0]:=chr(n);
  610. end;
  611. {****************************************************************************
  612. TOperand
  613. ****************************************************************************}
  614. constructor TOperand.Create;
  615. begin
  616. size:=OS_NO;
  617. hastype:=false;
  618. hasvar:=false;
  619. FillChar(Opr,sizeof(Opr),0);
  620. end;
  621. destructor TOperand.destroy;
  622. begin
  623. end;
  624. Procedure TOperand.SetSize(_size:longint;force:boolean);
  625. begin
  626. if force or
  627. ((size = OS_NO) and (_size<=extended_size)) then
  628. Begin
  629. case _size of
  630. 1 : size:=OS_8;
  631. 2 : size:=OS_16{ could be S_IS};
  632. 4 : size:=OS_32{ could be S_IL or S_FS};
  633. 8 : size:=OS_64{ could be S_D or S_FL};
  634. else
  635. begin
  636. { extended_size can also be 8, resulting in a
  637. duplicate label }
  638. if _size=extended_size then
  639. size:=OS_F80;
  640. end;
  641. end;
  642. end;
  643. end;
  644. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  645. begin
  646. end;
  647. Function TOperand.SetupResult:boolean;
  648. Begin
  649. SetupResult:=false;
  650. { replace by correct offset. }
  651. if (not is_void(current_procinfo.procdef.rettype.def)) then
  652. begin
  653. if (m_tp7 in aktmodeswitches) and
  654. (not paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption)) then
  655. begin
  656. Message(asmr_e_cannot_use_RESULT_here);
  657. exit;
  658. end;
  659. SetupResult:=setupvar('result',false)
  660. end
  661. else
  662. Message(asmr_e_void_function);
  663. end;
  664. Function TOperand.SetupSelf:boolean;
  665. Begin
  666. SetupSelf:=false;
  667. if assigned(current_procinfo.procdef._class) then
  668. SetupSelf:=setupvar('self',false)
  669. else
  670. Message(asmr_e_cannot_use_SELF_outside_a_method);
  671. end;
  672. Function TOperand.SetupOldEBP:boolean;
  673. Begin
  674. SetupOldEBP:=false;
  675. if current_procinfo.procdef.parast.symtablelevel>normal_function_level then
  676. SetupOldEBP:=setupvar('parentframe',false)
  677. else
  678. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  679. end;
  680. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  681. function symtable_has_varsyms(st:tsymtable):boolean;
  682. var
  683. sym : tsym;
  684. begin
  685. result:=false;
  686. sym:=tsym(st.symindex.first);
  687. while assigned(sym) do
  688. begin
  689. if sym.typ=varsym then
  690. begin
  691. result:=true;
  692. exit;
  693. end;
  694. sym:=tsym(sym.indexnext);
  695. end;
  696. end;
  697. { search and sets up the correct fields in the Instr record }
  698. { for the NON-constant identifier passed to the routine. }
  699. { if not found returns FALSE. }
  700. var
  701. sym : tsym;
  702. srsymtable : tsymtable;
  703. harrdef : tarraydef;
  704. l : longint;
  705. Begin
  706. SetupVar:=false;
  707. asmsearchsym(s,sym,srsymtable);
  708. if sym = nil then
  709. exit;
  710. if sym.typ=absolutesym then
  711. begin
  712. if (tabsolutesym(sym).abstyp=tovar) then
  713. sym:=tabsolutesym(sym).ref
  714. else
  715. Message(asmr_e_unsupported_symbol_type);
  716. end;
  717. case sym.typ of
  718. varsym :
  719. begin
  720. { we always assume in asm statements that }
  721. { that the variable is valid. }
  722. tvarsym(sym).varstate:=vs_used;
  723. inc(tvarsym(sym).refs);
  724. case tvarsym(sym).owner.symtabletype of
  725. objectsymtable :
  726. begin
  727. { We return the address of the field, just like Delphi/TP }
  728. opr.typ:=OPR_CONSTANT;
  729. opr.val:=tvarsym(sym).fieldoffset;
  730. hasvar:=true;
  731. SetupVar:=true;
  732. Exit;
  733. end;
  734. globalsymtable,
  735. staticsymtable :
  736. opr.ref.symbol:=objectlibrary.newasmsymboldata(tvarsym(sym).mangledname);
  737. parasymtable,
  738. localsymtable :
  739. begin
  740. if (vo_is_external in tvarsym(sym).varoptions) then
  741. opr.ref.symbol:=objectlibrary.newasmsymboldata(tvarsym(sym).mangledname)
  742. else
  743. begin
  744. opr.typ:=OPR_LOCAL;
  745. if assigned(current_procinfo.parent) and
  746. (tvarsym(sym).owner<>current_procinfo.procdef.localst) and
  747. (tvarsym(sym).owner<>current_procinfo.procdef.parast) and
  748. (current_procinfo.procdef.localst.symtablelevel>normal_function_level) and
  749. symtable_has_varsyms(current_procinfo.procdef.localst) then
  750. message1(asmr_e_local_para_unreachable,s);
  751. opr.localsym:=tvarsym(sym);
  752. opr.localsymofs:=0;
  753. end;
  754. if paramanager.push_addr_param(tvarsym(sym).varspez,tvarsym(sym).vartype.def,current_procinfo.procdef.proccalloption) then
  755. SetSize(pointer_size,false);
  756. end;
  757. end;
  758. case tvarsym(sym).vartype.def.deftype of
  759. orddef,
  760. enumdef,
  761. pointerdef,
  762. floatdef :
  763. SetSize(tvarsym(sym).getsize,false);
  764. arraydef :
  765. begin
  766. { for arrays try to get the element size, take care of
  767. multiple indexes }
  768. harrdef:=tarraydef(tvarsym(sym).vartype.def);
  769. while assigned(harrdef.elementtype.def) and
  770. (harrdef.elementtype.def.deftype=arraydef) do
  771. harrdef:=tarraydef(harrdef.elementtype.def);
  772. SetSize(harrdef.elesize,false);
  773. end;
  774. end;
  775. hasvar:=true;
  776. SetupVar:=true;
  777. Exit;
  778. end;
  779. typedconstsym :
  780. begin
  781. opr.ref.symbol:=objectlibrary.newasmsymboldata(ttypedconstsym(sym).mangledname);
  782. case ttypedconstsym(sym).typedconsttype.def.deftype of
  783. orddef,
  784. enumdef,
  785. pointerdef,
  786. floatdef :
  787. SetSize(ttypedconstsym(sym).getsize,false);
  788. arraydef :
  789. begin
  790. { for arrays try to get the element size, take care of
  791. multiple indexes }
  792. harrdef:=tarraydef(ttypedconstsym(sym).typedconsttype.def);
  793. while assigned(harrdef.elementtype.def) and
  794. (harrdef.elementtype.def.deftype=arraydef) do
  795. harrdef:=tarraydef(harrdef.elementtype.def);
  796. SetSize(harrdef.elesize,false);
  797. end;
  798. end;
  799. hasvar:=true;
  800. SetupVar:=true;
  801. Exit;
  802. end;
  803. constsym :
  804. begin
  805. if tconstsym(sym).consttyp in [constint,constchar,constbool] then
  806. begin
  807. opr.typ:=OPR_CONSTANT;
  808. opr.val:=tconstsym(sym).value.valueord;
  809. SetupVar:=true;
  810. Exit;
  811. end;
  812. end;
  813. typesym :
  814. begin
  815. if ttypesym(sym).restype.def.deftype in [recorddef,objectdef] then
  816. begin
  817. opr.typ:=OPR_CONSTANT;
  818. opr.val:=0;
  819. SetupVar:=TRUE;
  820. Exit;
  821. end;
  822. end;
  823. procsym :
  824. begin
  825. if Tprocsym(sym).procdef_count>1 then
  826. Message(asmr_w_calling_overload_func);
  827. l:=opr.ref.offset;
  828. opr.typ:=OPR_SYMBOL;
  829. opr.symbol:=objectlibrary.newasmsymbol(tprocsym(sym).first_procdef.mangledname);
  830. opr.symofs:=l;
  831. hasvar:=true;
  832. SetupVar:=TRUE;
  833. Exit;
  834. end;
  835. else
  836. begin
  837. Message(asmr_e_unsupported_symbol_type);
  838. exit;
  839. end;
  840. end;
  841. end;
  842. { looks for internal names of variables and routines }
  843. Function TOperand.SetupDirectVar(const hs:string): Boolean;
  844. var
  845. p : tasmsymbol;
  846. begin
  847. SetupDirectVar:=false;
  848. p:=objectlibrary.getasmsymbol(hs);
  849. if assigned(p) then
  850. begin
  851. opr.ref.symbol:=p;
  852. hasvar:=true;
  853. SetupDirectVar:=true;
  854. end;
  855. end;
  856. procedure TOperand.InitRef;
  857. {*********************************************************************}
  858. { Description: This routine first check if the opcode is of }
  859. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  860. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  861. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  862. { to point to the default segment. }
  863. {*********************************************************************}
  864. Begin
  865. case opr.typ of
  866. OPR_REFERENCE:
  867. exit;
  868. OPR_NONE: ;
  869. else
  870. Message(asmr_e_invalid_operand_type);
  871. end;
  872. opr.typ := OPR_REFERENCE;
  873. Fillchar(opr.ref,sizeof(treference),0);
  874. end;
  875. procedure TOperand.BuildOperand;
  876. begin
  877. abstract;
  878. end;
  879. {****************************************************************************
  880. TInstruction
  881. ****************************************************************************}
  882. constructor TInstruction.create;
  883. Begin
  884. Opcode:=A_NONE;
  885. Condition:=C_NONE;
  886. Ops:=0;
  887. InitOperands;
  888. Labeled:=false;
  889. end;
  890. destructor TInstruction.destroy;
  891. var
  892. i : longint;
  893. Begin
  894. for i:=1 to 3 do
  895. Operands[i].free;
  896. end;
  897. procedure TInstruction.InitOperands;
  898. var
  899. i : longint;
  900. begin
  901. for i:=1 to 3 do
  902. Operands[i].create;
  903. end;
  904. Procedure TInstruction.Swapoperands;
  905. Var
  906. p : toperand;
  907. Begin
  908. case Ops of
  909. 2 :
  910. begin
  911. p:=Operands[1];
  912. Operands[1]:=Operands[2];
  913. Operands[2]:=p;
  914. end;
  915. 3 :
  916. begin
  917. p:=Operands[1];
  918. Operands[1]:=Operands[3];
  919. Operands[3]:=p;
  920. end;
  921. end;
  922. end;
  923. procedure TInstruction.ConcatInstruction(p:TAAsmOutput);
  924. begin
  925. abstract;
  926. end;
  927. procedure TInstruction.BuildOpcode;
  928. begin
  929. abstract;
  930. end;
  931. {***************************************************************************
  932. TLocalLabel
  933. ***************************************************************************}
  934. constructor TLocalLabel.create(const n:string);
  935. begin
  936. inherited CreateName(n);
  937. lab:=nil;
  938. emitted:=false;
  939. end;
  940. function TLocalLabel.Gettasmlabel:tasmlabel;
  941. begin
  942. if not assigned(lab) then
  943. begin
  944. objectlibrary.getlabel(lab);
  945. { this label is forced to be used so it's always written }
  946. lab.increfs;
  947. end;
  948. Gettasmlabel:=lab;
  949. end;
  950. {***************************************************************************
  951. TLocalLabelList
  952. ***************************************************************************}
  953. procedure LocalLabelEmitted(p:tnamedindexitem;arg:pointer);
  954. begin
  955. if not TLocalLabel(p).emitted then
  956. Message1(asmr_e_unknown_label_identifier,p.name);
  957. end;
  958. procedure TLocalLabelList.CheckEmitted;
  959. begin
  960. ForEach_Static({$ifdef FPCPROCVAR}@{$endif}LocalLabelEmitted,nil)
  961. end;
  962. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  963. var
  964. lab : TLocalLabel;
  965. Begin
  966. CreateLocalLabel:=true;
  967. { Check if it already is defined }
  968. lab:=TLocalLabel(LocalLabellist.Search(s));
  969. if not assigned(lab) then
  970. begin
  971. lab:=TLocalLabel.Create(s);
  972. LocalLabellist.Insert(lab);
  973. end;
  974. { set emitted flag and check for dup syms }
  975. if emit then
  976. begin
  977. if lab.Emitted then
  978. begin
  979. Message1(asmr_e_dup_local_sym,lab.Name);
  980. CreateLocalLabel:=false;
  981. end;
  982. lab.Emitted:=true;
  983. end;
  984. hl:=lab.Gettasmlabel;
  985. end;
  986. {****************************************************************************
  987. Symbol table helper routines
  988. ****************************************************************************}
  989. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  990. var
  991. i : integer;
  992. begin
  993. i:=pos('.',s);
  994. { allow unit.identifier }
  995. if i>0 then
  996. begin
  997. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  998. if assigned(srsym) then
  999. begin
  1000. if (srsym.typ=unitsym) and
  1001. (srsym.owner.unitid=0) then
  1002. srsym:=searchsymonlyin(tunitsym(srsym).unitsymtable,Copy(s,i+1,255))
  1003. else
  1004. srsym:=nil;
  1005. end;
  1006. end
  1007. else
  1008. searchsym(s,srsym,srsymtable);
  1009. end;
  1010. Function SearchType(const hs:string;var size:longint): Boolean;
  1011. var
  1012. srsym : tsym;
  1013. srsymtable : tsymtable;
  1014. begin
  1015. result:=false;
  1016. size:=0;
  1017. asmsearchsym(hs,srsym,srsymtable);
  1018. if assigned(srsym) and
  1019. (srsym.typ=typesym) then
  1020. begin
  1021. size:=ttypesym(srsym).restype.def.size;
  1022. result:=true;
  1023. end;
  1024. end;
  1025. Function SearchRecordType(const s:string): boolean;
  1026. var
  1027. srsym : tsym;
  1028. srsymtable : tsymtable;
  1029. Begin
  1030. SearchRecordType:=false;
  1031. { Check the constants in symtable }
  1032. asmsearchsym(s,srsym,srsymtable);
  1033. if srsym <> nil then
  1034. Begin
  1035. case srsym.typ of
  1036. typesym :
  1037. begin
  1038. if ttypesym(srsym).restype.def.deftype in [recorddef,objectdef] then
  1039. begin
  1040. SearchRecordType:=true;
  1041. exit;
  1042. end;
  1043. end;
  1044. end;
  1045. end;
  1046. end;
  1047. Function SearchIConstant(const s:string; var l:longint): boolean;
  1048. {**********************************************************************}
  1049. { Description: Searches for a CONSTANT of name s in either the local }
  1050. { symbol list, then in the global symbol list, and returns the value }
  1051. { of that constant in l. Returns TRUE if successfull, if not found, }
  1052. { or if the constant is not of correct type, then returns FALSE }
  1053. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1054. { respectively. }
  1055. {**********************************************************************}
  1056. var
  1057. srsym : tsym;
  1058. srsymtable : tsymtable;
  1059. Begin
  1060. SearchIConstant:=false;
  1061. { check for TRUE or FALSE reserved words first }
  1062. if s = 'TRUE' then
  1063. Begin
  1064. SearchIConstant:=TRUE;
  1065. l:=1;
  1066. exit;
  1067. end;
  1068. if s = 'FALSE' then
  1069. Begin
  1070. SearchIConstant:=TRUE;
  1071. l:=0;
  1072. exit;
  1073. end;
  1074. { Check the constants in symtable }
  1075. asmsearchsym(s,srsym,srsymtable);
  1076. if srsym <> nil then
  1077. Begin
  1078. case srsym.typ of
  1079. constsym :
  1080. begin
  1081. if (tconstsym(srsym).consttyp in [constord,constint,constchar,constbool]) then
  1082. Begin
  1083. l:=tconstsym(srsym).value.valueord;
  1084. SearchIConstant:=TRUE;
  1085. exit;
  1086. end;
  1087. end;
  1088. enumsym:
  1089. Begin
  1090. l:=tenumsym(srsym).value;
  1091. SearchIConstant:=TRUE;
  1092. exit;
  1093. end;
  1094. end;
  1095. end;
  1096. end;
  1097. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  1098. { search and returns the offset and size of records/objects of the base }
  1099. { with field name setup in field. }
  1100. { returns FALSE if not found. }
  1101. { used when base is a variable or a typed constant name. }
  1102. var
  1103. st : tsymtable;
  1104. harrdef : tarraydef;
  1105. sym : tsym;
  1106. srsymtable : tsymtable;
  1107. i : longint;
  1108. base : string;
  1109. Begin
  1110. GetRecordOffsetSize:=FALSE;
  1111. Offset:=0;
  1112. Size:=0;
  1113. i:=pos('.',s);
  1114. if i=0 then
  1115. i:=255;
  1116. base:=Copy(s,1,i-1);
  1117. delete(s,1,i);
  1118. if base='SELF' then
  1119. st:=current_procinfo.procdef._class.symtable
  1120. else
  1121. begin
  1122. asmsearchsym(base,sym,srsymtable);
  1123. st:=nil;
  1124. { we can start with a var,type,typedconst }
  1125. case sym.typ of
  1126. varsym :
  1127. begin
  1128. case tvarsym(sym).vartype.def.deftype of
  1129. recorddef :
  1130. st:=trecorddef(tvarsym(sym).vartype.def).symtable;
  1131. objectdef :
  1132. st:=tobjectdef(tvarsym(sym).vartype.def).symtable;
  1133. end;
  1134. end;
  1135. typesym :
  1136. begin
  1137. case ttypesym(sym).restype.def.deftype of
  1138. recorddef :
  1139. st:=trecorddef(ttypesym(sym).restype.def).symtable;
  1140. objectdef :
  1141. st:=tobjectdef(ttypesym(sym).restype.def).symtable;
  1142. end;
  1143. end;
  1144. typedconstsym :
  1145. begin
  1146. case ttypedconstsym(sym).typedconsttype.def.deftype of
  1147. recorddef :
  1148. st:=trecorddef(ttypedconstsym(sym).typedconsttype.def).symtable;
  1149. objectdef :
  1150. st:=tobjectdef(ttypedconstsym(sym).typedconsttype.def).symtable;
  1151. end;
  1152. end;
  1153. end;
  1154. end;
  1155. { now walk all recordsymtables }
  1156. while assigned(st) and (s<>'') do
  1157. begin
  1158. { load next field in base }
  1159. i:=pos('.',s);
  1160. if i=0 then
  1161. i:=255;
  1162. base:=Copy(s,1,i-1);
  1163. delete(s,1,i);
  1164. if st.symtabletype=objectsymtable then
  1165. sym:=search_class_member(tobjectdef(st.defowner),base)
  1166. else
  1167. sym:=tsym(st.search(base));
  1168. if not assigned(sym) then
  1169. begin
  1170. GetRecordOffsetSize:=false;
  1171. exit;
  1172. end;
  1173. st:=nil;
  1174. case sym.typ of
  1175. varsym :
  1176. begin
  1177. inc(Offset,tvarsym(sym).fieldoffset);
  1178. Size:=tvarsym(sym).getsize;
  1179. case tvarsym(sym).vartype.def.deftype of
  1180. arraydef :
  1181. begin
  1182. { for arrays try to get the element size, take care of
  1183. multiple indexes }
  1184. harrdef:=tarraydef(tvarsym(sym).vartype.def);
  1185. while assigned(harrdef.elementtype.def) and
  1186. (harrdef.elementtype.def.deftype=arraydef) do
  1187. harrdef:=tarraydef(harrdef.elementtype.def);
  1188. size:=harrdef.elesize;
  1189. end;
  1190. recorddef :
  1191. st:=trecorddef(tvarsym(sym).vartype.def).symtable;
  1192. objectdef :
  1193. st:=tobjectdef(tvarsym(sym).vartype.def).symtable;
  1194. end;
  1195. end;
  1196. end;
  1197. end;
  1198. GetRecordOffsetSize:=(s='');
  1199. end;
  1200. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1201. var
  1202. sym : tsym;
  1203. srsymtable : tsymtable;
  1204. hs : string;
  1205. Begin
  1206. hl:=nil;
  1207. SearchLabel:=false;
  1208. { Check for pascal labels, which are case insensetive }
  1209. hs:=upper(s);
  1210. asmsearchsym(hs,sym,srsymtable);
  1211. if sym=nil then
  1212. exit;
  1213. case sym.typ of
  1214. labelsym :
  1215. begin
  1216. hl:=tlabelsym(sym).lab;
  1217. if emit then
  1218. tlabelsym(sym).defined:=true
  1219. else
  1220. tlabelsym(sym).used:=true;
  1221. SearchLabel:=true;
  1222. exit;
  1223. end;
  1224. end;
  1225. end;
  1226. {*************************************************************************}
  1227. { Instruction Generation Utilities }
  1228. {*************************************************************************}
  1229. Procedure ConcatString(p : TAAsmoutput;s:string);
  1230. {*********************************************************************}
  1231. { PROCEDURE ConcatString(s:string); }
  1232. { Description: This routine adds the character chain pointed to in }
  1233. { s to the instruction linked list. }
  1234. {*********************************************************************}
  1235. Var
  1236. pc: PChar;
  1237. Begin
  1238. getmem(pc,length(s)+1);
  1239. p.concat(Tai_string.Create_length_pchar(strpcopy(pc,s),length(s)));
  1240. end;
  1241. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  1242. {*********************************************************************}
  1243. { PROCEDURE ConcatPasString(s:string); }
  1244. { Description: This routine adds the character chain pointed to in }
  1245. { s to the instruction linked list, contrary to ConcatString it }
  1246. { uses a pascal style string, so it conserves null characters. }
  1247. {*********************************************************************}
  1248. Begin
  1249. p.concat(Tai_string.Create(s));
  1250. end;
  1251. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  1252. {*********************************************************************}
  1253. { PROCEDURE ConcatDirect(s:string) }
  1254. { Description: This routine output the string directly to the asm }
  1255. { output, it is only sed when writing special labels in AT&T mode, }
  1256. { and should not be used without due consideration, since it may }
  1257. { cause problems. }
  1258. {*********************************************************************}
  1259. Var
  1260. pc: PChar;
  1261. Begin
  1262. getmem(pc,length(s)+1);
  1263. p.concat(Tai_direct.Create(strpcopy(pc,s)));
  1264. end;
  1265. Procedure ConcatConstant(p: TAAsmoutput; value: longint; maxvalue: longint);
  1266. {*********************************************************************}
  1267. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1268. { Description: This routine adds the value constant to the current }
  1269. { instruction linked list. }
  1270. { maxvalue -> indicates the size of the data to initialize: }
  1271. { $ff -> create a byte node. }
  1272. { $ffff -> create a word node. }
  1273. { $ffffffff -> create a dword node. }
  1274. {*********************************************************************}
  1275. Begin
  1276. if (maxvalue <> longint($ffffffff)) and (value > maxvalue) then
  1277. Begin
  1278. Message(asmr_e_constant_out_of_bounds);
  1279. { assuming a value of maxvalue }
  1280. value:=maxvalue;
  1281. end;
  1282. if maxvalue = $ff then
  1283. p.concat(Tai_const.Create_8bit(byte(value)))
  1284. else
  1285. if maxvalue = $ffff then
  1286. p.concat(Tai_const.Create_16bit(word(value)))
  1287. else
  1288. if maxvalue = longint($ffffffff) then
  1289. p.concat(Tai_const.Create_32bit(longint(value)));
  1290. end;
  1291. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:longint);
  1292. begin
  1293. p.concat(Tai_const_symbol.Createname_offset(sym,l));
  1294. end;
  1295. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  1296. {***********************************************************************}
  1297. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1298. { Description: This routine adds the value constant to the current }
  1299. { instruction linked list. }
  1300. { real_typ -> indicates the type of the real data to initialize: }
  1301. { s32real -> create a single node. }
  1302. { s64real -> create a double node. }
  1303. { s80real -> create an extended node. }
  1304. { s64bit -> create a comp node. }
  1305. { f32bit -> create a fixed node. (not used normally) }
  1306. {***********************************************************************}
  1307. Begin
  1308. case real_typ of
  1309. s32real : p.concat(Tai_real_32bit.Create(value));
  1310. s64real : p.concat(Tai_real_64bit.Create(value));
  1311. s80real : p.concat(Tai_real_80bit.Create(value));
  1312. s64comp : p.concat(Tai_comp_64bit.Create(value));
  1313. end;
  1314. end;
  1315. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  1316. {*********************************************************************}
  1317. { PROCEDURE ConcatLabel }
  1318. { Description: This routine either emits a label or a labeled }
  1319. { instruction to the linked list of instructions. }
  1320. {*********************************************************************}
  1321. begin
  1322. p.concat(Tai_label.Create(l));
  1323. end;
  1324. procedure ConcatAlign(p:TAAsmoutput;l:longint);
  1325. {*********************************************************************}
  1326. { PROCEDURE ConcatPublic }
  1327. { Description: This routine emits an global definition to the }
  1328. { linked list of instructions.(used by AT&T styled asm) }
  1329. {*********************************************************************}
  1330. begin
  1331. p.concat(Tai_align.Create(l));
  1332. end;
  1333. procedure ConcatPublic(p:TAAsmoutput;const s : string);
  1334. {*********************************************************************}
  1335. { PROCEDURE ConcatPublic }
  1336. { Description: This routine emits an global definition to the }
  1337. { linked list of instructions.(used by AT&T styled asm) }
  1338. {*********************************************************************}
  1339. begin
  1340. p.concat(Tai_symbol.Createname_global(s,0));
  1341. end;
  1342. procedure ConcatLocal(p:TAAsmoutput;const s : string);
  1343. {*********************************************************************}
  1344. { PROCEDURE ConcatLocal }
  1345. { Description: This routine emits an local definition to the }
  1346. { linked list of instructions. }
  1347. {*********************************************************************}
  1348. begin
  1349. p.concat(Tai_symbol.Createname(s,0));
  1350. end;
  1351. Procedure ConcatGlobalBss(const s : string;size : longint);
  1352. {*********************************************************************}
  1353. { PROCEDURE ConcatGlobalBss }
  1354. { Description: This routine emits an global datablock to the }
  1355. { linked list of instructions. }
  1356. {*********************************************************************}
  1357. begin
  1358. bssSegment.concat(Tai_datablock.Create_global(s,size));
  1359. end;
  1360. Procedure ConcatLocalBss(const s : string;size : longint);
  1361. {*********************************************************************}
  1362. { PROCEDURE ConcatLocalBss }
  1363. { Description: This routine emits a local datablcok to the }
  1364. { linked list of instructions. }
  1365. {*********************************************************************}
  1366. begin
  1367. bssSegment.concat(Tai_datablock.Create(s,size));
  1368. end;
  1369. end.
  1370. {
  1371. $Log$
  1372. Revision 1.71 2003-10-23 17:19:11 peter
  1373. * SearchType returns also the size
  1374. Revision 1.70 2003/10/08 19:39:58 peter
  1375. * allow access to parent locals when the currnet localst has no
  1376. varsyms
  1377. Revision 1.69 2003/10/01 20:34:49 peter
  1378. * procinfo unit contains tprocinfo
  1379. * cginfo renamed to cgbase
  1380. * moved cgmessage to verbose
  1381. * fixed ppc and sparc compiles
  1382. Revision 1.68 2003/09/25 14:57:36 peter
  1383. * fix check for unreachable locals
  1384. Revision 1.67 2003/09/23 17:56:06 peter
  1385. * locals and paras are allocated in the code generation
  1386. * tvarsym.localloc contains the location of para/local when
  1387. generating code for the current procedure
  1388. Revision 1.66 2003/09/16 16:17:01 peter
  1389. * varspez in calls to push_addr_param
  1390. Revision 1.65 2003/09/03 15:55:01 peter
  1391. * NEWRA branch merged
  1392. Revision 1.64.2.1 2003/08/27 19:55:54 peter
  1393. * first tregister patch
  1394. Revision 1.64 2003/06/13 21:19:31 peter
  1395. * current_procdef removed, use current_procinfo.procdef instead
  1396. Revision 1.63 2003/06/06 14:43:29 peter
  1397. * absolutesym support
  1398. Revision 1.62 2003/05/30 23:57:08 peter
  1399. * more sparc cleanup
  1400. * accumulator removed, splitted in function_return_reg (called) and
  1401. function_result_reg (caller)
  1402. Revision 1.61 2003/05/25 08:55:49 peter
  1403. * load result using hidden parameter
  1404. Revision 1.60 2003/05/15 18:58:53 peter
  1405. * removed selfpointer_offset, vmtpointer_offset
  1406. * tvarsym.adjusted_address
  1407. * address in localsymtable is now in the real direction
  1408. * removed some obsolete globals
  1409. Revision 1.59 2003/05/12 17:22:00 jonas
  1410. * fixed (last?) remaining -tvarsym(X).address to
  1411. tg.direction*tvarsym(X).address...
  1412. Revision 1.58 2003/04/27 11:21:34 peter
  1413. * aktprocdef renamed to current_procinfo.procdef
  1414. * procinfo renamed to current_procinfo
  1415. * procinfo will now be stored in current_module so it can be
  1416. cleaned up properly
  1417. * gen_main_procsym changed to create_main_proc and release_main_proc
  1418. to also generate a tprocinfo structure
  1419. * fixed unit implicit initfinal
  1420. Revision 1.57 2003/04/27 07:29:51 peter
  1421. * current_procinfo.procdef cleanup, current_procinfo.procdef is now always nil when parsing
  1422. a new procdef declaration
  1423. * aktprocsym removed
  1424. * lexlevel removed, use symtable.symtablelevel instead
  1425. * implicit init/final code uses the normal genentry/genexit
  1426. * funcret state checking updated for new funcret handling
  1427. Revision 1.56 2003/04/25 20:59:34 peter
  1428. * removed funcretn,funcretsym, function result is now in varsym
  1429. and aliases for result and function name are added using absolutesym
  1430. * vs_hidden parameter for funcret passed in parameter
  1431. * vs_hidden fixes
  1432. * writenode changed to printnode and released from extdebug
  1433. * -vp option added to generate a tree.log with the nodetree
  1434. * nicer printnode for statements, callnode
  1435. Revision 1.55 2003/04/06 21:11:23 olle
  1436. * changed newasmsymbol to newasmsymboldata for data symbols
  1437. Revision 1.54 2003/03/28 19:16:57 peter
  1438. * generic constructor working for i386
  1439. * remove fixed self register
  1440. * esi added as address register for i386
  1441. Revision 1.53 2003/02/19 22:00:14 daniel
  1442. * Code generator converted to new register notation
  1443. - Horribily outdated todo.txt removed
  1444. Revision 1.52 2003/01/08 18:43:56 daniel
  1445. * Tregister changed into a record
  1446. Revision 1.51 2002/12/14 15:02:03 carl
  1447. * maxoperands -> max_operands (for portability in rautils.pas)
  1448. * fix some range-check errors with loadconst
  1449. + add ncgadd unit to m68k
  1450. * some bugfix of a_param_reg with LOC_CREFERENCE
  1451. Revision 1.50 2002/11/25 17:43:23 peter
  1452. * splitted defbase in defutil,symutil,defcmp
  1453. * merged isconvertable and is_equal into compare_defs(_ext)
  1454. * made operator search faster by walking the list only once
  1455. Revision 1.49 2002/11/22 22:48:10 carl
  1456. * memory optimization with tconstsym (1.5%)
  1457. Revision 1.48 2002/11/18 17:31:59 peter
  1458. * pass proccalloption to ret_in_xxx and push_xxx functions
  1459. Revision 1.47 2002/11/15 16:29:31 peter
  1460. * made tasmsymbol.refs private (merged)
  1461. Revision 1.46 2002/09/03 16:26:27 daniel
  1462. * Make Tprocdef.defs protected
  1463. Revision 1.45 2002/08/25 19:25:20 peter
  1464. * sym.insert_in_data removed
  1465. * symtable.insertvardata/insertconstdata added
  1466. * removed insert_in_data call from symtable.insert, it needs to be
  1467. called separatly. This allows to deref the address calculation
  1468. * procedures now calculate the parast addresses after the procedure
  1469. directives are parsed. This fixes the cdecl parast problem
  1470. * push_addr_param has an extra argument that specifies if cdecl is used
  1471. or not
  1472. Revision 1.44 2002/08/17 09:23:41 florian
  1473. * first part of procinfo rewrite
  1474. Revision 1.43 2002/08/16 14:24:59 carl
  1475. * issameref() to test if two references are the same (then emit no opcodes)
  1476. + ret_in_reg to replace ret_in_acc
  1477. (fix some register allocation bugs at the same time)
  1478. + save_std_register now has an extra parameter which is the
  1479. usedinproc registers
  1480. Revision 1.42 2002/08/13 18:01:52 carl
  1481. * rename swatoperands to swapoperands
  1482. + m68k first compilable version (still needs a lot of testing):
  1483. assembler generator, system information , inline
  1484. assembler reader.
  1485. Revision 1.41 2002/08/12 15:08:40 carl
  1486. + stab register indexes for powerpc (moved from gdb to cpubase)
  1487. + tprocessor enumeration moved to cpuinfo
  1488. + linker in target_info is now a class
  1489. * many many updates for m68k (will soon start to compile)
  1490. - removed some ifdef or correct them for correct cpu
  1491. Revision 1.40 2002/08/11 14:32:27 peter
  1492. * renamed current_library to objectlibrary
  1493. Revision 1.39 2002/08/11 13:24:13 peter
  1494. * saving of asmsymbols in ppu supported
  1495. * asmsymbollist global is removed and moved into a new class
  1496. tasmlibrarydata that will hold the info of a .a file which
  1497. corresponds with a single module. Added librarydata to tmodule
  1498. to keep the library info stored for the module. In the future the
  1499. objectfiles will also be stored to the tasmlibrarydata class
  1500. * all getlabel/newasmsymbol and friends are moved to the new class
  1501. Revision 1.38 2002/07/20 11:57:57 florian
  1502. * types.pas renamed to defbase.pas because D6 contains a types
  1503. unit so this would conflicts if D6 programms are compiled
  1504. + Willamette/SSE2 instructions to assembler added
  1505. Revision 1.37 2002/07/11 14:41:28 florian
  1506. * start of the new generic parameter handling
  1507. Revision 1.36 2002/07/01 18:46:25 peter
  1508. * internal linker
  1509. * reorganized aasm layer
  1510. Revision 1.35 2002/05/18 13:34:17 peter
  1511. * readded missing revisions
  1512. Revision 1.34 2002/05/16 19:46:44 carl
  1513. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1514. + try to fix temp allocation (still in ifdef)
  1515. + generic constructor calls
  1516. + start of tassembler / tmodulebase class cleanup
  1517. Revision 1.31 2002/05/12 16:53:10 peter
  1518. * moved entry and exitcode to ncgutil and cgobj
  1519. * foreach gets extra argument for passing local data to the
  1520. iterator function
  1521. * -CR checks also class typecasts at runtime by changing them
  1522. into as
  1523. * fixed compiler to cycle with the -CR option
  1524. * fixed stabs with elf writer, finally the global variables can
  1525. be watched
  1526. * removed a lot of routines from cga unit and replaced them by
  1527. calls to cgobj
  1528. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1529. u32bit then the other is typecasted also to u32bit without giving
  1530. a rangecheck warning/error.
  1531. * fixed pascal calling method with reversing also the high tree in
  1532. the parast, detected by tcalcst3 test
  1533. Revision 1.30 2002/04/20 21:32:24 carl
  1534. + generic FPC_CHECKPOINTER
  1535. + first parameter offset in stack now portable
  1536. * rename some constants
  1537. + move some cpu stuff to other units
  1538. - remove unused constents
  1539. * fix stacksize for some targets
  1540. * fix generic size problems which depend now on EXTEND_SIZE constant
  1541. Revision 1.29 2002/04/15 19:02:35 carl
  1542. + target_info.size_of_pointer -> pointer_Size
  1543. Revision 1.28 2002/04/02 17:11:29 peter
  1544. * tlocation,treference update
  1545. * LOC_CONSTANT added for better constant handling
  1546. * secondadd splitted in multiple routines
  1547. * location_force_reg added for loading a location to a register
  1548. of a specified size
  1549. * secondassignment parses now first the right and then the left node
  1550. (this is compatible with Kylix). This saves a lot of push/pop especially
  1551. with string operations
  1552. * adapted some routines to use the new cg methods
  1553. Revision 1.27 2002/01/29 21:32:03 peter
  1554. * allow accessing locals in other lexlevel when the current assembler
  1555. routine doesn't have locals.
  1556. Revision 1.26 2002/01/24 18:25:50 peter
  1557. * implicit result variable generation for assembler routines
  1558. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  1559. }