Channels ▼

Jack Woehr

Dr. Dobb's Bloggers

Generating Java from m4 macros

August 17, 2008

Pounding code on my now-two-week-old open source project PigIron , I am confronted by a problem I've seen before: a wire protocol with a large number of data types. The macro language Gnu m4 rides to the rescue!

There are 100 remote-callable functions in IBM z/VM V5R3.0 Systems Management Application Programming (VSMAPI) at which PigIron is targeted. Many parameter types are common between many or all VSMAPI individual functions, but many or most possess function-specific struct or array-of-struct types.

I'm now generating the .java files to embody these struct and array types from simple description files in m4, specifically, Gnu m4, using macros which I call (in order to have some name for the directory in which they are stored) PigGen.

Below are two description files. One generates and the other generates for the array which contains repeating instances of (an intermediate counted-struct representation (not shown here) of) the memory segment struct. The code for pigstruct.m4 and pigarray.m4 which do all the work is in the PigIron source tree (though not in the first release. 0.1, currently posted on SourceForge ).

{geshibot}include(`pigstruct.m4')dnl \\ memory_segment_struct.m4
pigparm_class(`MemorySegmentStruct', `VSMStruct',`com.softwoehr.pigiron.access.paramstructs',`dnl

* MemorySegmentStruct implements the memory_segment_structure from Shared_Memory_Query
* @see com.softwoehr.pigiron.functions.SharedMemoryQuery
pigparm_constant(`public', `int', `MEMORY_SEGMENT_STATUS_SKELETON', `1', `Skeleton')dnl
pigparm_constant(`public', `int', `MEMORY_SEGMENT_STATUS_AVAILABLE_NONRESTRICTED', `2', `Available and nonrestricted')dnl
pigparm_constant(`public', `int', `MEMORY_SEGMENT_STATUS_AVAILABLE_RESTRICTED', `3', `Available and restricted')dnl
pigparm_constant(`public', `int', `MEMORY_SEGMENT_STATUS_PENDING_PURGE', `4', `Pending purge')dnl
pigparm_model_parm(`VSMInt4', `-1', `memory_segment_name_length')dnl
pigparm_model_parm(`VSMString', `null', `memory_segment_name')dnl
pigparm_model_parm(`VSMInt1', `-1', `memory_segment_status')dnl
pigparm_model_parm(`VSMInt4', `-1', `page_range_array_length')dnl
pigparm_model_parm(`PageRangeArray', `null', `page_range_array')dnl

{geshibot}include(`pigarray.m4')dnl \\ memory_segment_array.m4
pigparm_class(`MemorySegmentArray', `VSMArray',`com.softwoehr.pigiron.access.paramstructs',`dnl

* MemorySegmentArray implements the memory_segment_array from Shared_Memory_Query
* @see com.softwoehr.pigiron.functions.SharedMemoryQuery
* @see com.softwoehr.pigiron.access.paramstructs.PageRangeStruct
pigparm_model_parm(`MemorySegmentStructCounted', `null', `memory_segment_structure_counted')dnl

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.