java peephole optimization beginner compilers [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
As part of a group project I'm writing a compiler for a simplified language. As one of the optional features I thought I'd add a peephole optimizer to go over the codegen's output intel assembly code and optimize it.
Our compiler is done in java and it seems like it's going to be a lot of work to create this peephole optimizer using the java I've learned so far. Is there some sort of tool I should be using to make this possible, as pattern matching strings doesn't sound like a good approach in java.
thanks

Peephole optimization ought to be done on a binary representation of the parse tree, not on text intended as input to the assembler.

Hard to say without looking at the design of your compiler, but typically you would have an intermediate step between generating code and emitting it. For example, you could think of having the output of the code generation phase be e.g. a linked list of instructions, where each instruction object stores the kind of instruction, any arguments, label/branch destinations, and so on. Then each pattern would inspect the current node and its immediate successors (e.g. if (curr.isMov() && curr.next.isPush() && ...) and modify the list accordingly. Then your peephole optimizer starts with the codegen output, runs each pattern on it, and does this over and over until the list stops changing. Then you have a separate phase which just takes this list of instructions and outputs the actual assembly.

I definitely wouldn't use strings for this. You might look at lex/yacc, and its ilk (e.g. Jack is one for Java, although I haven't used it) to generate the AST of the assembly, then run optimizations on the AST, and write out the assembly again … but you do realise this is a hard thing to do, right? :-)

Related

