Showing posts with label Encoder. Show all posts
Showing posts with label Encoder. Show all posts

Thursday, October 31, 2013

Table – Has Operand Determination

During the development of the encoder before all of the routines were implemented, it was necessary to have a has operand flag in the table entries for codes that have an operand.  This is no longer necessary as the equivalent can now be determined by whether the code has an operand text or an encode function pointer.

The has operand flag was removed from the table entries of these codes.  A has operand table access function was added that returns whether the table entry of the code has an operand text function.  The tests for the has operand flag were replaced with this new function or use the  presence of the operand text or encode function pointer (by using the table access functions for these).

The program model operand text function is also no longer needed, which had special allowance for whether there was no operand text function for a code with the has operand flag.  The encode routine also had special allowance for this same condition, but this is also not necessary since all codes with operands have an encode function.

[commit 1e8b92e47c]

Sunday, October 27, 2013

Encoder/Program – Release

This concludes the integration of the encoder with the program model and dictionaries for the initial BASIC features being implemented.  The program model still keeps all of the translation RPN lists, but these are only used to detect when a line changes.  Once the recreator is implemented, these lists will no longer need to be kept.

Version 0.5.3 has been released (branch0.5 was merged to the master branch and tagged v0.5.3).  Archive files containing the source and binaries (with test files) for both Windows and Linux have been uploaded to SourceForge.  For Windows, there is also the ibcp-libs.zip file that contains all the dynamic linked libraries required to run the program if the MinGW and QtSDK packages have not been installed (extract into the ibcp directory).  Linux should already have the required libraries installed.  This concludes the 0.5 development series.

Implementation of the recreator will now begin with the 0.6 development series.  The recreator will convert the internal program code back to a reasonable facsimile of the originally entered program text.  This text will then replace the original text in the edit box.

[commit d984a70c6b]

Saturday, October 26, 2013

Program – Delay Line Encoding

The edit box class has an issue where sometimes unmodified lines are reported as being changed.  There is currently a check when replacing a program line where if the line has not changed, no action is taken.  Currently the translated RPN list is compared to the stored RPN list for the line.  Eventually however, the RPN lists will not be stored and this line change detection will have to be changed.  More on this later, but this change will be made once the recreator is implemented.

A newly translated line can't be encoded until it has been determined that the line has changed because the process of encoding adds or updates references in the dictionaries.  If the line then hasn't changed, this would need to be undone, which would be unnecessarily complicated.  Therefore, the encoding of the line was delayed until after it is determined that the line changed.  Since this does not affect new line insertions, the line also needs to be encoded for the insert operation.  The line is only encoded in both places if there was no translation error.

[commit b5c9020cc8]

Friday, October 25, 2013

Program – Dictionary Debug Output

The program debug output shows the indexes of dictionary entries, but this is insufficient for showing if the dictionary entries were removed correctly and are placed on the free stack of the dictionary for reused.  Code was added to output the contents of each dictionary.

The debug text routine was added to the dictionary class that takes a header string as an argument.  After appending the header string to the output string, it loops through the dictionary entries and appends the index, use count and string of every entry with a non-zero use count.  After the entries, the indexes in the free stack are appended.  If any free stack item contains a non-zero use count, the use count is appended after the index.  Also, if the item has a non-empty string, the string is also append.  The strings of deleted entries should be cleared.

The debug text dictionaries routine was added to the program model class, which calls the debug text routine of each dictionary and appends each to the output string.  A call to this routine was added to the tester class run routine after the program model debug text function is called to output the program code.  The expected results for encoder test #1 and #2 were updated for the additional dictionary debug output.

[commit 11337cb673]

Program – Dereference Removed Lines

When a line of code is replaced or removed, the use counts of any dictionary entries referenced on the line need to be decremented.  If the use counts becomes zero, the entry is no longer being used and needs to be deleted from the dictionary (the entry becomes available for use by another item upon the next add).

The dereference routine was added to the program model to scan a line that is about to be replaced or removed.  This routine loops through each program word of the line and removes the reference for any code that has an operand, which is determined by whether the code has a remove function.  In the update line routine, this routine is called before the line is replaced or removed.

Table entry remove functions were added for the various REM, constant and variable codes.  Each remove function calls the remove routine of appropriate dictionary (just like the encode function calls the add routine of appropriate dictionary to add a reference).

