Numiton

NOTE: To use the advanced features of this site you need javascript turned on.

Home Knowledge Base Software Migrators Migration Case Study

Migration Case Study

An example for migrator between similar programming languages is one from Object Pascal to C++. Both languages are object-oriented. Naturally, there is no perfect overlap between their features:

 

 

From the translation point of view, the Object Pascal specific and the common features are the most relevant. The features specific to C++ are only used to simplify the structure of the generated code.

The information in the above figure is also useful for evaluating the possibility of migrating from C++ to Object Pascal. Features like pointer arithmetics, operator overloading and templates make the design of such a translator a much more difficult task.

Pascal has functions and procedures, but C++ only has functions. There is a special category of functions with void return type that are the equivalent of Pascal's procedures. There is no incompatibility between mechanisms for parameter passing – by value or by reference for both languages.

Pascal allows the declaration of nested procedures. A nested procedure is declared in the declaration section of its parent procedure. It has access to the declared variables of the parent. The transformation engine of the migrator transforms the nested procedures into regular functions, as in the figure below.

 

 

The transformation process handles the issue of identifier overlapping and visibility. Identifier overlapping is due to the fact that two nested procedures may have the same name if they are nested in different parent procedures. A solution is to rename the procedure by concatenation with the name of the parent, thus giving it an unique name. The issue of variable visibility is due to the fact that the nested procedure has access to local variables of the parent. Transforming these local variables into global ones, together with uniquely renaming them, is a solution.

Elementary data types are similar in both Pascal and C++. Both languages have integer, floating-point and character types. The string type from Pascal becomes a C++ pointer. User-defined types are useful in situations when a certain level of data abstraction is desired. By using them, subsequent type modifications for variables is easy to do by modifying the underlying type. Both languages allow user-defined types, so the translation process does a one-to-one mapping. C++ structures correspond to Pascal records. Variable-length Pascal records are the equivalent of unions in C++.

C++ is much richer in operators than Pascal. C++ has specialized operators that perform certain operations more efficiently than generic counterparts. This is the case of the incrementing (++) and decrementing (--) operators. A migrator that makes use of these operators performs a source code optimization.

Pascal expressions can in most cases be mapped one-to-one to C++ expressions. The exception are string expressions. Pascal defines the addition operator to concatenate character strings. In C++ character strings are defined as pointers, so functions from the standard library are used instead of operators. The C++ feature of operator overloading can be used to increase code readability.

Decision and loop statements have almost identical semantics in both languages. The FOR statement is more generic in C++, so the translation is direct. By contrast, one of the difficulties encountered by the reverse translator would be passing from a generic C++ FOR loop to a more restrictive Pascal one.

Object-oriented concepts are available both in Pascal and C++. Features offered by C++ are richer than Pascal's, which makes the translation easy.

The difficulty of migrating from Pascal to C++ is translating the Pascal specific features, which require some kind of emulation in C++. The emulation of the set concept from Pascal, is done by writing a specialized Set class. The operator used to test set membership is replaced by a specialized class method or an overloaded operator.

Intervals are used in Pascal to declare arrays and represent the variation interval for access indexes. In C++ any array has the implicit start index of zero. A possible solution for mapping is to write a specialized class that wraps the array and performs automatic index conversion.

Each programming language has its own standard program libraries. The standard Pascal libraries require emulation in C++ in order to solve link dependencies. Because the standard C++ libraries are very rich, the emulation process is in fact a straightforward mapping to existing functions.

This discussion only briefly presented the similarities and differences between Object Pascal and C++. In order to achieve a semantically correct translation, “the devil is in the details” when implementing a software migrator.