DotModificator.cpp 2.17 KB
Newer Older
1 2
#include <iostream>
#include <fstream>
3
#include <string>
4 5 6 7 8 9

#include "DotModificator.h"

namespace lrusecurity
{

Julien Balette-Pape's avatar
Julien Balette-Pape committed
10 11 12 13 14 15 16
void displayString(string str, std::ofstream *flux)
{
    for (String::Iter i = str.begin(); i!= str.end(); i++)
        *flux << *i;
}


Julien Balette-Pape's avatar
Julien Balette-Pape committed
17
void DotModificator::modify(otawa::CFG *cfg, const otawa::icache::Access *access, otawa::Address address)
18
{
19
    std::ofstream flux(_filename, std::ios::app);
20 21 22

    if (flux){
        // Permet de mettre des nodes au même niveau
23
        if (isFirst()) {
24 25 26
            flux << "rankdir = TB;" << io::endl;
        }

Julien Balette-Pape's avatar
Julien Balette-Pape committed
27
        //Copie de l'addresse pour affichage
Julien Balette-Pape's avatar
Julien Balette-Pape committed
28
        string addr = _  << address;
29

Julien Balette-Pape's avatar
Julien Balette-Pape committed
30
        if (access){
31
            // On récupère le bloc correspondant à l'accès grâce à l'InstMatcher
Julien Balette-Pape's avatar
Julien Balette-Pape committed
32
            otawa::BasicBlock *bb_match = getMatcher()->match_access(access);
33

Julien Balette-Pape's avatar
Julien Balette-Pape committed
34
            // Copie de l'adresse de l'accès pour affichage
Julien Balette-Pape's avatar
Julien Balette-Pape committed
35
            string acc_addr = _ << access->address();
Julien Balette-Pape's avatar
Julien Balette-Pape committed
36 37

            // Creation du node UNSAFE
Julien Balette-Pape's avatar
Julien Balette-Pape committed
38 39 40 41 42
            flux << "Unsafe_" << _count << " [label=\"{UNSAFE ";
            displayString(acc_addr, &flux);
            flux <<"|Lead to a hit @ ";
            displayString(addr, &flux);
            flux <<"}\", color = red];" << io::endl;
43

Julien Balette-Pape's avatar
Julien Balette-Pape committed
44
            // On met au même niveau le node Unsafe et le BB auquel il correspond
45
            flux << "{rank = same; Unsafe_" << _count << "; ";
Julien Balette-Pape's avatar
Julien Balette-Pape committed
46
            displayString(cfg->name(), &flux);
47
            flux << "_" << cfg->index() << "_" << bb_match->index() <<";}" << io::endl;   //TODO: à optimisé (1 par BB)
Julien Balette-Pape's avatar
Julien Balette-Pape committed
48

Julien Balette-Pape's avatar
Julien Balette-Pape committed
49 50 51 52 53 54
        } else { // UNSAFE au début
            flux << "Unsafe_" << _count << " [label=\"{UNSAFE at the ENTRY|Can lead to a hit @ ";
            displayString(addr, &flux);
            flux << "|In function ";
            displayString(cfg->name(), &flux);
            flux << "}\", color = red];" << io::endl;
Julien Balette-Pape's avatar
Julien Balette-Pape committed
55
        }
56

Julien Balette-Pape's avatar
Julien Balette-Pape committed
57
        // On incrémente le compteur
58 59
        _count++;
    } else {
60
        cerr << "ERROR: Unable to open the file" << io::endl;
61 62 63 64 65 66 67 68 69 70
    }
}

void DotModificator::finish(void)
{
    std::ofstream flux(_filename);

    if (flux){
        flux << "}" << io::endl;
    } else {
71
        cerr << "ERROR: Unable to open the file" << io::endl;
72 73 74 75
    }
}

}