Encoder test #2 contains replace and remove operations, but previously the use counts of dictionary entries were not being decremented.  Now that they are, several dictionary entries are now removed since their uses counts become zero and are removed from the dictionary.  This allowed new items to be added in the unused entries, which affected the index of several dictionary entries on some of the program lines, therefore the expected results were updated.

[commit a310dc458e]

Monday, October 14, 2013

Encoder Testing – Program Output

Now that encoder testing is inserting the input test lines into an actual program model, it would be helpful to know if the lines are being inserted correctly.  The encoded line is output after each input line, and while this is extracted from the program model, it is not known if the lines are actually in the correct location within the program.

The offset and size could have been added to the output line, but this is insufficient for testing if lines are replaced and removed correctly.  These operations have not been implemented yet, but will be shortly.  Therefore, at the end of testing, all of the lines currently in the program model are output.  If a line contains an error, the column, length and message of the error are output.

Included in the output is the index number of the line, the offset range of the line to verify that lines are inserted in the correct place with no gaps between lines, and the debug text output for the line.  Blank lines take no space in the program code and so no offset range, so only the offset is output.  The offset is needed in case the blank line is replaced.  The expected results for encoder test #1 were updated for the new output.

[commit 96060db9b7]

Sunday, October 13, 2013

Program Model Update – Encoding

The program model needs to encode lines when an update signal is received once each line has been successfully translated.  The program model update slot routine receives this signal from the edit box, which includes the line number, number of lines deleted, number of lines inserted, and a list of strings of the lines.  The type of operations needed on the program is then determined (change, insert or remove lines) and calls the update line routine to perform the operation for each line affected.

The update line routine was modified to encode the line after a successful translate.  A temporary program word vector was added to the line info list to the code for the line.  The line info list currently holds a pointer to the translated RPN list (temporary), and an index to the error list if the line has an error.  The program model will contain a single program word vector for the entire program unit with the line info list containing offsets and sizes of the lines within this vector.  This will allow execution through the program without using time dealing with lines.  Only the insert operation was modified to store the line code vector at this time.

The tester class encode input routine was modified to let the program model translate and encode the line, by calling the update slot routine directly to insert a single line to the end of the program.  An access function to the RPN list pointer was added to the program model to get the RPN list for a line to determine if there was a translation error and to get the information about the error (column, length and message).  The debug text routine was modified to take a line index argument instead of a program word pointer and a word count, which uses the line index to get the program line code and work count.  Because the program model still possesses the RPN list, the encode input routine can no longer delete it.

As a result, a token memory leak occurred (which could previously be seen when running the GUI and then exiting) because the program model was not deleting the RPN lists contained in the line info list when the application exited.  This was corrected by deleting all of the RPN lists in the program model destructor.  The tester class run routine was modified not to report token errors after each line, which will be reported when the application terminates.

[commit 49b3231acb]

Saturday, October 12, 2013

Encoder Class – Removal

The next step is to implement the code to insert the encoded lines into the program (currently the translated RPN lists are stored).  Part of this process that is already implemented is to increment the use counts for dictionary items when a line is inserted into the program.  The encoder handled this part.  Related to this is when a line is removed from the program - the use counts need to be decremented for dictionary items that were on the line (deleting a dictionary item when it is no longer used).

This remove line process appeared to be mirror to the encode (add) line process and therefore the question arose whether the encoder class should also contain a remove routine.  Looking at the current encoder class, it currently contained the single encode routine and no member variables (except for a reference to the table instance).  It also required a pointer to the current program unit, which is only used to pass to the encode functions for each code with an operand.  A reference to the encoder was not passed to the encode function since the encoder class did not contain anything that was needed.

So the conclusion was made that the encoder class was not really necessary and that the encode routine should be in the program model (which contains all the dictionaries needed for encoding) along with the remove routine that will be implemented.  Therefore, the encoder class was removed and the encode routine was moved to the program model class.

[commit 85827d49e8]

Sunday, October 6, 2013

Encoding – Second Phase (Tagged)


This concludes the implementation of the encoder for the planned components needed to getting all modules up to the run-time working.  The next step is fully integrating the encoder into the program model, where lines are inserted into, placed and removed from the program.  Before continuing, the source was tagged version v0.5.2.

[commit cf579cd389]

Constant String Dictionary

The constant string dictionary was a little tricky to get working because of a difficult to resolve memory leak.  The constant string dictionary will also be an information dictionary, but was not defined as an information template dictionary directly.  The information required for this dictionary contains a pointer to a QString instance.  At run-time, an array of QString pointers will be used for the string constants (though the pointers will be wrapped within a class syntactically).

