LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-26-2014, 06:59 PM   #76
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,416

Original Poster
Blog Entries: 43

Rep: Reputation: 36

My line numbers had were origionally needed when I started, to "move" the code to the right spot. Now that's been dealt with a long time ago. Line numbers in the source code are not needed. It generates the code, and it's in the right spot.

It first generates psudocode, that is in a data structure with a name, and some code. These are code "chunks", that make up the whole thing. Each part of the syntax, has a name, and a chunk of psudocode. Later on from that, it is joined and put into an array, which could still be modified if necessary. Then the array is output to a file. From my current understanding, though this is flat, this is basically a type of AST.

What I needed to do, was know how to fix up the label numbers in the generated code. The transformation occurs after the code is in the array, but before the code is dumped to a file. This is so that I can see all the label numbers in the program.

It is how to fix it up that remains, unless my clues to use to fix it up are wrong, I can fix it up using my clues and then end up with correct code for all if-else scenerios. I just am not sure how to fix it up and if my clues are wrong, I'm not sure what kind of clues to leave to fix it up.
 
Old 08-26-2014, 07:05 PM   #77
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,416

Original Poster
Blog Entries: 43

Rep: Reputation: 36
I could, go back and add features in the interpreter for string label names. This would end up with code to jump to a label name, instead of a label number. But this might sort of shift the problem a little. Since no two labels can be the same, or it won't know which one to jump to, I'd have to fix up the label names with numbers as well as names, so that the code for the if-else wouldn't make them all the same label names for it's parts, over and over. Then I'd need code to know which number as well as which name. Probably I could do this using the ifcounter, which counts the number of ifs.
 
Old 08-26-2014, 07:07 PM   #78
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,416

Original Poster
Blog Entries: 43

Rep: Reputation: 36
If this problem is indeed to complicated you guys think to solve without more knowledge, then I will close it and study more before finishing, but I'm not sure that it is, as I am getting very close to a solution I believe.
 
Old 08-28-2014, 01:45 AM   #79
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,416

Original Poster
Blog Entries: 43

Rep: Reputation: 36
This is now my algorithm:

Code:
  1. Count the labels until there is an if-else.
  2. Count the if else labels.
  3. Replace end with the results of (step 1 - 1) + (step 2 - 1).
  4. Count the labels from the if begin until the if code end.
  5. Replace if_part with (step 4) - 1.
  6. Count the labels from the if begin until the if or else end.
  7. Replace else_part_or_end with (step 6) - 1.
I think that would work if there was no ladder, but there is a the possibility of a ladder, so I need to take that into account.
 
Old 08-28-2014, 02:50 AM   #80
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Did you not read the posts about 'ifcounter', or did you not understand?
Or are you in 'write/only' mode?
 
Old 08-28-2014, 03:17 AM   #81
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,416

Original Poster
Blog Entries: 43

Rep: Reputation: 36
Perhaps I didn't understand. My intent with the ifcounter variable, is to count the number of if statements. In my current code, I was using ifcounter to tell apart the ifs, such as:

Code:
VERSION TVOID 0V


VOID TVOID 0V
; Begin if :1
ALOAD TBOOLEAN true
CGOTOL TVOID if_part
GOTOL TVOID else_part_or_end


LBL TVOID 0V
VOID TVOID 0V
; End if code :1
GOTOL TVOID end


LBL TVOID 0V
; End if or else code :1
QUIT TVOID 0

END TVOID 0V
The 1's, are telling me that part belongs to if number 1.
 
Old 08-28-2014, 05:14 AM   #82
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Well, I don't understand (nor wish I to understand) your Assembly language, but the ifcounter-problem is quit interesting (partly because of the optional 'else' part), so I've hacked a minimal example with minimal/no code-generation:

