rautils.pas 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  1. {
  2. $Id$
  3. Copyright (c) 1998 Carl Eric Codere
  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. {*************************************************************************}
  20. { This unit implements some objects as well as utilities which will be }
  21. { used by all inline assembler parsers (non-processor specific). }
  22. { }
  23. { Main routines/objects herein: }
  24. { o Object TExprParse is a simple expression parser to resolve assembler }
  25. { expressions. (Based generally on some code by Thai Tran from SWAG). }
  26. { o Object TInstruction is a simple object used for instructions }
  27. { o Record TOperand is a simple record used to store information on }
  28. { each operand. }
  29. { o String conversion routines from octal,binary and hex to decimal. }
  30. { o A linked list object/record for local labels }
  31. { o Routines for retrieving symbols (local and global) }
  32. { o Object for a linked list of strings (with duplicate strings not }
  33. { allowed). }
  34. { o Non-processor dependant routines for adding instructions to the }
  35. { instruction list. }
  36. {*************************************************************************}
  37. {--------------------------------------------------------------------}
  38. { LEFT TO DO: }
  39. { o Fix the remaining bugs in the expression parser, such as with }
  40. { 4+-3 }
  41. { o Add support for local typed constants search. }
  42. { o Add support for private/protected fields in method assembler }
  43. { routines. }
  44. {--------------------------------------------------------------------}
  45. Interface
  46. Uses
  47. globtype,systems,
  48. symtable,aasm,hcodegen,verbose,globals,files,strings,
  49. cobjects,
  50. {$ifdef i386}
  51. {$ifdef Ag386Bin}
  52. i386base;
  53. {$else}
  54. i386;
  55. {$endif}
  56. {$endif}
  57. {$ifdef m68k}
  58. m68k;
  59. {$endif}
  60. Const
  61. RPNMax = 10; { I think you only need 4, but just to be safe }
  62. OpMax = 25;
  63. maxoperands = 3; { Maximum operands for assembler instructions }
  64. Type
  65. {---------------------------------------------------------------------}
  66. { Label Management types }
  67. {---------------------------------------------------------------------}
  68. PAsmLabel = ^TAsmLabel;
  69. { Each local label has this structure associated with it }
  70. TAsmLabel = record
  71. name: PString; { pointer to a pascal string name of label }
  72. lab: PLabel; { pointer to a label as defined in FPC }
  73. emitted: boolean; { as the label itself been emitted ? }
  74. next: PAsmLabel; { next node }
  75. end;
  76. TAsmLabelList = Object
  77. public
  78. First: PAsmLabel;
  79. Constructor Init;
  80. Destructor Done;
  81. Procedure Insert(s:string; lab: PLabel; emitted: boolean);
  82. Function Search(const s: string): PAsmLabel;
  83. private
  84. Last: PAsmLabel;
  85. Function NewPasStr(s:string): PString;
  86. end;
  87. {---------------------------------------------------------------------}
  88. { Instruction management types }
  89. {---------------------------------------------------------------------}
  90. toperandtype = (OPR_NONE,OPR_REFERENCE,OPR_CONSTANT,OPR_REGISTER,OPR_LABINSTR,
  91. OPR_REGLIST,OPR_SYMBOL);
  92. { When the TReference field isintvalue = TRUE }
  93. { then offset points to an ABSOLUTE address }
  94. { otherwise isintvalue should always be false }
  95. { Special cases: }
  96. { For the M68k Target, size is UNUSED, the }
  97. { opcode determines the size of the }
  98. { instruction. }
  99. { DIVS/DIVU/MULS/MULU of the form dn,dn:dn }
  100. { is stored as three operands!! }
  101. { Each instruction operand can be of this type }
  102. TOperand = record
  103. size: topsize;
  104. opinfo: longint; { ao_xxxx flags }
  105. overriden : boolean; { indicates if the opcode has been overriden }
  106. { by a pseudo-opcode such as DWORD PTR }
  107. case operandtype:toperandtype of
  108. { the size of the opr_none field should be at least equal to each }
  109. { other field as to facilitate initialization. }
  110. OPR_NONE: (l: array[1..sizeof(treference)] of byte);
  111. OPR_REFERENCE: (ref:treference);
  112. OPR_CONSTANT: (val: longint);
  113. OPR_REGISTER: (reg:tregister);
  114. OPR_LABINSTR: (hl: plabel);
  115. { Register list such as in the movem instruction }
  116. OPR_REGLIST: (list: set of tregister);
  117. OPR_SYMBOL : (symbol:pasmsymbol);
  118. end;
  119. TInstruction = object
  120. public
  121. operands: array[1..maxoperands] of TOperand;
  122. { if numops = zero, a size may still be valid in operands[1] }
  123. { it still should be checked. }
  124. numops: byte;
  125. { set to TRUE if the instruction is labeled. }
  126. labeled: boolean;
  127. { This is used for instructions such A_CMPSB... etc, to determine }
  128. { the size of the instruction. }
  129. stropsize: topsize;
  130. procedure init;
  131. procedure done;
  132. { sets up the prefix field with the instruction pointed to in s }
  133. procedure addprefix(tok: tasmop);
  134. { sets up the instruction with the instruction pointed to in s }
  135. procedure addinstr(tok: tasmop);
  136. { get the current instruction of this object }
  137. function getinstruction: tasmop;
  138. { get the current prefix of this instruction }
  139. function getprefix: tasmop;
  140. private
  141. prefix: tasmop;
  142. instruction: tasmop;
  143. end;
  144. {---------------------------------------------------------------------}
  145. { Expression parser types }
  146. {---------------------------------------------------------------------}
  147. { expression parser error codes }
  148. texpr_error =
  149. (zero_divide, { divide by zero. }
  150. stack_overflow, { stack overflow. }
  151. stack_underflow, { stack underflow. }
  152. invalid_number, { invalid conversion }
  153. invalid_op); { invalid operator }
  154. TExprOperator = record
  155. ch: char; { operator }
  156. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  157. end;
  158. String15 = String[15];
  159. {**********************************************************************}
  160. { The following operators are supported: }
  161. { '+' : addition }
  162. { '-' : subtraction }
  163. { '*' : multiplication }
  164. { '/' : modulo division }
  165. { '^' : exclusive or }
  166. { '<' : shift left }
  167. { '>' : shift right }
  168. { '&' : bitwise and }
  169. { '|' : bitwise or }
  170. { '~' : bitwise complement }
  171. { '%' : modulo division }
  172. { nnn: longint numbers }
  173. { ( and ) parenthesis }
  174. {**********************************************************************}
  175. TExprParse = Object
  176. public
  177. Constructor Init;
  178. Destructor Done;
  179. Function Evaluate(Expr: String): longint;
  180. Procedure Error(anerror: texpr_error); virtual;
  181. Function Priority(_Operator: Char): Integer; virtual;
  182. private
  183. RPNStack : Array[1..RPNMax] of longint; { Stack For RPN calculator }
  184. RPNTop : Integer;
  185. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  186. OpTop : Integer;
  187. Procedure RPNPush(Num: Longint);
  188. Function RPNPop: Longint;
  189. Procedure RPNCalc(token: String15; prefix: boolean);
  190. Procedure OpPush(_Operator: char; prefix: boolean);
  191. { In reality returns TExprOperaotr }
  192. Procedure OpPop(var _Operator:TExprOperator);
  193. end;
  194. { Evaluate an expression string to a longint }
  195. Function CalculateExpression(const expression: string): longint;
  196. {---------------------------------------------------------------------}
  197. { String routines }
  198. {---------------------------------------------------------------------}
  199. {*********************************************************************}
  200. { PROCEDURE PadZero; }
  201. { Description: Makes sure that the string specified is of the given }
  202. { length, by padding it with binary zeros, or truncating if necessary}
  203. { Remark: The return value is determined BEFORE any eventual padding.}
  204. { Return Value: TRUE = if length of string s was <= then n }
  205. { FALSE = if length of string s was > then n }
  206. {*********************************************************************}
  207. Function PadZero(Var s: String; n: byte): Boolean;
  208. { Converts an Hex digit string to a Decimal string }
  209. { Returns '' if there was an error. }
  210. Function HexToDec(const S:String): String;
  211. { Converts a binary digit string to a Decimal string }
  212. { Returns '' if there was an error. }
  213. Function BinaryToDec(const S:String): String;
  214. { Converts an octal digit string to a Decimal string }
  215. { Returns '' if there was an error. }
  216. Function OctalToDec(const S:String): String;
  217. { Converts a string containing C styled escape sequences to }
  218. { a pascal style string. }
  219. Function EscapeToPascal(const s:string): string;
  220. Procedure ConcatPasString(p : paasmoutput;s:string);
  221. { Writes the string s directly to the assembler output }
  222. Procedure ConcatDirect(p : paasmoutput;s:string);
  223. {---------------------------------------------------------------------}
  224. { Symbol helper routines }
  225. {---------------------------------------------------------------------}
  226. Procedure SetOperandSize(var instr:TInstruction;operandnum,size:longint);
  227. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  228. Function SearchIConstant(const s:string; var l:longint): boolean;
  229. Function SearchLabel(const s: string; var hl: plabel): boolean;
  230. Function CreateVarInstr(var Instr: TInstruction; const hs:string;
  231. operandnum:byte):boolean;
  232. {*********************************************************************}
  233. { FUNCTION NewPasStr(s:string): PString }
  234. { Description: This routine allocates a string on the heap and }
  235. { returns a pointer to the allocated string. }
  236. { }
  237. { Remarks: The string allocated should not be modified, since it's }
  238. { length will be less then 255. }
  239. { Remarks: It is assumed that HeapError will be called if an }
  240. { allocation fails. }
  241. {*********************************************************************}
  242. Function newpasstr(s: string): Pointer;
  243. Procedure SetupResult(Var Instr:TInstruction; operandnum: byte);
  244. {$ifdef i386}
  245. Procedure FWaitWarning;
  246. {$endif}
  247. {---------------------------------------------------------------------}
  248. { Instruction generation routines }
  249. {---------------------------------------------------------------------}
  250. { swaps in the case of a 2/3 operand opcode the destination and the }
  251. { source as to put it in AT&T style instruction format. }
  252. Procedure SwapOperands(Var instr: TInstruction);
  253. Procedure ConcatLabel(p: paasmoutput;var l : plabel);
  254. Procedure ConcatConstant(p : paasmoutput;value: longint; maxvalue: longint);
  255. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  256. Procedure ConcatString(p : paasmoutput;s:string);
  257. Procedure ConcatPublic(p:paasmoutput;const s : string);
  258. Procedure ConcatLocal(p:paasmoutput;const s : string);
  259. Procedure ConcatGlobalBss(const s : string;size : longint);
  260. Procedure ConcatLocalBss(const s : string;size : longint);
  261. { add to list of external labels }
  262. Procedure ConcatExternal(const s : string;typ : texternal_typ);
  263. { add to internal list of labels }
  264. Procedure ConcatInternal(const s : string;typ : texternal_typ);
  265. Implementation
  266. {*************************************************************************}
  267. { Expression Parser }
  268. {*************************************************************************}
  269. Constructor TExprParse.Init;
  270. Begin
  271. end;
  272. Procedure TExprParse.Error(anerror:texpr_error);
  273. var
  274. t : tmsgconst;
  275. Begin
  276. case anerror of
  277. zero_divide: t:=assem_f_ev_zero_divide;
  278. stack_overflow: t:=assem_f_ev_stack_overflow;
  279. stack_underflow: t:=assem_f_ev_stack_underflow;
  280. invalid_number: t:=assem_f_ev_invalid_number;
  281. invalid_op: t:=assem_f_ev_invalid_op;
  282. else
  283. t:=assem_f_ev_unknown;
  284. end;
  285. Message(t);
  286. end;
  287. Procedure TExprParse.RPNPush(Num : longint); { Add an operand to the top of the RPN stack }
  288. begin
  289. if RPNTop < RPNMax then
  290. begin
  291. Inc(RPNTop);
  292. RPNStack[RPNTop] := Num;
  293. end
  294. else
  295. Error(stack_overflow); { Put some error handler here }
  296. end;
  297. Function TExprParse.RPNPop : longint; { Get the operand at the top of the RPN stack }
  298. begin
  299. if RPNTop > 0 then
  300. begin
  301. RPNPop := RPNStack[RPNTop];
  302. Dec(RPNTop);
  303. end
  304. else { Put some error handler here }
  305. Error(stack_underflow);
  306. end;
  307. Procedure TExprParse.RPNCalc(Token : String15; prefix:boolean); { RPN Calculator }
  308. Var
  309. Temp : longint;
  310. LocalError : Integer;
  311. begin
  312. { Write(Token, ' '); This just outputs the RPN expression }
  313. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  314. Case Token[1] of { Handle operators }
  315. '+' : Begin
  316. if prefix then
  317. else
  318. RPNPush(RPNPop + RPNPop);
  319. end;
  320. '-' : Begin
  321. if prefix then
  322. RPNPush(-(RPNPop))
  323. else
  324. RPNPush(RPNPop - RPNPop);
  325. end;
  326. '*' : RPNPush(RPNPop * RPNPop);
  327. '&' : RPNPush(RPNPop AND RPNPop);
  328. '|' : RPNPush(RPNPop OR RPNPop);
  329. '~' : RPNPush(NOT RPNPop);
  330. '<' : RPNPush(RPNPop SHL RPNPop);
  331. '>' : RPNPush(RPNPop SHR RPNPop);
  332. '%' : begin
  333. Temp := RPNPop;
  334. if Temp <> 0 then
  335. RPNPush(RPNPop mod Temp)
  336. else Error(zero_divide); { Handle divide by zero error }
  337. end;
  338. '^' : RPNPush(RPNPop XOR RPNPop);
  339. '/' :
  340. begin
  341. Temp := RPNPop;
  342. if Temp <> 0 then
  343. RPNPush(RPNPop div Temp)
  344. else Error(zero_divide);{ Handle divide by 0 error }
  345. end;
  346. end
  347. else
  348. begin { Convert String to number and add to stack }
  349. if token='-2147483648' then
  350. begin
  351. temp:=$80000000;
  352. localerror:=0;
  353. end
  354. else
  355. Val(Token, Temp, LocalError);
  356. if LocalError = 0 then
  357. RPNPush(Temp)
  358. else Error(invalid_number);{ Handle error }
  359. end;
  360. end;
  361. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean); { Add an operator onto top of the stack }
  362. begin
  363. if OpTop < OpMax then
  364. begin
  365. Inc(OpTop);
  366. OpStack[OpTop].ch := _Operator;
  367. OpStack[OpTop].is_prefix := prefix;
  368. end
  369. else Error(stack_overflow); { Put some error handler here }
  370. end;
  371. Procedure TExprParse.OpPop(var _Operator:TExprOperator); { Get operator at the top of the stack }
  372. begin
  373. if OpTop > 0 then
  374. begin
  375. _Operator := OpStack[OpTop];
  376. Dec(OpTop);
  377. end
  378. else Error(stack_underflow); { Put some error handler here }
  379. end;
  380. Function TExprParse.Priority(_Operator : Char) : Integer; { Return priority of operator }
  381. { The greater the priority, the higher the precedence }
  382. begin
  383. Case _Operator OF
  384. '(' : Priority := 0;
  385. '+', '-' : Priority := 1;
  386. '*', '/','%','<','>' : Priority := 2;
  387. '|','&','^','~': Priority := 0;
  388. else Error(invalid_op);{ More error handling }
  389. end;
  390. end;
  391. Function TExprParse.Evaluate(Expr : String):longint;
  392. Var
  393. I : Integer;
  394. Token : String15;
  395. opr: TExprOperator;
  396. begin
  397. OpTop := 0; { Reset stacks }
  398. RPNTop := 0;
  399. Token := '';
  400. For I := 1 to Length(Expr) DO
  401. begin
  402. if Expr[I] in ['0'..'9'] then
  403. begin { Build multi-digit numbers }
  404. Token := Token + Expr[I];
  405. if I = Length(Expr) then { Send last one to calculator }
  406. RPNCalc(Token,false);
  407. end
  408. else
  409. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  410. begin
  411. if Token <> '' then
  412. begin { Send last built number to calc. }
  413. RPNCalc(Token,false);
  414. Token := '';
  415. end;
  416. Case Expr[I] OF
  417. '(' : OpPush('(',false);
  418. ')' : begin
  419. While OpStack[OpTop].ch <> '(' DO
  420. Begin
  421. OpPop(opr);
  422. RPNCalc(opr.ch,opr.is_prefix);
  423. end;
  424. OpPop(opr); { Pop off and ignore the '(' }
  425. end;
  426. '+','-','~' : Begin
  427. { workaround for -2147483648 }
  428. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  429. begin
  430. token:='-';
  431. expr[i]:='+';
  432. end;
  433. { if start of expression then surely a prefix }
  434. { or if previous char was also an operator }
  435. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  436. OpPush(Expr[I],true)
  437. else
  438. Begin
  439. { Evaluate all higher priority operators }
  440. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  441. Begin
  442. OpPop(opr);
  443. RPNCalc(opr.ch,opr.is_prefix);
  444. end;
  445. OpPush(Expr[I],false);
  446. End;
  447. end;
  448. '*', '/',
  449. '^','|','&',
  450. '%','<','>' : begin
  451. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  452. Begin
  453. OpPop(opr);
  454. RPNCalc(opr.ch,opr.is_prefix);
  455. end;
  456. OpPush(Expr[I],false);
  457. end;
  458. end; { Case }
  459. end
  460. else
  461. Error(invalid_op); { Handle bad input error }
  462. end;
  463. { Pop off the remaining operators }
  464. While OpTop > 0 do
  465. Begin
  466. OpPop(opr);
  467. RPNCalc(opr.ch,opr.is_prefix);
  468. end;
  469. { The result is stored on the top of the stack }
  470. Evaluate := RPNPop;
  471. end;
  472. Destructor TExprParse.Done;
  473. Begin
  474. end;
  475. Function CalculateExpression(const expression: string): longint;
  476. var
  477. expr: TExprParse;
  478. Begin
  479. expr.Init;
  480. CalculateExpression := expr.Evaluate(expression);
  481. expr.Done;
  482. end;
  483. {*************************************************************************}
  484. { String conversions/utils }
  485. {*************************************************************************}
  486. Function newpasstr(s: string): Pointer;
  487. Var
  488. StrPtr: PString;
  489. Begin
  490. GetMem(StrPtr, length(s)+1);
  491. Move(s,StrPtr^,length(s)+1);
  492. newpasstr:= Strptr;
  493. end;
  494. Function EscapeToPascal(const s:string): string;
  495. { converts a C styled string - which contains escape }
  496. { characters to a pascal style string. }
  497. var
  498. i,j: word;
  499. str: string;
  500. temp: string;
  501. value: byte;
  502. code: integer;
  503. Begin
  504. str:='';
  505. i:=1;
  506. j:=1;
  507. repeat
  508. if s[i] = '\' then
  509. Begin
  510. Inc(i);
  511. if i > 255 then
  512. Begin
  513. EscapeToPascal:=str;
  514. exit;
  515. end;
  516. case s[i] of
  517. '\': insert('\',str,j);
  518. 'b': insert(#08,str,j);
  519. 'f': insert(#12,str,j);
  520. 'n': insert(#10,str,j);
  521. 'r': insert(#13,str,j);
  522. 't': insert(#09,str,j);
  523. '"': insert('"',str,j);
  524. { octal number }
  525. '0'..'7': Begin
  526. temp:=s[i];
  527. temp:=temp+s[i+1];
  528. temp:=temp+s[i+2];
  529. inc(i,2);
  530. val(octaltodec(temp),value,code);
  531. if (code <> 0) then
  532. Message(assem_w_invalid_numeric);
  533. insert(chr(value),str,j);
  534. end;
  535. { hexadecimal number }
  536. 'x': Begin
  537. temp:=s[i+1];
  538. temp:=temp+s[i+2];
  539. inc(i,2);
  540. val(hextodec(temp),value,code);
  541. if (code <> 0) then
  542. Message(assem_w_invalid_numeric);
  543. insert(chr(value),str,j);
  544. end;
  545. else
  546. Begin
  547. Message1(assem_e_escape_seq_ignored,s[i]);
  548. insert(s[i],str,j);
  549. end;
  550. end; {end case }
  551. Inc(i);
  552. end
  553. else
  554. Begin
  555. Insert(s[i],str,j);
  556. Inc(i);
  557. if i > 255 then
  558. Begin
  559. EscapeToPascal:=str;
  560. exit;
  561. end;
  562. end;
  563. Inc(j);
  564. until (i > length(s)) or (j > 255);
  565. EscapeToPascal:=str;
  566. end;
  567. Function OctalToDec(const S:String): String;
  568. { Converts an octal string to a Decimal string }
  569. { Returns '' if there was an error. }
  570. var vs: longint;
  571. c: byte;
  572. st: string;
  573. Begin
  574. vs := 0;
  575. for c:=1 to length(s) do
  576. begin
  577. case s[c] of
  578. '0': vs:=vs shl 3;
  579. '1': vs:=vs shl 3+1;
  580. '2': vs:=vs shl 3+2;
  581. '3': vs:=vs shl 3+3;
  582. '4': vs:=vs shl 3+4;
  583. '5': vs:=vs shl 3+5;
  584. '6': vs:=vs shl 3+6;
  585. '7': vs:=vs shl 3+7;
  586. else
  587. begin
  588. Message(assem_f_error_converting_octal);
  589. OctalToDec := '';
  590. exit;
  591. end;
  592. end;
  593. end;
  594. str(vs,st);
  595. OctalToDec := st;
  596. end;
  597. Function BinaryToDec(const S:String): String;
  598. { Converts a binary string to a Decimal string }
  599. { Returns '' if there was an error. }
  600. var vs: longint;
  601. c: byte;
  602. st: string;
  603. Begin
  604. vs := 0;
  605. for c:=1 to length(s) do
  606. begin
  607. if s[c] = '0' then
  608. vs:=vs shl 1
  609. else
  610. if s[c]='1' then
  611. vs:=vs shl 1+1
  612. else
  613. begin
  614. Message(assem_f_error_converting_bin);
  615. BinaryToDec := '';
  616. exit;
  617. end;
  618. end;
  619. str(vs,st);
  620. BinaryToDec := st;
  621. end;
  622. Function HexToDec(const S:String): String;
  623. var vs: longint;
  624. c: byte;
  625. st: string;
  626. Begin
  627. vs := 0;
  628. for c:=1 to length(s) do
  629. begin
  630. case upcase(s[c]) of
  631. '0': vs:=vs shl 4;
  632. '1': vs:=vs shl 4+1;
  633. '2': vs:=vs shl 4+2;
  634. '3': vs:=vs shl 4+3;
  635. '4': vs:=vs shl 4+4;
  636. '5': vs:=vs shl 4+5;
  637. '6': vs:=vs shl 4+6;
  638. '7': vs:=vs shl 4+7;
  639. '8': vs:=vs shl 4+8;
  640. '9': vs:=vs shl 4+9;
  641. 'A': vs:=vs shl 4+10;
  642. 'B': vs:=vs shl 4+11;
  643. 'C': vs:=vs shl 4+12;
  644. 'D': vs:=vs shl 4+13;
  645. 'E': vs:=vs shl 4+14;
  646. 'F': vs:=vs shl 4+15;
  647. else
  648. begin
  649. Message(assem_f_error_converting_hex);
  650. HexToDec := '';
  651. exit;
  652. end;
  653. end;
  654. end;
  655. str(vs,st);
  656. HexToDec := st;
  657. end;
  658. Function PadZero(Var s: String; n: byte): Boolean;
  659. Begin
  660. PadZero := TRUE;
  661. { Do some error checking first }
  662. if Length(s) = n then
  663. exit
  664. else
  665. if Length(s) > n then
  666. Begin
  667. PadZero := FALSE;
  668. delete(s,n+1,length(s));
  669. exit;
  670. end
  671. else
  672. PadZero := TRUE;
  673. { Fill it up with the specified character }
  674. fillchar(s[length(s)+1],n-1,#0);
  675. {$ifndef TP}
  676. {$ifopt H+}
  677. setlength(s,n);
  678. {$else}
  679. s[0] := chr(n);
  680. {$endif}
  681. {$else}
  682. s[0] := chr(n);
  683. {$endif}
  684. end;
  685. {*************************************************************************}
  686. { Instruction utilities }
  687. {*************************************************************************}
  688. Procedure TInstruction.init;
  689. var
  690. k: integer;
  691. Begin
  692. numops := 0;
  693. labeled := FALSE;
  694. stropsize := S_NO;
  695. prefix := A_NONE;
  696. instruction := A_NONE;
  697. for k:=1 to maxoperands do
  698. begin
  699. operands[k].size := S_NO;
  700. operands[k].overriden := FALSE;
  701. operands[k].operandtype := OPR_NONE;
  702. { init to zeros }
  703. fillchar(operands[k].l, sizeof(operands[k].l),#0);
  704. end;
  705. end;
  706. Procedure TInstruction.addprefix(tok: tasmop);
  707. Begin
  708. if tok = A_NONE then
  709. Message(assem_e_syn_prefix_not_found);
  710. if Prefix = A_NONE then
  711. Prefix := tok
  712. else
  713. Message(assem_e_syn_try_add_more_prefix);
  714. end;
  715. Procedure TInstruction.addinstr(tok: tasmop);
  716. Begin
  717. if tok = A_NONE then
  718. Message(assem_e_syn_opcode_not_found);
  719. Instruction := tok;
  720. end;
  721. function TInstruction.getinstruction: tasmop;
  722. Begin
  723. getinstruction := Instruction;
  724. end;
  725. { get the current prefix of this instruction }
  726. function TInstruction.getprefix: tasmop;
  727. Begin
  728. getprefix := prefix;
  729. end;
  730. Procedure TInstruction.done;
  731. Begin
  732. end;
  733. {*************************************************************************}
  734. { Local label utilities }
  735. {*************************************************************************}
  736. Constructor TAsmLabelList.Init;
  737. Begin
  738. First := nil;
  739. Last := nil;
  740. end;
  741. Procedure TAsmLabelList.Insert(s:string; lab: PLabel; emitted: boolean);
  742. {*********************************************************************}
  743. { Description: Insert a node at the end of the list with lab and }
  744. { and the name in s. The name is allocated on the heap. }
  745. { Duplicates are not allowed. }
  746. { Indicate in emitted if this label itself has been emitted, or is it}
  747. { a simple labeled instruction? }
  748. {*********************************************************************}
  749. Begin
  750. if search(s) = nil then
  751. Begin
  752. if First = nil then
  753. Begin
  754. New(First);
  755. Last := First;
  756. end
  757. else
  758. Begin
  759. New(Last^.Next);
  760. Last := Last^.Next;
  761. end;
  762. Last^.name := NewPasStr(s);
  763. Last^.Lab := lab;
  764. Last^.Next := nil;
  765. Last^.emitted := emitted;
  766. end;
  767. end;
  768. Function TAsmLabelList.Search(const s: string): PAsmLabel;
  769. {*********************************************************************}
  770. { Description: This routine searches for a label named s in the }
  771. { linked list, returns a pointer to the label if found, otherwise }
  772. { returns nil. }
  773. {*********************************************************************}
  774. Var
  775. asmlab: PAsmLabel;
  776. Begin
  777. asmlab := First;
  778. if First = nil then
  779. Begin
  780. Search := nil;
  781. exit;
  782. end;
  783. While (asmlab^.name^ <> s) and (asmlab^.Next <> nil) do
  784. asmlab := asmlab^.Next;
  785. if asmlab^.name^ = s then
  786. search := asmlab
  787. else
  788. search := nil;
  789. end;
  790. Destructor TAsmLabelList.Done;
  791. {*********************************************************************}
  792. { Description: This routine takes care of deallocating all nodes }
  793. { in the linked list, as well as deallocating the string pointers }
  794. { of these nodes. }
  795. { }
  796. { Remark: The PLabel field is NOT freed, the compiler takes care of }
  797. { this. }
  798. {*********************************************************************}
  799. Var
  800. temp: PAsmLabel;
  801. temp1: PAsmLabel;
  802. Begin
  803. temp := First;
  804. while temp <> nil do
  805. Begin
  806. Freemem(Temp^.name, length(Temp^.name^)+1);
  807. Temp1 := Temp^.Next;
  808. Dispose(Temp);
  809. Temp := Temp1;
  810. { The plabel could be deleted here, but let us not do }
  811. { it, FPC will do it instead. }
  812. end;
  813. end;
  814. Function TAsmLabelList.newpasstr(s: string): PString;
  815. {*********************************************************************}
  816. { FUNCTION NewPasStr(s:string): PString }
  817. { Description: This routine allocates a string on the heap and }
  818. { returns a pointer to the allocated string. }
  819. { }
  820. { Remarks: The string allocated should not be modified, since it's }
  821. { length will be less then 255. }
  822. { Remarks: It is assumed that HeapError will be called if an }
  823. { allocation fails. }
  824. {*********************************************************************}
  825. Var
  826. StrPtr: PString;
  827. Begin
  828. GetMem(StrPtr, length(s)+1);
  829. Move(s,StrPtr^,length(s)+1);
  830. newpasstr:= Strptr;
  831. end;
  832. {*************************************************************************}
  833. { Symbol table helper routines }
  834. {*************************************************************************}
  835. Procedure SwapOperands(Var instr: TInstruction);
  836. Var
  837. tempopr: TOperand;
  838. Begin
  839. if instr.numops = 2 then
  840. Begin
  841. tempopr := instr.operands[1];
  842. instr.operands[1] := instr.operands[2];
  843. instr.operands[2] := tempopr;
  844. end
  845. else
  846. if instr.numops = 3 then
  847. Begin
  848. tempopr := instr.operands[1];
  849. instr.operands[1] := instr.operands[3];
  850. instr.operands[3] := tempopr;
  851. end;
  852. end;
  853. Function SearchIConstant(const s:string; var l:longint): boolean;
  854. {**********************************************************************}
  855. { Description: Searches for a CONSTANT of name s in either the local }
  856. { symbol list, then in the global symbol list, and returns the value }
  857. { of that constant in l. Returns TRUE if successfull, if not found, }
  858. { or if the constant is not of correct type, then returns FALSE }
  859. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  860. { respectively. }
  861. {**********************************************************************}
  862. var
  863. sym: psym;
  864. Begin
  865. SearchIConstant := FALSE;
  866. { check for TRUE or FALSE reserved words first }
  867. if s = 'TRUE' then
  868. Begin
  869. SearchIConstant := TRUE;
  870. l := 1;
  871. end
  872. else
  873. if s = 'FALSE' then
  874. Begin
  875. SearchIConstant := TRUE;
  876. l := 0;
  877. end
  878. else
  879. if assigned(aktprocsym) then
  880. Begin
  881. if assigned(aktprocsym^.definition) then
  882. Begin
  883. { Check the local constants }
  884. if assigned(aktprocsym^.definition^.localst) and
  885. (lexlevel >= normal_function_level) then
  886. sym := aktprocsym^.definition^.localst^.search(s)
  887. else
  888. sym := nil;
  889. if assigned(sym) then
  890. Begin
  891. if (sym^.typ = constsym) and
  892. (pconstsym(sym)^.consttype in [constord,constint,constchar,constbool]) then
  893. Begin
  894. l:=pconstsym(sym)^.value;
  895. SearchIConstant := TRUE;
  896. exit;
  897. end;
  898. end;
  899. end;
  900. end;
  901. { Check the global constants }
  902. getsym(s,false);
  903. if srsym <> nil then
  904. Begin
  905. case srsym^.typ of
  906. constsym :
  907. begin
  908. if (pconstsym(srsym)^.consttype in [constord,constint,constchar,constbool]) then
  909. Begin
  910. l:=pconstsym(srsym)^.value;
  911. SearchIConstant := TRUE;
  912. exit;
  913. end;
  914. end;
  915. end;
  916. end;
  917. end;
  918. Procedure SetupResult(Var Instr:TInstruction; operandnum: byte);
  919. {**********************************************************************}
  920. { Description: This routine changes the correct fields and correct }
  921. { offset in the reference, so that it points to the __RESULT or }
  922. { @Result variable (depending on the inline asm). }
  923. { Resturns a reference with all correct offset correctly set up. }
  924. { The Operand should already point to a treference on entry. }
  925. {**********************************************************************}
  926. Begin
  927. { replace by correct offset. }
  928. if assigned(procinfo.retdef) and
  929. (procinfo.retdef<>pdef(voiddef)) then
  930. begin
  931. instr.operands[operandnum].ref.offset := procinfo.retoffset;
  932. instr.operands[operandnum].ref.base := procinfo.framepointer;
  933. { always assume that the result is valid. }
  934. procinfo.funcret_is_valid:=true;
  935. end
  936. else
  937. Message(assem_e_invalid_symbol_ref);
  938. end;
  939. {$ifdef i386}
  940. Procedure FWaitWarning;
  941. begin
  942. if (target_info.target=target_i386_GO32V2) and (cs_fp_emulation in aktmoduleswitches) then
  943. Message(assem_w_fwait_emu_prob);
  944. end;
  945. {$endif i386}
  946. Procedure SetOperandSize(var instr:TInstruction;operandnum,size:longint);
  947. begin
  948. { the current size is NOT overriden if it already }
  949. { exists, such as in the case of a byte ptr, in }
  950. { front of the identifier. }
  951. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  952. Begin
  953. case size of
  954. 1: instr.operands[operandnum].size := S_B;
  955. 2: instr.operands[operandnum].size := S_W{ could be S_IS};
  956. 4: instr.operands[operandnum].size := S_L{ could be S_IL or S_FS};
  957. 8: instr.operands[operandnum].size := S_IQ{ could be S_D or S_FL};
  958. extended_size: instr.operands[operandnum].size := S_FX;
  959. else
  960. { this is in the case where the instruction is LEA }
  961. { or something like that, in that case size is not }
  962. { important. }
  963. instr.operands[operandnum].size := S_NO;
  964. end; { end case }
  965. end;
  966. end;
  967. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  968. { search and returns the offset and size of records/objects of the base }
  969. { with field name setup in field. }
  970. { returns FALSE if not found. }
  971. { used when base is a variable or a typed constant name. }
  972. var
  973. st : psymtable;
  974. sym : psym;
  975. i : longint;
  976. base : string;
  977. Begin
  978. GetRecordOffsetSize := FALSE;
  979. Offset:=0;
  980. Size:=0;
  981. i:=pos('.',s);
  982. if i=0 then
  983. i:=255;
  984. base:=Copy(s,1,i-1);
  985. delete(s,1,i);
  986. getsym(base,false);
  987. sym:=srsym;
  988. st:=nil;
  989. { we can start with a var,type,typedconst }
  990. case sym^.typ of
  991. varsym :
  992. begin
  993. case pvarsym(sym)^.definition^.deftype of
  994. recorddef :
  995. st:=precdef(pvarsym(sym)^.definition)^.symtable;
  996. objectdef :
  997. st:=pobjectdef(pvarsym(sym)^.definition)^.publicsyms;
  998. end;
  999. end;
  1000. typesym :
  1001. begin
  1002. case ptypesym(sym)^.definition^.deftype of
  1003. recorddef :
  1004. st:=precdef(ptypesym(sym)^.definition)^.symtable;
  1005. objectdef :
  1006. st:=pobjectdef(ptypesym(sym)^.definition)^.publicsyms;
  1007. end;
  1008. end;
  1009. typedconstsym :
  1010. begin
  1011. case pvarsym(sym)^.definition^.deftype of
  1012. recorddef :
  1013. st:=precdef(ptypedconstsym(sym)^.definition)^.symtable;
  1014. objectdef :
  1015. st:=pobjectdef(ptypedconstsym(sym)^.definition)^.publicsyms;
  1016. end;
  1017. end;
  1018. end;
  1019. { now walk all recordsymtables }
  1020. while assigned(st) and (s<>'') do
  1021. begin
  1022. { load next field in base }
  1023. i:=pos('.',s);
  1024. if i=0 then
  1025. i:=255;
  1026. base:=Copy(s,1,i-1);
  1027. delete(s,1,i);
  1028. sym:=st^.search(base);
  1029. st:=nil;
  1030. case sym^.typ of
  1031. varsym :
  1032. begin
  1033. inc(Offset,pvarsym(sym)^.address);
  1034. Size:=PVarsym(sym)^.getsize;
  1035. case pvarsym(sym)^.definition^.deftype of
  1036. recorddef :
  1037. st:=precdef(pvarsym(sym)^.definition)^.symtable;
  1038. objectdef :
  1039. st:=pobjectdef(pvarsym(sym)^.definition)^.publicsyms;
  1040. end;
  1041. end;
  1042. end;
  1043. end;
  1044. GetRecordOffsetSize:=(s='');
  1045. end;
  1046. Function CreateVarInstr(var Instr: TInstruction; const hs:string;operandnum:byte): Boolean;
  1047. { search and sets up the correct fields in the Instr record }
  1048. { for the NON-constant identifier passed to the routine. }
  1049. { if not found returns FALSE. }
  1050. var
  1051. sym : psym;
  1052. Begin
  1053. CreateVarInstr := FALSE;
  1054. { are we in a routine ? }
  1055. getsym(hs,false);
  1056. sym:=srsym;
  1057. if sym=nil then
  1058. exit;
  1059. case sym^.typ of
  1060. varsym :
  1061. begin
  1062. { we always assume in asm statements that }
  1063. { that the variable is valid. }
  1064. pvarsym(sym)^.is_valid:=1;
  1065. inc(pvarsym(sym)^.refs);
  1066. case pvarsym(sym)^.owner^.symtabletype of
  1067. unitsymtable,
  1068. globalsymtable,
  1069. staticsymtable :
  1070. instr.operands[operandnum].ref.symbol:=newasmsymbol(pvarsym(sym)^.mangledname);
  1071. parasymtable :
  1072. begin
  1073. instr.operands[operandnum].ref.base := procinfo.framepointer;
  1074. instr.operands[operandnum].ref.offset := pvarsym(sym)^.address+aktprocsym^.definition^.parast^.address_fixup;
  1075. end;
  1076. localsymtable :
  1077. begin
  1078. if (pvarsym(sym)^.var_options and vo_is_external)<>0 then
  1079. instr.operands[operandnum].ref.symbol:=newasmsymbol(pvarsym(sym)^.mangledname)
  1080. else
  1081. begin
  1082. instr.operands[operandnum].ref.base := procinfo.framepointer;
  1083. instr.operands[operandnum].ref.offset := -(pvarsym(sym)^.address);
  1084. end;
  1085. end;
  1086. end;
  1087. case pvarsym(sym)^.definition^.deftype of
  1088. orddef,
  1089. enumdef,
  1090. floatdef :
  1091. SetOperandSize(instr,operandnum,pvarsym(sym)^.getsize);
  1092. arraydef :
  1093. SetOperandSize(instr,operandnum,parraydef(pvarsym(sym)^.definition)^.elesize)
  1094. end;
  1095. CreateVarInstr := TRUE;
  1096. Exit;
  1097. end;
  1098. typedconstsym :
  1099. begin
  1100. instr.operands[operandnum].ref.symbol:=newasmsymbol(ptypedconstsym(sym)^.mangledname);
  1101. case ptypedconstsym(sym)^.definition^.deftype of
  1102. orddef,
  1103. enumdef,
  1104. floatdef :
  1105. SetOperandSize(instr,operandnum,ptypedconstsym(sym)^.getsize);
  1106. arraydef :
  1107. SetOperandSize(instr,operandnum,parraydef(ptypedconstsym(sym)^.definition)^.elesize)
  1108. end;
  1109. CreateVarInstr := TRUE;
  1110. Exit;
  1111. end;
  1112. constsym :
  1113. begin
  1114. if pconstsym(sym)^.consttype in [constint,constchar,constbool] then
  1115. begin
  1116. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1117. instr.operands[operandnum].val:=pconstsym(sym)^.value;
  1118. CreateVarInstr := TRUE;
  1119. Exit;
  1120. end;
  1121. end;
  1122. typesym :
  1123. begin
  1124. if ptypesym(sym)^.definition^.deftype in [recorddef,objectdef] then
  1125. begin
  1126. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1127. instr.operands[operandnum].val:=0;
  1128. CreateVarInstr := TRUE;
  1129. Exit;
  1130. end;
  1131. end;
  1132. procsym :
  1133. begin
  1134. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  1135. Message(assem_w_calling_overload_func);
  1136. instr.operands[operandnum].operandtype:=OPR_SYMBOL;
  1137. instr.operands[operandnum].symbol:=newasmsymbol(pprocsym(sym)^.definition^.mangledname);
  1138. CreateVarInstr := TRUE;
  1139. Exit;
  1140. end;
  1141. else
  1142. begin
  1143. Message(assem_e_unsupported_symbol_type);
  1144. exit;
  1145. end;
  1146. end;
  1147. end;
  1148. Function SearchLabel(const s: string; var hl: plabel): boolean;
  1149. {**********************************************************************}
  1150. { Description: Searches for a pascal label definition, first in the }
  1151. { local symbol list and then in the global symbol list. If found then }
  1152. { return pointer to label and return true, otherwise returns false. }
  1153. {**********************************************************************}
  1154. var
  1155. sym: psym;
  1156. Begin
  1157. SearchLabel := FALSE;
  1158. if assigned(aktprocsym) then
  1159. Begin
  1160. { Check the local constants }
  1161. if assigned(aktprocsym^.definition) then
  1162. Begin
  1163. if assigned(aktprocsym^.definition^.localst) then
  1164. sym := aktprocsym^.definition^.localst^.search(s)
  1165. else
  1166. sym := nil;
  1167. if assigned(sym) then
  1168. Begin
  1169. if (sym^.typ = labelsym) then
  1170. Begin
  1171. hl:=plabelsym(sym)^.number;
  1172. SearchLabel := TRUE;
  1173. exit;
  1174. end;
  1175. end;
  1176. end;
  1177. end;
  1178. { Check the global label symbols... }
  1179. getsym(s,false);
  1180. if srsym <> nil then
  1181. Begin
  1182. if (srsym^.typ=labelsym) then
  1183. Begin
  1184. hl:=plabelsym(srsym)^.number;
  1185. SearchLabel:= TRUE;
  1186. exit;
  1187. end;
  1188. end;
  1189. end;
  1190. {*************************************************************************}
  1191. { Instruction Generation Utilities }
  1192. {*************************************************************************}
  1193. Procedure ConcatString(p : paasmoutput;s:string);
  1194. {*********************************************************************}
  1195. { PROCEDURE ConcatString(s:string); }
  1196. { Description: This routine adds the character chain pointed to in }
  1197. { s to the instruction linked list. }
  1198. {*********************************************************************}
  1199. Var
  1200. pc: PChar;
  1201. Begin
  1202. getmem(pc,length(s)+1);
  1203. p^.concat(new(pai_string,init_length_pchar(strpcopy(pc,s),length(s))));
  1204. end;
  1205. Procedure ConcatPasString(p : paasmoutput;s:string);
  1206. {*********************************************************************}
  1207. { PROCEDURE ConcatPasString(s:string); }
  1208. { Description: This routine adds the character chain pointed to in }
  1209. { s to the instruction linked list, contrary to ConcatString it }
  1210. { uses a pascal style string, so it conserves null characters. }
  1211. {*********************************************************************}
  1212. Begin
  1213. p^.concat(new(pai_string,init(s)));
  1214. end;
  1215. Procedure ConcatDirect(p : paasmoutput;s:string);
  1216. {*********************************************************************}
  1217. { PROCEDURE ConcatDirect(s:string) }
  1218. { Description: This routine output the string directly to the asm }
  1219. { output, it is only sed when writing special labels in AT&T mode, }
  1220. { and should not be used without due consideration, since it may }
  1221. { cause problems. }
  1222. {*********************************************************************}
  1223. Var
  1224. pc: PChar;
  1225. Begin
  1226. getmem(pc,length(s)+1);
  1227. p^.concat(new(pai_direct,init(strpcopy(pc,s))));
  1228. end;
  1229. Procedure ConcatConstant(p: paasmoutput; value: longint; maxvalue: longint);
  1230. {*********************************************************************}
  1231. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1232. { Description: This routine adds the value constant to the current }
  1233. { instruction linked list. }
  1234. { maxvalue -> indicates the size of the data to initialize: }
  1235. { $ff -> create a byte node. }
  1236. { $ffff -> create a word node. }
  1237. { $ffffffff -> create a dword node. }
  1238. {*********************************************************************}
  1239. Begin
  1240. if value > maxvalue then
  1241. Begin
  1242. Message(assem_e_constant_out_of_bounds);
  1243. { assuming a value of maxvalue }
  1244. value := maxvalue;
  1245. end;
  1246. if maxvalue = $ff then
  1247. p^.concat(new(pai_const,init_8bit(byte(value))))
  1248. else
  1249. if maxvalue = $ffff then
  1250. p^.concat(new(pai_const,init_16bit(word(value))))
  1251. else
  1252. if maxvalue = $ffffffff then
  1253. p^.concat(new(pai_const,init_32bit(longint(value))));
  1254. end;
  1255. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  1256. {***********************************************************************}
  1257. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1258. { Description: This routine adds the value constant to the current }
  1259. { instruction linked list. }
  1260. { real_typ -> indicates the type of the real data to initialize: }
  1261. { s32real -> create a single node. }
  1262. { s64real -> create a double node. }
  1263. { s80real -> create an extended node. }
  1264. { s64bit -> create a comp node. }
  1265. { f32bit -> create a fixed node. (not used normally) }
  1266. {***********************************************************************}
  1267. Begin
  1268. case real_typ of
  1269. s32real : p^.concat(new(pai_single,init(value)));
  1270. s64real : p^.concat(new(pai_double,init(value)));
  1271. s80real : p^.concat(new(pai_extended,init(value)));
  1272. s64bit : p^.concat(new(pai_comp,init(value)));
  1273. f32bit : p^.concat(new(pai_const,init_32bit(trunc(value*$10000))));
  1274. end;
  1275. end;
  1276. Procedure ConcatLabel(p: paasmoutput;var l : plabel);
  1277. {*********************************************************************}
  1278. { PROCEDURE ConcatLabel }
  1279. { Description: This routine either emits a label or a labeled }
  1280. { instruction to the linked list of instructions. }
  1281. {*********************************************************************}
  1282. begin
  1283. p^.concat(new(pai_label,init(l)))
  1284. end;
  1285. procedure ConcatPublic(p:paasmoutput;const s : string);
  1286. {*********************************************************************}
  1287. { PROCEDURE ConcatPublic }
  1288. { Description: This routine emits an global definition to the }
  1289. { linked list of instructions.(used by AT&T styled asm) }
  1290. {*********************************************************************}
  1291. begin
  1292. p^.concat(new(pai_symbol,init_global(s)));
  1293. { concat_internal(s,EXT_NEAR); done in aasm }
  1294. end;
  1295. procedure ConcatLocal(p:paasmoutput;const s : string);
  1296. {*********************************************************************}
  1297. { PROCEDURE ConcatLocal }
  1298. { Description: This routine emits an local definition to the }
  1299. { linked list of instructions. }
  1300. {*********************************************************************}
  1301. begin
  1302. p^.concat(new(pai_symbol,init(s)));
  1303. { concat_internal(s,EXT_NEAR); done in aasm }
  1304. end;
  1305. Procedure ConcatGlobalBss(const s : string;size : longint);
  1306. {*********************************************************************}
  1307. { PROCEDURE ConcatGlobalBss }
  1308. { Description: This routine emits an global datablock to the }
  1309. { linked list of instructions. }
  1310. {*********************************************************************}
  1311. begin
  1312. bsssegment^.concat(new(pai_datablock,init_global(s,size)));
  1313. { concat_internal(s,EXT_NEAR); done in aasm }
  1314. end;
  1315. Procedure ConcatLocalBss(const s : string;size : longint);
  1316. {*********************************************************************}
  1317. { PROCEDURE ConcatLocalBss }
  1318. { Description: This routine emits a local datablcok to the }
  1319. { linked list of instructions. }
  1320. {*********************************************************************}
  1321. begin
  1322. bsssegment^.concat(new(pai_datablock,init(s,size)));
  1323. { concat_internal(s,EXT_NEAR); done in aasm }
  1324. end;
  1325. { add to list of external labels }
  1326. Procedure ConcatExternal(const s : string;typ : texternal_typ);
  1327. {*********************************************************************}
  1328. { PROCEDURE ConcatExternal }
  1329. { Description: This routine emits an external definition to the }
  1330. { linked list of instructions.(used by AT&T styled asm) }
  1331. {*********************************************************************}
  1332. { check if in internal list and remove it there }
  1333. var p : pai_external;
  1334. begin
  1335. p:=search_assembler_symbol(internals,s,typ);
  1336. if p<>nil then internals^.remove(p);
  1337. concat_external(s,typ);
  1338. end;
  1339. { add to internal list of labels }
  1340. Procedure ConcatInternal(const s : string;typ : texternal_typ);
  1341. {*********************************************************************}
  1342. { PROCEDURE ConcatInternal }
  1343. { Description: This routine emits an internal definition of a symbol }
  1344. { (used by AT&T styled asm for undefined labels) }
  1345. {*********************************************************************}
  1346. begin
  1347. concat_internal(s,typ);
  1348. end;
  1349. end.
  1350. {
  1351. $Log$
  1352. Revision 1.9 1999-04-26 23:26:14 peter
  1353. * redesigned record offset parsing to support nested records
  1354. * normal compiler uses the redesigned createvarinstr()
  1355. Revision 1.8 1999/03/31 13:55:19 peter
  1356. * assembler inlining working for ag386bin
  1357. Revision 1.7 1999/03/24 23:17:23 peter
  1358. * fixed bugs 212,222,225,227,229,231,233
  1359. Revision 1.6 1999/03/01 13:22:25 pierre
  1360. * varsym refs incremented
  1361. Revision 1.5 1999/02/25 21:02:51 peter
  1362. * ag386bin updates
  1363. + coff writer
  1364. Revision 1.4 1999/02/22 02:15:39 peter
  1365. * updates for ag386bin
  1366. Revision 1.3 1999/02/16 00:47:28 peter
  1367. * fixed local copies of value para's
  1368. Revision 1.2 1999/01/27 13:04:11 pierre
  1369. * bug with static vars in assembler readers
  1370. Revision 1.1 1999/01/10 15:38:00 peter
  1371. * moved some tables from ra386*.pas -> i386.pas
  1372. + start of coff writer
  1373. * renamed asmutils unit to rautils
  1374. }