As for number constants, a constant string information class was added with a lone QString pointer to the string constant value.  Even though the information will contain a new instance of a QString, due to the implicit sharing feature of Qt, the underlying string will reference the original string in the token.  The original string will also similarly be referenced by the key list and key map members of the dictionary.  After the token is deleted, the string is still referenced three times (information element, key list and key map).  The string is not deallocated until all three instances are removed.

When the constant string information element is created for a new element, a new QString instance is created from the string in the token.  To prevent a memory leak, these QString instances need to be deleted.  A destructor was needed for the constant string dictionary, so a new constant string dictionary class was created.  This new class overloads the destructor, which deletes all the QString instances in the information elements.  It will be necessary to nullify this QString pointer when the string constant is removed from the dictionary.  The destructor then just deletes all the instance pointers and if one is null then no action is taken.

Initially, an information class instance was created locally in the encode routines and a reference to it was passed to the information dictionary add routine.  For constant strings, a QString instance was created in this local instance.  This was not an issue when the copy of the instance was put into the information vector for a new or reused instance as the instance pointer was copied into the vector.  However, a memory leak occurred when the constant was already in the dictionary and the instance was left hanging (the QString instance was not deleted).  This was previously resolved in the information dictionary template class by having its add routine create the instance only when adding a new item.

To complete the implementation, a constant string dictionary was added to the program unit class with an access function.  The constant string encode and operand text routines were added that simply call the dictionary add and string routines.  Finally, pointers to these functions were added to the associated table entry.  The expected results file for encoder test #1 was updated for the numeric constants that are now in the output.  A new statement was added with a string containing an embedded double quote character.

[commit 8a85668458]

Constant Number Dictionary

The information template dictionary will be used for the constant number dictionary.  The additional information will hold the double and integer value of the constant, and is obtained from the same members in the token.  Even through for double constants that can't be converted to an integer, the integer value (which is not set) is still copied to the information element, but it won't actually be used at run-time.

A constant number information class was defined containing the double and integer values.  This class was given a blank default constructor (required for the QVector class), and a constructor taking a token pointer to initialize the values (required for the information template dictionary).

To complete the implementation, a constant number dictionary was added to the program unit with allocation and an access function.  The constant number encode and operand text routines were added that simply call the dictionary add and string routines.  Finally, pointers to these functions were added to the associated table entries.  The expected results file for encoder test #1 was updated for the numeric constants that are now in the output.

[commit babfb758ec]

Saturday, October 5, 2013

Variable Dictionaries

When the program is run, the values for the variables need to be initialized (numbers to zero, and strings to empty).  If all three data types of variables are stored in the same dictionary, the initialization routine would need to check the data type for each variable before initializing its value.  This is not necessary if a variable dictionary contains a single data type, in which case it just has to loop through and initialize a single value type.  The variable dictionaries will not hold the actual variable values.  The run-time module will allocate and own the arrays with the values.

For now, no other information is needed in the variable dictionaries.  The variable name is stored as the dictionary key, and the data type is implied by which dictionary contains the variable.  Therefore at this moment, the base dictionary class is sufficient for the variable dictionaries.  However, eventually addition information will need to be stored with the variable.

To complete the implementation of the variable dictionaries, three dictionaries, one for each data type, were added to the program unit class with the associated allocation and access functions.  Encode and operand text routines were implemented for each of the three data types.  These routines call the add (for encode) or string (for operand text) routines of the variable dictionary for the associated data type.  These functions were placed in a the new basic.cpp source file, which will also contain the run-time routines for the variable and variable reference codes along with other miscellaneous functions (the two rem functions were also moved into this file).

Finally, pointers to these functions were placed in the six table entries for these codes (variable and variable reference times the three data types).  Several new statements were added to encoder test #1 so that there are more than one variable of each data type.  The expected results file for this test was updated for the variable names that are now in the output along with the output of the new statements.

[commit 2843b44761]

Operand Text / Remark Operands

Next up was to retrieve the text for the operand (comment string) of the remark command or operator from the dictionary using the index that is stored in the program code.  The recreator also needs to do this, therefore, a mechanism was implemented to allow for the getting the text for the debug output, which will later be used for the recreator.

A new operand text function pointer was added to the table entry.  This function is only needed for codes that have an operand, and an access function was added for this pointer.  These functions have arguments for the pointer to the current program unit (to access the dictionaries) and the operand.  The rem operand text function was implemented to get the text of the remark for an operand (an index) by passing it to the new dictionary string access function (gets the text from the key list member).

