Skip to content

Commit 75ff557

Browse files
committed
[Literate] Generate enumeration values for bitfields
1 parent 4f44c9a commit 75ff557

File tree

2 files changed

+100
-4
lines changed

2 files changed

+100
-4
lines changed

src/main/java/net/signbit/samx/literate/CppVisitor.java

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,64 @@ private enum FieldIndices
298298
ValueDescription
299299
}
300300

301+
class EnumerationValue
302+
{
303+
String name;
304+
String value;
305+
final String description;
306+
307+
public EnumerationValue(String name, String value, String description)
308+
{
309+
if ((name == null) || name.isEmpty())
310+
{
311+
this.name = computeNameFromDescription(description);
312+
}
313+
else
314+
{
315+
this.name = name;
316+
}
317+
if (NumberUtils.isCreatable(value))
318+
{
319+
this.value = value;
320+
}
321+
else
322+
{
323+
if ((value.charAt(0) == '0') && (value.charAt(1) == 'b'))
324+
{
325+
int intValue = Integer.parseInt(value.substring(2), 2);
326+
this.value = Integer.toHexString(intValue);
327+
}
328+
else
329+
{
330+
int intValue = NumberUtils.createInteger(value);
331+
this.value = Integer.toString(intValue);
332+
}
333+
}
334+
335+
this.description = description + ": " + value;
336+
}
337+
338+
public String getName()
339+
{
340+
return name;
341+
}
342+
343+
public String getValue()
344+
{
345+
return value;
346+
}
347+
348+
public String getDescription()
349+
{
350+
return description;
351+
}
352+
}
353+
354+
private String computeNameFromDescription(String description)
355+
{
356+
return description.replace(' ', '_').replace('/', '_');
357+
}
358+
301359
class BitField
302360
{
303361
final int word;
@@ -306,6 +364,7 @@ class BitField
306364
final String name;
307365

308366
final String enumType;
367+
final ArrayList<EnumerationValue> values = new ArrayList<>();
309368

310369
public BitField(RecordSetVisitor.RecordDataGroup rdg)
311370
{
@@ -320,6 +379,33 @@ public BitField(RecordSetVisitor.RecordDataGroup rdg)
320379
if (rdg.getRows().size() > 1)
321380
{
322381
enumType = name;
382+
383+
for (RecordSetVisitor.RecordData rd : rdg.getRows())
384+
{
385+
values.add(new EnumerationValue(rd.getValue(FieldIndices.ValueName.ordinal(), plainTextVisitor), rd.getValue(FieldIndices.Value.ordinal(), plainTextVisitor), rd.getValue(FieldIndices.ValueDescription.ordinal(), plainTextVisitor)));
386+
}
387+
388+
int maxNameLength = 0;
389+
int maxValueLength = 0;
390+
391+
for (EnumerationValue ev: values)
392+
{
393+
if (maxNameLength < ev.name.length())
394+
{
395+
maxNameLength = ev.name.length();
396+
}
397+
398+
if (maxValueLength < ev.value.length())
399+
{
400+
maxValueLength = ev.value.length();
401+
}
402+
}
403+
404+
for (EnumerationValue ev: values)
405+
{
406+
ev.name = String.format("%1$-" + maxNameLength + "s", ev.name);
407+
ev.value = String.format("%1$" + maxValueLength + "s", ev.value);
408+
}
323409
}
324410
else
325411
{
@@ -362,14 +448,19 @@ public boolean isBoolean()
362448

363449
public boolean isEnumeration()
364450
{
365-
return enumType != null && (width > 1);
451+
return (! values.isEmpty());
366452
}
367453

368454
public String getEnumType()
369455
{
370456
return enumType;
371457
}
372458

459+
public ArrayList<EnumerationValue> getEnumerationValues()
460+
{
461+
return values;
462+
}
463+
373464
public String getName()
374465
{
375466
return name;
@@ -389,15 +480,15 @@ public BitFieldDefinition(SamXParser.RecordSetContext ctx)
389480
final String unitWidthHeader = ctx.headerRow().NAME(0).getText();
390481
unitWidth = unitWidths.getOrDefault(unitWidthHeader, 0);
391482

392-
for (RecordSetVisitor.RecordDataGroup rdg: rs.getGroups())
483+
for (RecordSetVisitor.RecordDataGroup rdg : rs.getGroups())
393484
{
394485
if (rdg.hasSingleValue(FieldIndices.Name.ordinal()) && rdg.hasSingleValue(FieldIndices.Offset.ordinal()) && rdg.hasSingleValue(FieldIndices.Offset.ordinal()))
395486
{
396487
fields.add(new BitField(rdg));
397488
}
398489
else
399490
{
400-
for (RecordSetVisitor.RecordData rd: rdg.getRows())
491+
for (RecordSetVisitor.RecordData rd : rdg.getRows())
401492
{
402493
fields.add(new BitField(rd));
403494
}

src/main/resources/net/signbit/samx/literate/cpp_header.stg

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,16 @@ private:
5757

5858
>>
5959

60+
enumerationDecl(enumVal) ::= <<
61+
<enumVal.name> = <enumVal.value>, /// <enumVal.description>
62+
63+
>>
64+
6065
bitFieldDecl(field, unitWidth) ::= <<
6166

6267
<if(field.enumeration)>enum class <field.enumType> : uint<unitWidth>_t
6368
{
64-
};<endif>
69+
<field.enumerationValues:enumerationDecl()>};<endif>
6570

6671

6772
/** <field.name>

0 commit comments

Comments
 (0)