Just need help starting up for a program that transfer from c++/c to mips [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
first of all I want to make it clear that's what I am asking is for an assignment.
The idea was to make a program in c++ or java where the user enter a simple c code (A=B, a while loop, a for loop and array functions) and the output is the mips code (lw addi etc) we are free to use whatever registers doing so.
Surely I am not asking for the whole code. The problem is in my head I can only think of hardcoding the whole things inputting a string then cutting it into part and then run a hideous amount of if condition and or switch cases I think there must be a simpler way but I can't get my mind on it.
Damn, whenever you get to studying compilers this will be crazy to look back to.
Anyway you are probably not in the level of building a C compiler from scratch.
So think of it this way, you need to have a set of instructions and yes you will have to go through the program string and transform the string into MIPS instructions.
The most naive way to do it and it might be what your teacher expects unless it is a compiler course, would be to parse the program text line by line as it is expected to be a simple program and as you said have a lot of conditions for each type of expression that you will evaluate.
A tip: Save for / while loops as a label as soon as you read them and from there you need to check where is the endpoint of it for the jump when necessary (completed for/while conditions etc)
Now if it is a compiler project. I'd strongly recommend you read into this book:
Compilers: Principles, Techniques, and Tools
Because to build a real compiler you need to understand all the stages of a compiler and how does it work together... You would also need to know some Language Theory.

Run a math expression from string in java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to know if there is any efficient method to Run a math expression from string in java , Having some example input and results of that function.
Starting from simple linear functions : a*x+b .To more complex ones
Or is there any good source i can start reading.
I take your task as: take observed input-output and learn some representation which is able to do that transformation with new inputs.
(Some) Neural Networks can learn an approximation-function (Universal approximation theorem
) (and probably other approaches), but there is something important to remark:
Without assumptions about your function (e.g. smoothness), there can't be an algorithm achieving what you want to do! Without assumptions there are infinite many approximation-functions, which are all equally good on your examples, but behave arbitrarily different on new data!
(I'm also ignoring special-cases as: random-data or cryptographic random-generators where this mapping also can't be learned (the former in theory; the latter at least in practice)

What are some performance-improving tips for Java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Hey guys,
I'm a relatively new programmer in Java(and in general), but I want to know different ways of minimizing memory and RAM usage in programs that I make. I've heard of a few such as StringBuilder as an alternative to + String concatenation and stuff, but I'd like to hear what you guys know about how to maximize performance and why.
Thanks in advance!
In modern programming, it is a far better use of time your time to focus on making your code readable rather than trying to micro-optimse.
Modern compilers do an extremely impressive job of these small optimisations so that everyday programmers don't have to deal with them, and in the majority of cases it is better to leave it up to the compiler than to attempt it yourself.
In general I would say that the largest performance improvements can be gained by thinking about the design of your program ahead of time, before you even start typing. Once you've already bashed out 10,000+ lines of code implementing your latest 3D high-performance MMORPG, and you realise it's not as high-performance as you were hoping, making any drastic design changes will be considerable work. Some things to think about beforehand are:
Think about your algorithms complexity, for example string concatenation can be O(n^2) using String, but O(n) using StringBuilder.
Use object pools to reuse memory rather than creating new instances each time
Reuse existing library implementations of data-structures etc, rather than trying to recreate them yourself. Many more man-hours will have been put into these implementations than you could possibly spend on them, and so they are likely to be more efficient/robust
Finally I should mention, that if you do go trying to optimise some existing code because its not performing as well as needed, it's very important to know specifically where the problem area is. In this case a profiler is invaluable, and should help pinpoint any particular areas that are affecting performance. They might not be where you expect!

My own code vs library [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
This is kind of unusual question for developers but for some reason i want to post it here and hope to get adequate answer.
Here is a simple example:
I wrote a java function that calculates distance between two geo points. The function is not more than 50 lines of code. I decided to download a source code from ibm that does the same thing but when i opened it i saw that it looks very complicated and is almost thousand lines of code.
What kind of people write such source code? Are they just very good programmers? Should i use their source code or my own?
I have noticed this kind of thing lots of times and i from time to time i start to wonder if it is just me who do not know how exactly to program or maybe i am wrong?
Do you guys have the same kind of feeling when you browse throught some other peoples source code?
The code you found, does it do the exact same calculation? Perhaps it takes into account some edge cases you didn't think of, or uses an algorithm that has better numerical stability, lower asymptotic complexity, or is written to take advantage of branch prediction or CPU caches. Or it could be just over-engineered.
Remember the saying: "For every complex problem there is a solution that is simple, elegant, and wrong." If you are dealing with numerical software, even the most basic problems like adding a bunch of numbers can turn out to be surprisingly complex.

Test equality of an API which is present in different languages [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
How would you tackle the following problem?
I've got an API which validates tokens (which are just simple XML files). So the API specifies a bunch of validation methods like validateTime(String tokenPath), validateFileHash(String tokenPath) or validateSomthingElse(String tokenPath).
The API is already implemented in two different languages, Java and C. My task is to make sure, that both versions behave the same. So if Java throws a TokenExpiredException after invoking validateTime("expiredToken.xml"), C should return a corresponding error value (in this case a predefined -4 for TOKEN_EXPIRED).
The good old approach would be to write Unit/Integration-tests in both languages. However, this would require double the effort as I would have to implement essentially the same Tests in Java and in C.
My idea was to define a XML-Schema for TestCases which would look something like this.
<!-- TestCases.xml -->
<testcase>
<tokenpath>expiredToken.xml</tokenpath>
<apiMethod>validateTime</apiMethod>
<expectationJava>TokenExpiredException</expectationJava>
<expectationC>-4</expectationC>
</testcase>
<testcase>
...
</testcase>
Furthermore, I would build a small Java tool to parse TestCases.xml and directly invoke both API versions (using JNI for C) to match the outcome to the preset expectations.
Do you think this is a feasible plan, or is it better to do the old approach? Are there Frameworks to deal with this kind of tasks or is it a smelly idea to begin with?
Your approach is feasible, what would be even better is if you can take advantage of some existing data driven testing frameworks. This way you don't need to do the legwork of parsing inputs, running test cases and asserting outputs.
Here's an example of how to drive Java tests through JUnit + an excel spreadsheet containing the data: http://www.wakaleo.com/component/content/article/241
I didn't see one immediately, but hopefully you can find something similar for C.

Categories