The program line text routine was renamed to debug text since it is only used for generating text for test output.  Also, since this routine needs access the program unit for the dictionaries to look index values, it was moved to the program unit class.  Since the program line class was only created to add the text routine to a QVector, this class was removed and  QVector<ProgramWord> is now used.

The operand debug text (renamed) routine outputs the index of the operand along with the value of the operand, and needed to output the text of the operand.  To get the pointer to the operand text function, it would need to be passed the code for the operand and would need the table instance.  Instead, this functionality is handled by the debug text routine and the resulting text is passed to this routine to add the word index and operand index values.

The debug text routine also needs the table instance, so a table reference was added to the program unit class.  This routine will also be called when encoded debug text is integrated into the program view of the GUI.  The test routine called this routine with a program line (now a program word vector), however, when used for the program view, a program word vector would need to be extracted from the program unit code vector.  This would require a vector to be copied from part of the total program code vector.  To prevent this copy, the debug text was modified to take a program word pointer and a count of the number of words in the line.

Some minor cleanup of the table entries were performed on a separate commit (placed function pointers on their own lines, corrected the camel casing of the rem encode routine, and fixed a comment).  The expected results file of encoder test #1 was updated for the remark comments that are now in the output.

[commit f5fe1efa4f] [commit bf9264bafb]

Thursday, October 3, 2013

Remark Dictionary

The remark dictionary is the first dictionary implemented.  This is the simplest dictionary as it only needs to store the string of a comment, so the base dictionary is sufficient for this dictionary.  The ProgramUnit class was added to contain the remark dictionary, which is defined as a pointer and allocated in the ProgramUnit constructor.  A pointer was used since all the other program units will use the same remark dictionary.

Previously, the encoder encode routine just set the operand to zero and was modified to call the encode function for the code with an operand.  For now, if the code does not have an encode function, the operand is still set to zero.  The encode functions contain arguments for the pointer to the current program unit and the pointer to the token being encoded.  Additional arguments will probably be needed, but these are sufficient for now.

The rem encode function was implemented, which simply passes the token to the add function of the remark dictionary and returns the index of the entry to be set as the operand word.  The pointer to an encode function was added to the table entry structure and a table access function was added to return this pointer.  A pointer to the rem encode function was added to the REM command and REM operator code table entries.

For testing, a temporary program unit was added to the main tester run routine and a pointer to it is passed to the encode input routine, which passed to the encode routine.  Additional remarks were added to encoder test #1 including one set of duplicate comments to test that a single entry is stored in the dictionary.  Currently only the index value is output as the string (of the remark) is not yet retrieved, which will be the subject of the next change.  For now, the current output of this test (though incomplete) was copied for the expected results.  Though this makes the test pass, the purpose for changing this file was detect changes as more of the encode routines are implemented.

[commit d2222df1d8]

Tuesday, September 24, 2013

Encoding Program Lines (Begin)

The initial version of the encode routine was implemented that takes the token list generated by the translator and converts it to a vector of program words, the contents of which will be inserted into the program.  The size for the program word vector is obtained from the token list, which was calculated at the end of a successful translation.  For now, the operand words are set to zero for instructions with an operand.  Dictionary look ups will be required to generate the indexes for the operands.

The ProgramWord class was implemented containing a single unsigned short (16-bit) word variable with instruction and operand access functions.  This class does not determine whether the word is an instruction or an operand, which is up to the user of this class.  There are access functions for getting the code from an instruction word, checking if an instruction word has a sub-code set, setting an instruction to a code and sub-code, getting the operand word, and setting the operand word.

The ProgramLine class was implemented to contain a vector of program words and is based on the QVector class of the ProgramWord class.  A class was implemented instead of just using a vector so that a text routine could be added to convert the program line into text for testing.  The instruction text and operand text routines were implemented in the ProgramWord class.  I single text could not be implemented since by itself, this class does not know whether the word is used as an instruction or an operand.

The test encode input routine was modified to call the encode routine upon a successful translation and output the encoded program line (a vector of program words) using the program line text routine.  Since the operand words are set to zero, encoder test #1 currently fails since the text for operands is do no match.  This will be handled as the dictionaries are implemented.

[commit 60add1604a]

Sunday, September 22, 2013

Condensing Sub-Code Bits

A program word will consist of instruction words and operand words.  The instruction word will consist of the instruction code and sub-codes.  The sub-codes will generally only be used to recreate the original program and will not be used during execution (though there will be a few exceptions).  The code will reside in the lower 10 bits of the instruction word, which will allow for 1,024 different codes (which should be sufficient).  This leaves the upper 6 bits for the sub-codes.