Code:
if:     IF                { /* $2 */
                            int current_if;
                            current_if= $$.ifcounter= ++ifcounter;
                            fprintf (stderr, "Here starts if#%d\n",
                                current_if);
                            fflush (stderr); }

        '(' expr ')'      { /* $6 */
                            int current_if= $2.ifcounter;
                            fprintf (stderr, "We are after the code of if#%d / condition-part\n",
                                current_if);
                            fprintf (stderr, "\tJMP_conditional NMATCH_%d\n", 
                                current_if); }

        stmt              { /* $8 */
                            int current_if= $2.ifcounter;
                            fprintf (stderr, "We are after the code of if#%d / then-part\n",
                                current_if);
                            fflush (stderr); }

        opt_else          { /* $10 */ };

opt_else:                 { /* $1 */
                            /* WATCH OUT: GETTING VALUE FROM 'if:' rule */
                            int current_if= $-6.ifcounter;

                            $$.obj = NULL;
                            fprintf (stderr, "NMATCH_%d: LABEL\n",
                                current_if); }

        | ELSE            { /* $2 */
                            /* WATCH OUT: GETTING VALUE FROM 'if:' rule */
                            int current_if= $-6.ifcounter;

                            fprintf (stderr, "\tJMP ENDIF_%d\n",
                                current_if);
                            fprintf (stderr, "NMATCH_%d: LABEL\n",
                                current_if);
                            fflush (stderr);
                          }
          stmt            { /* $4 */
                            /* WATCH OUT: GETTING VALUE FROM 'if:' rule */
                            int current_if= $-6.ifcounter;

                            fprintf (stderr, "We are after the code of if#%d / else-part\n",
                                current_if);
                            fprintf (stderr, "ENDIF_%d: LABEL\n",
                                current_if);
                            fflush (stderr); };
example input:
Code:
	 IF (1 == 2) PUT ('then1')
	 ELSE IF (2 == 3) PUT ('then2')
	 ELSE PUT ('else');
example output:
Code:
Here starts if#8
We are after the code of if#8 / condition-part
        JMP_conditional NMATCH_8
We are after the code of if#8 / then-part
        JMP ENDIF_8
NMATCH_8: LABEL
Here starts if#9
We are after the code of if#9 / condition-part
        JMP_conditional NMATCH_9
We are after the code of if#9 / then-part
        JMP ENDIF_9
NMATCH_9: LABEL
We are after the code of if#9 / else-part
ENDIF_9: LABEL
We are after the code of if#8 / else-part
ENDIF_8: LABEL

Last edited by NevemTeve; 08-28-2014 at 05:30 AM.
 
Old 09-08-2014, 01:21 AM   #83
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,416

Original Poster
Blog Entries: 43

Rep: Reputation: 36
I just modified the interpreter so that I can use label names as well as label numbers. I will try to use my label names to write the code now instead of the label numbers. I may be just shifting the problem, but I'll try it and see if I shifted the problem or not. I think ifcounter already does what it is intended to do.
 
Old 09-10-2014, 10:24 PM   #84
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,416

Original Poster
Blog Entries: 43

Rep: Reputation: 36
Here is a sample now of the input and output:

input:
Code:
;
if (3 + 2)
 ;
end 0;

output:
Code:
VERSION TVOID 0V


VOID TVOID 0V
; Begin if :1
ALOAD TBOOLEAN true
CGOTOL TSTRING "if_part_1"
GOTOL TSTRING "else_part_or_end_1"


LBL TSTRING "if_part_1"
VOID TVOID 0V
; End if code :1
GOTOL TSTRING "end"


LBL TSTRING "else_part_or_end_1"
QUIT TVOID 0

END TVOID 0V

Perhaps this is easier to transform? A fully transformed file in this case would be:

Code:
VERSION TVOID 0V


VOID TVOID 0V
; Begin if :1
ALOAD TBOOLEAN true
CGOTOL TSTRING "if_part_1"
GOTOL TSTRING "end_1"


LBL TSTRING "if_part_1"
VOID TVOID 0V
; End if code :1
GOTOL TSTRING "end_1"


LBL TSTRING "end_1"
QUIT TVOID 0

END TVOID 0V
 
Old 09-10-2014, 10:25 PM   #85
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,416

Original Poster
Blog Entries: 43

Rep: Reputation: 36
So I'd probably need help with a possible algorithm to transform the code...
 
