Skip to content

Commit 1b719af

Browse files
authored
Merge pull request #3241 from masatake/gas-macro-params
Asm: extract macro parameters
2 parents ea9afbb + d0ccdc4 commit 1b719af

File tree

6 files changed

+186
-18
lines changed

6 files changed

+186
-18
lines changed

Tmain/list-fields-with-prefix.d/stdout-expected.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ o UCTAGSnth no NONE -i- no -- the order
66
p UCTAGSscopeKind no NONE s-- no -- [tags output] no effect, [xref and json output] kind of scope in long-name form
77
r UCTAGSroles no NONE s-- no r- Roles
88
x UCTAGSxpath no NONE s-- no -- xpath for the tag
9+
- UCTAGSproperties yes Asm s-- no -- properties (req, vararg for parameters)
910
- UCTAGSproperties no AutoIt s-- no -- properties (static, volatile, ...)
1011
- UCTAGSmacrodef no C s-- no -- macro definition
1112
- UCTAGSproperties no C s-- no -- properties (static, inline, mutable,...)

Tmain/list-fields.d/stdout-expected.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ s NONE yes NONE s-- no -- [tags output] scope (kind:name) of tag definition, [xr
2424
t typeref yes NONE s-- no rw Type and name of a variable or typedef
2525
x xpath no NONE s-- no -- xpath for the tag
2626
z kind no NONE s-- no r- [tags output] prepend "kind:" to k/ (or K/) field output, [xref and json output] kind in long-name form
27+
- properties yes Asm s-- no -- properties (req, vararg for parameters)
2728
- properties no AutoIt s-- no -- properties (static, volatile, ...)
2829
- macrodef no C s-- no -- macro definition
2930
- properties no C s-- no -- properties (static, inline, mutable,...)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--sort=no
2+
--kinds-Asm=+z
3+
--fields=+eSo
4+
--fields-Asm=+{properties}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
comm input.asm /^ .macro comm$/;" m end:4
2+
plus1 input.asm /^ .macro plus1 p, p1$/;" m signature:p p1 end:7
3+
p input.asm /^ .macro plus1 p, p1$/;" z macro:plus1 nth:0
4+
p1 input.asm /^ .macro plus1 p, p1$/;" z macro:plus1 nth:1
5+
plus2 input.asm /^ .macro plus2 p p1$/;" m signature:p p1 end:10
6+
p input.asm /^ .macro plus2 p p1$/;" z macro:plus2 nth:0
7+
p1 input.asm /^ .macro plus2 p p1$/;" z macro:plus2 nth:1
8+
reserve_str input.asm /^ .macro reserve_str p1=0 p2$/;" m signature:p1=0 p2 end:13
9+
p1 input.asm /^ .macro reserve_str p1=0 p2$/;" z macro:reserve_str nth:0
10+
p2 input.asm /^ .macro reserve_str p1=0 p2$/;" z macro:reserve_str nth:1
11+
m input.asm /^ .macro m p1:req, p2=0, p3:vararg$/;" m signature:p1:req p2=0 p3:vararg end:16
12+
p1 input.asm /^ .macro m p1:req, p2=0, p3:vararg$/;" z macro:m nth:0 properties:req
13+
p2 input.asm /^ .macro m p1:req, p2=0, p3:vararg$/;" z macro:m nth:1
14+
p3 input.asm /^ .macro m p1:req, p2=0, p3:vararg$/;" z macro:m nth:2 properties:vararg
15+
func_define input.asm /^ .macro func_define name,nr=0$/;" m signature:name nr=0 end:27
16+
name input.asm /^ .macro func_define name,nr=0$/;" z macro:func_define nth:0
17+
nr input.asm /^ .macro func_define name,nr=0$/;" z macro:func_define nth:1
18+
\\name input.asm /^ .macro \\name arg1,arg2,arg3,arg4$/;" m macro:func_define signature:arg1 arg2 arg3 arg4 end:26
19+
arg1 input.asm /^ .macro \\name arg1,arg2,arg3,arg4$/;" z macro:func_define.\\name nth:0
20+
arg2 input.asm /^ .macro \\name arg1,arg2,arg3,arg4$/;" z macro:func_define.\\name nth:1
21+
arg3 input.asm /^ .macro \\name arg1,arg2,arg3,arg4$/;" z macro:func_define.\\name nth:2
22+
arg4 input.asm /^ .macro \\name arg1,arg2,arg3,arg4$/;" z macro:func_define.\\name nth:3
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
;; Taken from "7.63 '.macro'", the section of the info document for Gas
2+
3+
.macro comm
4+
.endm
5+
6+
.macro plus1 p, p1
7+
.endm
8+
9+
.macro plus2 p p1
10+
.endm
11+
12+
.macro reserve_str p1=0 p2
13+
.endm
14+
15+
.macro m p1:req, p2=0, p3:vararg
16+
.endm
17+
18+
;; Taken From linux/arch/m68k/kernel/head.S
19+
.macro func_define name,nr=0
20+
.macro \name arg1,arg2,arg3,arg4
21+
move_stack \nr,\arg1,\arg2,\arg3,\arg4
22+
func_call \name
23+
.if \nr
24+
lea %sp@(\nr*4),%sp
25+
.endif
26+
.endm
27+
.endm

parsers/asm.c

Lines changed: 131 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ typedef enum {
3232
K_PSUEDO_MACRO_END = -2,
3333
K_NONE = -1, K_DEFINE, K_LABEL, K_MACRO, K_TYPE,
3434
K_SECTION,
35+
K_PARAM,
3536
} AsmKind;
3637

3738
typedef enum {
@@ -65,6 +66,16 @@ typedef struct {
6566
AsmKind kind;
6667
} opKind;
6768

69+
typedef enum {
70+
F_PROPERTIES,
71+
} asmField;
72+
73+
static fieldDefinition AsmFields[] = {
74+
{ .name = "properties",
75+
.description = "properties (req, vararg for parameters)",
76+
.enabled = true },
77+
};
78+
6879
/*
6980
* DATA DEFINITIONS
7081
*/
@@ -81,6 +92,7 @@ static kindDefinition AsmKinds [] = {
8192
{ true, 't', "type", "types (structs and records)" },
8293
{ true, 's', "section", "sections",
8394
.referenceOnly = true, ATTACH_ROLES(asmSectionRoles)},
95+
{ false,'z', "parameter", "parameters for a macro" },
8496
};
8597

8698
static const keywordTable AsmKeywords [] = {
@@ -184,33 +196,35 @@ static bool isDefineOperator (const vString *const operator)
184196
return result;
185197
}
186198

187-
static void makeAsmTag (
199+
static int makeAsmTag (
188200
const vString *const name,
189201
const vString *const operator,
190202
const bool labelCandidate,
191203
const bool nameFollows,
192204
const bool directive,
193-
int *lastMacroCorkIndex)
205+
int *scope)
194206
{
207+
int r = CORK_NIL;
208+
195209
if (vStringLength (name) > 0)
196210
{
197211
bool found;
198212
const AsmKind kind = operatorKind (operator, &found);
199213
if (found)
200214
{
201215
if (kind > K_NONE)
202-
makeSimpleTag (name, kind);
216+
r = makeSimpleTag (name, kind);
203217
}
204218
else if (isDefineOperator (operator))
205219
{
206220
if (! nameFollows)
207-
makeSimpleTag (name, K_DEFINE);
221+
r = makeSimpleTag (name, K_DEFINE);
208222
}
209223
else if (labelCandidate)
210224
{
211225
operatorKind (name, &found);
212226
if (! found)
213-
makeSimpleTag (name, K_LABEL);
227+
r = makeSimpleTag (name, K_LABEL);
214228
}
215229
else if (directive)
216230
{
@@ -223,27 +237,34 @@ static void makeAsmTag (
223237
case K_NONE:
224238
break;
225239
case K_MACRO:
226-
*lastMacroCorkIndex = makeSimpleTag (operator,
227-
kind_for_directive);
228-
if (*lastMacroCorkIndex != CORK_NIL)
229-
registerEntry (*lastMacroCorkIndex);
240+
r = makeSimpleTag (operator, kind_for_directive);
241+
macro_tag = getEntryInCorkQueue (r);
242+
if (macro_tag)
243+
{
244+
macro_tag->extensionFields.scopeIndex = *scope;
245+
registerEntry (r);
246+
*scope = r;
247+
}
230248
break;
231249
case K_PSUEDO_MACRO_END:
232-
macro_tag = getEntryInCorkQueue (*lastMacroCorkIndex);
250+
macro_tag = getEntryInCorkQueue (*scope);
233251
if (macro_tag)
252+
{
234253
macro_tag->extensionFields.endLine = getInputLineNumber ();
235-
*lastMacroCorkIndex = CORK_NIL;
254+
*scope = macro_tag->extensionFields.scopeIndex;
255+
}
236256
break;
237257
case K_SECTION:
238-
makeSimpleRefTag (operator,
239-
kind_for_directive,
240-
ASM_SECTION_PLACEMENT);
258+
r = makeSimpleRefTag (operator,
259+
kind_for_directive,
260+
ASM_SECTION_PLACEMENT);
241261
break;
242262
default:
243-
makeSimpleTag (operator, kind_for_directive);
263+
r = makeSimpleTag (operator, kind_for_directive);
244264
}
245265
}
246266
}
267+
return r;
247268
}
248269

249270
static const unsigned char *readSymbol (
@@ -305,6 +326,94 @@ static const unsigned char *asmReadLineFromInputFile (void)
305326
return (unsigned char *)vStringValue (line);
306327
}
307328

329+
static void readMacroParameters (int index, tagEntryInfo *e, const unsigned char *cp)
330+
{
331+
vString *name = vStringNew ();
332+
vString *signature = vStringNew ();
333+
int nth = 0;
334+
335+
if (*cp == ',')
336+
++cp;
337+
338+
while (*cp)
339+
{
340+
const unsigned char *tmp;
341+
tagEntryInfo *e = NULL;
342+
343+
while (isspace ((int) *cp))
344+
++cp;
345+
346+
tmp = cp;
347+
cp = readSymbol (cp, name);
348+
if (cp == tmp)
349+
break;
350+
351+
{
352+
int r = makeSimpleTag (name, K_PARAM);
353+
e = getEntryInCorkQueue (r);
354+
if (e)
355+
{
356+
e->extensionFields.scopeIndex = index;
357+
e->extensionFields.nth = nth++;
358+
}
359+
if (vStringLength (signature) > 0 && vStringLast (signature) != ' ')
360+
vStringPut (signature, ' ');
361+
vStringCat (signature, name);
362+
}
363+
364+
if (*cp == ':')
365+
{
366+
cp++;
367+
if (strncmp((const char *)cp, "req" ,3) == 0)
368+
{
369+
cp += 3;
370+
if (e)
371+
attachParserField (e, true, AsmFields[F_PROPERTIES].ftype,
372+
"req");
373+
vStringCatS (signature, ":req");
374+
}
375+
else if (strncmp((const char *)cp, "vararg", 6) == 0)
376+
{
377+
cp += 6;
378+
if (e)
379+
attachParserField (e, true, AsmFields[F_PROPERTIES].ftype,
380+
"vararg");
381+
vStringCatS (signature, ":vararg");
382+
}
383+
cp = (const unsigned char *)strpbrk ((const char *)cp , " \t,=");
384+
if (cp == NULL)
385+
break;
386+
}
387+
if (*cp == '=')
388+
{
389+
const unsigned char *start = cp;
390+
cp = (const unsigned char *)strpbrk ((const char *)cp , " \t,");
391+
392+
if (cp)
393+
vStringNCatS (signature, (const char *)start, cp - start);
394+
else
395+
{
396+
vStringCatS (signature, (const char *)start);
397+
break;
398+
}
399+
}
400+
401+
while (isspace ((int) *cp))
402+
++cp;
403+
404+
if (*cp == ',')
405+
cp++;
406+
}
407+
408+
if (vStringLength (signature) > 0)
409+
{
410+
e->extensionFields.signature = vStringDeleteUnwrap (signature);
411+
signature = NULL;
412+
}
413+
vStringDelete (signature); /* NULL is acceptable. */
414+
vStringDelete (name);
415+
}
416+
308417
static void findAsmTags (void)
309418
{
310419
vString *name = vStringNew ();
@@ -315,7 +424,7 @@ static void findAsmTags (void)
315424
KIND_GHOST_INDEX, 0, 0, KIND_GHOST_INDEX, KIND_GHOST_INDEX, 0, 0,
316425
FIELD_UNKNOWN);
317426

318-
int lastMacroCorkIndex = CORK_NIL;
427+
int scope = CORK_NIL;
319428

320429
while ((line = asmReadLineFromInputFile ()) != NULL)
321430
{
@@ -379,8 +488,10 @@ static void findAsmTags (void)
379488
cp = readSymbol (cp, name);
380489
nameFollows = true;
381490
}
382-
makeAsmTag (name, operator, labelCandidate, nameFollows, directive,
383-
&lastMacroCorkIndex);
491+
int r = makeAsmTag (name, operator, labelCandidate, nameFollows, directive, &scope);
492+
tagEntryInfo *e = getEntryInCorkQueue (r);
493+
if (e && e->kindIndex == K_MACRO && isRoleAssigned(e, ROLE_DEFINITION_INDEX))
494+
readMacroParameters (r, e, cp);
384495
}
385496

386497
cppTerminate ();
@@ -420,5 +531,7 @@ extern parserDefinition* AsmParser (void)
420531
def->keywordCount = ARRAY_SIZE (AsmKeywords);
421532
def->selectLanguage = selectors;
422533
def->useCork = CORK_QUEUE | CORK_SYMTAB;
534+
def->fieldTable = AsmFields;
535+
def->fieldCount = ARRAY_SIZE (AsmFields);
423536
return def;
424537
}

0 commit comments

Comments
 (0)