There are already five sub-codes that need to be stored in the instruction word, including the Parentheses, Colon, LET, Keep and Question sub-codes.  This only leaves one bit for another sub-code and there are a lot more commands to implement.  The sub-code bit usage needed to be reduced.  As it so happens, the LET, Keep and Question sub-codes will never be on the same code, so only one bit is really required for these sub-codes.

To accomplish this, these three sub-codes were replaced with the new Option sub-code.  The LET and INPUT translate routines now set this sub-code instead of the individual sub-codes that were removed.  This new sub-code also can be reused for other commands requiring an individual sub-code.

The bits of the sub-codes were also rearranged with the sub-codes that will be used in the instruction words (Parentheses, Colon, and Option) in the same bit positions.  This will prevent having two sets of sub-code definitions.  A new Program Mask sub-code definition was added that will be used the mask the program sub-codes from the other token sub-codes when the instruction is created.

So that the proper sub-code can be output during testing, an option name variable was added to the table entries.  Commands used the option sub-code will have the text name of the sub-code in the option name.  In the token text routine, if a token has the Option sub-code, the option name is output (or the string "BUG" if the code does not have an option name).

[commit e721a3e3ae]

Encoding – First Phase - Revisited (Tagged)

The tokenCodes0.5 branch of development was successful so it was merged back into branch0.5 (a fast forward merge meaning the branch0.5 pointer was simply moved).

Since the commands.h header file will contain definitions for other things (like routines for constants), it was renamed to the more generic basic.h, which was also added as a dependency to the application binary in the CMake build file so that it appears in the Project source file list within QtCreator.

In the table entry initialization, it turns out that using "" (blank string) and NULL (pointer) for an initializer to a constant QString variable has the same effect, therefore, all "" were replaced with NULL in the table entries.

The first phase required for encoding is complete (again) except now it is now performed by the translator.  This is a good point to tag version v0.5.1.  Work will now begin again in encoding the tokens into the internal program code format to be stored in the program model.

[commit b461229bcd] [commit f64d4401d2]

Friday, September 13, 2013

Encoder/Translator – Design Considerations

In preparing for implementation of the next Encoder step of actually encoding the RPN list of tokens into program code, I realized that were some design issues.  The first minor issue was that assigning codes and assigning program code word indexes with word counting can't be in the same loop because later for arrays, integer conversion codes may need to be inserted after subscripts, which will trip up the indexes already sett and the words counted.  These indexes and word count could be adjusted as items are inserted, but it would more efficient to have two loops.

In further consideration of the design, I realized that the these first steps should really be in the translator and not the encoder.  The code for these steps were moved to the translator (executed after successfully getting the commands for the line).  A new test mode option was needed so that this call would not be made when just testing the translator so the expected translator test results would not change.

However, I realized that the translator could assign the codes before waiting until the end of the translation.  The codes could be assigned when the tokens are first processed, specifically in the get operand routine.  Also, the translator cannot work in a vacuum, meaning it will need access to the existing program, specifically the various dictionaries so that the types of tokens can be determined (variables, arrays, or functions).

Therefore, the translator will be changed to this new scheme of assigned codes to tokens in the get operand routine, but over the next several commits, with the exception that the dictionaries are not implemented yet.  The first change made was to move the first two encoder steps into the translator and two separate loops, though this is a temporary step (and the encoder class is now empty, but this is also temporary).

The second change in this transition was to add a temporary check to determine if an identifier with parentheses is an array or function.  Eventually this will be accomplished with the array and function dictionaries.  To make this determination simple, if the identifier starts with the 'F' letter, then it is considered a function, otherwise it is considered an array.  This affected the expected results because array subscripts are checked for integers with integer conversions added as needed.

[commit f9f0cd3de7] [commit 9111d8e0f8]

Saturday, September 7, 2013

Encoder – First Phase Complete (Tagged)

The initial implementation of the encode is complete, which includes preparing the tokens for encoding.  Version v.0.5.0 has been tagged.  Work will now begin in encoding the tokens into the internal program code format.

[commit 6ed93f833c]

Encoder Test Mode – Blank Lines

When the new line was added to encoder test #1, a blank line was added before the new line, but the line was ignored.  The test run routine was modified to allow a blank line only when testing the encoder.  The expected results were updated accordingly.

[commit c84fbfa08b]