Old 09-10-2014, 10:27 PM   #86
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,416

Original Poster
Blog Entries: 43

Rep: Reputation: 36
I'm very close to a working solution...
 
Old 09-10-2014, 10:52 PM   #87
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,416

Original Poster
Blog Entries: 43

Rep: Reputation: 36
P.S. - Not that it matters much, but now I got a new computer and am developing on a Windows 8.1 machine.
 
Old 09-10-2014, 11:08 PM   #88
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,416

Original Poster
Blog Entries: 43

Rep: Reputation: 36
Here is another example:

ifelsetest:
Code:
if (3 + 2)
 ;
else
 ;
compiles to:
Code:
VERSION TVOID 0V


; Begin if :1
ALOAD TBOOLEAN true
CGOTOL TSTRING "if_part_1"
GOTOL TSTRING "else_part_or_end_1"


LBL TSTRING "if_part_1"
VOID TVOID 0V
; End if code :1
GOTOL TSTRING "end"


LBL TSTRING "else_part_or_end_1"
VOID TVOID 0V
; End if or else code :1
GOTOL TSTRING "end"


LBL TSTRING "end"
; End if :1

END TVOID 0V
It already compiles correctly.
 
Old 09-10-2014, 11:14 PM   #89
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,416

Original Poster
Blog Entries: 43

Rep: Reputation: 36
Here is complicatediftest:

Code:
if (3 + 2)
 ;
else if (false)
 ;
It compiles to:
Code:
VERSION TVOID 0V


; Begin if :1
ALOAD TBOOLEAN true
CGOTOL TSTRING "if_part_1"
GOTOL TSTRING "else_part_or_end_1"


LBL TSTRING "if_part_1"
VOID TVOID 0V
; End if code :1
GOTOL TSTRING "end"


LBL TSTRING "else_part_or_end_1"
; Begin if :2
ALOAD TBOOLEAN false
CGOTOL TSTRING "if_part_2"
GOTOL TSTRING "else_part_or_end_2"


LBL TSTRING "if_part_2"
VOID TVOID 0V
; End if code :2
GOTOL TSTRING "end"


LBL TSTRING "else_part_or_end_2"
; End if or else code :1
GOTOL TSTRING "end"


LBL TSTRING "end"
; End if :1

END TVOID 0V
It compiles fine...
 
Old 09-10-2014, 11:17 PM   #90
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,416

Original Poster
Blog Entries: 43

Rep: Reputation: 36
complicatediftest2:
Code:
if (3 + 2)
 ;
else if (false)
      ;
     else
      ;
Compiles to:
Code:
VERSION TVOID 0V


; Begin if :1
ALOAD TBOOLEAN true
CGOTOL TSTRING "if_part_1"
GOTOL TSTRING "else_part_or_end_1"


LBL TSTRING "if_part_1"
VOID TVOID 0V
; End if code :1
GOTOL TSTRING "end"


LBL TSTRING "else_part_or_end_1"
; Begin if :2
ALOAD TBOOLEAN false
CGOTOL TSTRING "if_part_2"
GOTOL TSTRING "else_part_or_end_2"


LBL TSTRING "if_part_2"
VOID TVOID 0V
; End if code :2
GOTOL TSTRING "end"


LBL TSTRING "else_part_or_end_2"
VOID TVOID 0V
; End if or else code :2
GOTOL TSTRING "end"


LBL TSTRING "end"
; End if :2
; End if or else code :1
GOTOL TSTRING "end"


LBL TSTRING "end"
; End if :1

END TVOID 0V
compiles fine.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Portable Numbers Format: PNFHA des_a Programming 14 07-18-2014 10:51 PM
[SOLVED] Prime Generation Code Not Working Properly ashok.g Programming 29 12-14-2009 03:59 AM
A GCC code generation problem? dogbird Programming 4 12-09-2005 11:52 AM
tool for code generation? bcalmac Linux - Software 0 08-22-2005 10:41 AM
UML and C++ code generation GŠutama Linux - Software 2 11-13-2003 06:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration